TestNG is a unit testing framework used by developers, but it has also gained popularity among testers.
It makes life easy of a tester while writing tests.
This post will be focused on one of the important features of TestNG called Assertions.
what is assertion?
A statement that asserts that a certain premise is true
In Testers language, Assertion is a check/validation to compare the expected and actual outcome.
All the assertions are part of the Assert class
Example of a assertion
Assert.assertEquals(boolean actual, boolean expected)
Asserts that two booleans are equal.
More assertions can be found here : http://testng.org/javadoc/org/testng/Assert.html
There are 2 types of assertions
Hard assertions & Soft assertions
Hard Assertion : Throws an AssertException once Assert fails and continues with execution of next test in the suite. For example if you have a bunch of assertions in a @Test, If one assertion fails, the whole test fails.All assertions after the failed assertion are skipped once a assertion fails.
Soft Assertion; soft assert allows to continue with next @Test without throwing any exception
Hard assert:
@Test(priority = 2)
public void ffPageElementsTestHardAssert() {
Assert.assertTrue(ffPage.ff_Type_oneway.isDisplayed());
Assert.assertTrue(ffPage.ff_Type_roundtrip.isDisplayed());
Assert.assertTrue(ffPage.ff_PassengerCount.isDisplayed());
Assert.assertTrue(ffPage.ff_DepartingFrom.isDisplayed());
Assert.assertTrue(ffPage.ff_DepartingMonth.isDisplayed());
Assert.assertTrue(ffPage.ff_DepartingDay.isDisplayed());
Assert.assertTrue(ffPage.ff_ArrivingIn.isDisplayed());
Assert.assertTrue(ffPage.ff_ReturningMonth.isDisplayed());
Assert.assertTrue(ffPage.ff_ReturningDay.isDisplayed());
Assert.assertTrue(ffPage.ff_ServiceClass_EC.isDisplayed());
Assert.assertTrue(ffPage.ff_ServiceClass_BC.isDisplayed());
Assert.assertTrue(ffPage.ff_ServiceClass_FC.isDisplayed());
Assert.assertTrue(ffPage.ff_Airline.isDisplayed());
Assert.assertTrue(ffPage.ff_Continue.isDisplayed());
}
Soft assert:
SoftAssert sa = new SoftAssert();
@Test(priority = 2)
public void ffPageElementsTestHardAssert() {
sa.assertTrue(ffPage.ff_Type_oneway.isDisplayed());
sa.assertTrue(ffPage.ff_Type_roundtrip.isDisplayed());
sa.assertTrue(ffPage.ff_PassengerCount.isDisplayed());
sa.assertTrue(ffPage.ff_DepartingFrom.isDisplayed());
sa.assertTrue(ffPage.ff_DepartingMonth.isDisplayed());
sa.assertTrue(ffPage.ff_DepartingDay.isDisplayed());
}
Soft assertions can be helpful when we are validation forms.