Assertion in Selenium

In this section, we will discuss on the Assertion in Selenium. Generally, Selenium commands has 3 types.




Action generally deals with the state of the application like “click the menu”, “Choose the option” etc. Once action fails, the current execution of test will stop.

Accessors examine the state of the application whereas Assertions verifies the state of application.

Again, Assertions can be classified in to 3  types




Now let’s understand the difference between assert, verify and waitFor. Once assert fails, the test is aborted. But if verify fails, test will continue execution without abort and this will log the failure.

In case of waitFor, this will wait for the certain condition to be satisfied within the specified timeout. This will pause the test until the timeout. Once execution time exceed the specified timeout then test will fail and log the failure. If the condition become true immediately, test will succeed immediately also.

 When we use the Assertion in Selenium using TestNG then we can classify the Assertion in 2 types.

            Hard Assertion

            Soft Assertion

Hard Assertion

After the test fail, a hard assert throws AssertExceotion immediately and test is marked as fail status. But test will continue to next @Test annotation. In the case of Hard Assertion, you can handle the error by using a catch block like a java exception. This can be of following types.







Let’s discuss the details of above hard assertions.


This is generally used for comparing the actual and expected values in Selenium WebDriver. Assertion passes no exception if the actual and expected values are same. If the actual and expected values are not same then this will trigger and exception with failed status and continues to the next test case if any.

Below is the example of assertEquals declaration

Assert.assertEquals(Actual. Expected)


assertNotEquals is just an opposite of assertEquals. That means whenever the actual and expected results are matched then only assetNotEquals() will trigger and exception and failed the test case. There will not be any exception triggered if the both results are not equals and continue the test with out any failure

Below is the syntax for the assertNotEquals.



The use of assertTrue is same as previous one. But this will be used only if we will be working with Boolean conditions. If applied condition passed, assertTrue will return True. This will trigger an exception if this will fail and continue to the next test by skipping the current one.

Below is the syntax of assertTrue.



assertFalse is just an opposite of assertTrue. That means, if applied condition is failed then only this will return true and will trigger an exception

Below is the syntax of assertFalse



This assertion checks whether the object is null or not. If the object is null then this will trigger an exception resulting in aborting the test. The syntax is as below.



assertNotNull is just an opposite to the assertNull. If an object has some value then this will trigger an exception and abort the test. Below is the syntax of assertNotNull.


Soft Assertion

In the previous section we have gone through the hard assertion. Is this a good approach for the assertion in Selenium checks?

Yes, but nor always. The main disadvantage of the hard assertion is this terminated the current test and continues to the next test.

What if you want to run the current test without termination but log this failure?

Yes. This can be achieved by soft assertion.

For Example:

To use a soft assertion, we must include its corresponding class (as SoftAssert()) in the script. This class prevents the execution to throw any exception (of assertion). Also, the most important context is, now the failed assertions will be reported report and not making the test to abort anywhere.


    public void hardAssertion(){
    	System.out.println("This line is executed because assertEquals "
    			+ "passed as both the strings are same");
    	System.out.println("Since the object under assertion"
    			+ " is not null, the assertion will fail. "
    			+ "This line will not be executed");
    public void softAssertion(){
    	System.out.println("We are using Soft assertion in this method,"
    			+ " so this line of code will also be executed even if "
    			+ "the assetion fails.Wherever we want to execute full "
    			+ "testcase/method, we should use SoftAssertion");

Leave a Reply

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