Hard / Soft Asserts in Selenium:
- The Asserts helps us verify the terms of the conditions and decide whether the test has failed or passed.
- Without throwing any exception, a test is considered to be only successful.
- Test script can be very strong and big.
- Assertions is the best way to perform any kind of validations in the tests.
- When a assertion fails, the test script prevents execution, unless it is made in some form.
There are two types of Assert in Selenium:
- Hard Assert
- Soft Assert
Package:
1 2 3 4 5 | import org.testng.Assert; import org.testng.asserts.Assertion; import org.testng.asserts.SoftAssert; |
Hard Assert:
- A Hard Assertion is a type of assertion that instantly throws an exception when a assert statement fails and continues with the next test in the test suite.
- To achieve this, we need to handle the Assertion error which is thrown with a catch block like the Java block.
- After the suit is completed, after completion of the execution, particular test has been marked as passed instead of FAIL.
Different Hard Assert Statements:
1 2 3 4 5 6 7 8 9 10 11 | Assert.assertEquals(actual,expected); Assert.assertNotEquals(actual,expected,Message); Assert.assertTrue(condition); Assert.assertFalse(condition); Assert.assertNull(object); Assert.assertNotNull(object); |
Soft Assert:
- To deal with the disadvantage of Hard Assertions, a custom error handler provided by TestNG is called soft Assertion.
- Soft Assertions is definitely the assertions type that does not throw an exception when a assertion fails and continues in the next phase after the assert statement.
- This is usually used when our test requires many assertions to execute and the user wants to execute all the assertions / code before failing / skipping the tests.
Different Soft Assert Statements:
1 2 3 4 5 | softAssert.assertTrue(false); softAssert.assertEquals(1, 2); softAssert.assertAll(); |
Create object / instance of Hard / Soft assert:
1 2 3 | Assertion hardAssert = new Assertion(); SoftAssert softAssert = new SoftAssert(); |
Example of Hard / Soft Asserts in Selenium:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | package package_name; import org.testng.annotations.Test; import org.testng.asserts.Assertion; import org.testng.asserts.SoftAssert; public class Soft_Hard_Assert { String class_Name = "Soft_Hard_Assert"; Assertion hardAssert = new Assertion(); SoftAssert softAssert = new SoftAssert(); @Test public void hardAssertMethod() { hardAssert.assertTrue(true == true); hardAssert.assertEquals("Soft_Hard_Assert", "Soft_Hard_Assert"); hardAssert.assertEquals(class_Name, "Soft_Hard_Assert"); System.out.println("hardAssertMethod Successfully passed!"); } @Test public void softAssertionMethod() { softAssert.assertTrue(true == true); softAssert.assertEquals("Soft_Hard_Assert", "Soft_Hard_Assert"); softAssert.assertEquals(class_Name, "Soft_Hard_Assert"); System.out.println("softAssertionMethod Successfully passed!"); softAssert.assertAll(); } } |
Output:
1 2 3 4 5 6 7 | hardAssertMethod Successfully passed! softAssertionMethod Successfully passed! PASSED: hardAssertMethod PASSED: softAssertionMethod |
Hard and Soft Assertions in Selenium and Hard and Soft assertion in TestNG with Selenium Webdriver in Java TestNG Assertions and Difference Between Hard Assert and Soft Assert