Monday, 1 December 2014

Hard Assertion and Soft Assertion in Selenium webdriver using TestNG framework




-> Assertion used to verify the result. and if result is not as our expected means its stopped the perticular method execution.

-> There are many assertions available in Selenium WebDriver with TestNG framework and we will look at all of then one by one.

-> Assertions are important and very useful in any automation tools to assert something during your test execution.

-> This pass or fail indication will helps you in your testing process. So this is the very good function in selenium
WebDriver which helps us to find and log issue.

-> Use hard assertions in selenium webdriver only when you wants to skip remaining test execution on assertion fail. If you wants to continue execution on assertion failure, you can use TestNg soft assertions.



Difference between Assertion and Verification

Verification and assertion are not euqal. Both are different.

-> Verification is used to verify the result.

-> Assertion also verify the result. If result is not our expected means its stopped the perticular method execution.



How to use Hard Assertion in Selenium Webdriver

-> We can use pre defined class(Assert) for Hard Assertion. We no need to create object.

-> We use Hard Assertion in Selenium Webdriver like below

Assert.assertEquals(actual, expected)
Assert.assertNotEquals(actual, expected, message)
Assert..assertTrue(condition)
Assert.assertFalse(condition)
Assert.assertNull(object)
Assert.assertNotNull(object)



=========================================================\



Soft Assertion 



-> The hard assertion used to mark method as fail if assertion condition get false and the remaining statements inside the method will be aborted.

-> But the soft Assertion used to mark method as fail if assertion condition get false and the remaining statements inside the method will be continue the process. And also its reports them the Assertion get failure.



How to use Soft Assertion in Selenium Webdriver


-> If you will use soft assertion then your test execution will remain continue even If any assertion fails.

-> The Assertion failure will be reported in the report, so that we can verify at the end of the execution.

-> We can use soft assertion when we have multiple assertions need to be run in same method and execute all assertion even if any one of them fails in between.

-> To use testng soft assertion, you have to use testng SoftAssert class. Its declaration like below,

SoftAssert S_assert = new SoftAssert()

The we can use above object (S_assert) for using same methods which using in Hard Assertion like

S_assert.assertEquals(actual, expected)
S_assert.assertNotEquals(actual, expected, message)
S_assert.assertTrue(condition)
S_assert.assertFalse(condition)
S_assert.assertNull(object)
S_assert.assertNotNull(object)


The above methods used for Soft assertion in Selenium webdriver


This SoftAssert class is not throw the exception if assertion failure and recording failure. 

2 comments: