Assert in Selenium

Assert is to compare the expected with actual.

Assert.assertEquals:
Fails i.e if expected != actual, then test execution stops with exception: java.lang.AssertionError
Else, Program continues normal flow after checking the Assert

Assert.assertTrue: is to evaluate a condition

Usage:
Best example is login, if logged then continue else stop executing rest of the test case.
To check if a table exist or not, if not present, then no need to loop through the content..

Let’s see the implementation of Assert.


Assert.assertEquals statement:

OutPut:

To avoid skipping the rest of code if Assert fails, then catch the exception and fail the testcase, then continue to the next test case (mostly in framework)
Example of handling the assertionError using try catch block
Let’s look into a +ve scenario, where Assert passes….

OutPut:

Assert.assertTrue

OutPut:

 

Content posted is based on learning or working experience, please leave comments if anything needs to be added or updated, discuss your queries on our facebook:qavalidation.com, Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: