Sitemap
Suganya Nesappan

Suganya Nesappan

Assertions In Selenium

2 min readSep 22, 2023

--

Asserts are commonly used in Selenium for verifying or validating applications. Assertion determines the state of the application whether it is the same as what we are expecting or not.

Get Suganya Nesappan’s stories in your inbox

Join Medium for free to get updates from this writer.

A test case is considered to be passed only if all the assertions have been met. Assertions in Selenium can be handled by pre-defined methods which are available in Junit and TestNG frameworks.

Types Of Assertions In Selenium

  • Hard Assertion — Hard Assertion is an Assertion that throws the AssertException when the test case fails. In the case of Hard Assertion, you can handle the error by using a catch block like a Java exception. Suppose we have two test cases in a suite. The first test case in a suite has an assertion that fails, and if we want to run the second case in a suit, then we need to handle the assertion error. A Hard Assertion contains the following methods:
  • AssertEquals
  • AssertNotEquals
  • AssertTrue
  • AssertFalse
  • AssertNull
  • AssertNotNull
Press enter or click to view image in full size
Assertion exception when it fails
  • Soft Assertion — In this assertion, the test execution will continue till the end of the test case even if the assert condition is not met. Soft Asserts will report the errors at the end of the test. Here, tests will not be aborted if any condition is not met. Testers need to invoke the assertAll() method to view the results.

Conclusion

As discussed above, we have seen various Assertion methods that can be used for the test case validation and ensure whether the test has a Pass or Fail status.

Thank you

--

--