top of page
divyakeerthivasan9

How to Use TestNg Assertions in Selenium

Imagine you are going on a treasure hunt, and you have a map that tells you where all the hidden treasures are. You must find those treasures and see whether they are located exactly as per the map. Now imagine instead of searching by yourself, you have a robot named Selenium who will find all the treasures for you. But how do you ensure Selenium is finding all the treasures correctly? That’s what assertions do.


What is Assertion:

Assertions in Selenium are used to verify if a given condition is true or false. It acts as a checkpoint to validate the behavior of a web application during testing. If an assertion fails, it indicates a bug or issue in the application. Mainly assertion maintains the integrity of your tests by ensuring the expected outcomes match the actual results. Assertions in Selenium can be handled by pre-defined methods of TestNG frameworks.


Types of Assertions in Selenium:

In Selenium, there are two types of assertions, and it can be categorized depending on how it behaves after a condition is pass or fail. The two types of assertions are

  • Hard Assertions

  • Soft Assertions

Let’s explore more about these two types of assertions


Hard Assertions:

Hard Assertions also known as simple assertions are those asserts that throws an exception immediately and stops the test execution when an assert statement fails, and subsequent assert statements are therefore not validated. It is considered hard because they stop the test execution if the expected outcome does not match the actual results. There are various methods of Hard Assertions, will see one by one below:


  •  assertEquals(): 

This method is used to compare the expected outcome with the actual outcome in the Selenium WebDriver. Whenever the expected and actual outcome are same the assertion passes with no exception. But if the actual and expected outcome are not same then the assert fails with an exception and the test is marked as failed. It takes 2 arguments and can compare string, integer, Boolean etc.


Syntax: Assert.assertEquals(actual,expected)


In this example, the assertion checks for the title of the page using assertEquals(). The tittle matches with expected title then the assertion is passed.

 

Console Output:


  • assertNotEquals(): 

This method is used to check if the expected value is not equal to the actual value. If the values are equal, then the assertion fails, and test case is marked as failed.


Syntax: Assert.assertNotEquals(actual,expected)


In this example, the actual title doesn't match the expected title and this is asserted using assertNotEquals() and the assertion is passed.

Console Output:

  • assertTrue(): 

This method is used to verify a condition that should be true. If the condition is true, then the test case passes else the assertion fails and the test case is marked as failed.


Syntax: Assert.assertTrue(condition)


In this example, the assertTrue() checks if the boolean result is true . Since the result is true the assertion is passed.


Console Output:


  • assertFalse():

 This method is used to verify a condition that should be false. If the condition is false, then the test case passes else the assertion fails and test case is marked as failed.


Syntax: Assert.assertFalse(condition)


In this example, assertFalse() checks if the given condition is false. The condition is found true, the assertion is failed and it throws an java exception.


Console Output:


  • assertNull(): 

This method is used to check that a value is null. If the value is null, then the test case passes. If the value is not null, then the assertion fails, and test case is marked as failed.


Syntax: Assert.assertNull(object)


In this example, assertNull() checks if the object is null. Since the object is not null the assertion is failed and then throws an exception .


Console Output:


  • assertNotNull(): 

This method is used to check that a value is not null. If the value is not null, then the test case passes. If the value is null, then the assertion fails, and the test case is marked as failed.


Syntax: Assert.assertNotNull(object)


In this example, assertNotNull() checks if the object is not null. The object is not null, the assertion is passed.

 

Console Output:


Soft Assertions:

Soft Assertions are opposite of hard assertions, they do not throw an exception when an assertion fails and continue with the next step after the assert statement. Here all the failed assertions are reported at the end of the test. Soft assertions are considered soft because they do not stop the test execution if an assertion fails, and the test continues to execute.


  • assertAll():

TestNG provides the class ‘SoftAssert’ to implement soft assertions. The method assertAll() is called to throw all exceptions caught during execution. When softAssert is used, it performs assertion and if an exception is found, its not thrown immediately, rather than it continues until we call the method assertAll() to throw all exceptions caught. Soft Assert supports all the methods used by Hard Asserts.


 


In this example, we can see that even though the assertion is failed , the execution didn't stop there but continued to execute the next line of code, likewise Assertions 1, Assertions2, Assertions3, Assertions4 was printed. The exception was thrown only after assertAll() was called.


Console Output:


Conclusion:

If you need all the steps of a test case to execute even after an assertion fails, and you also want to report assertion exceptions, then go for soft assertions. Soft assertions are good practice and an effective way of handling test execution, as they allow you to gather all failures and analyze them at the end of the test method.


On the other hand, if you want your test case execution to proceed only after an assertion passes, then use hard assertions. Hard assertions ensure that critical conditions are met before continuing with the test, maintaining the integrity and accuracy of your test results.


By understanding the appropriate scenarios for using soft and hard assertions, you can enhance the robustness and reliability of your automated tests.



Happy Learning!


 

 

 

 

52 views

Recent Posts

See All
bottom of page