How to Use TestNG Assertions to Verify Tests in Selenium

While using Selenium for automated testing of web applications, we need to add validations in our tests to report them as pass or fail. And assertions can let us do that within a single line of code. Here, you’ll learn how to use TestNG assertions and know the different methods to assert conditions.

Assertions give you a way, other than If-Else blocks, to test conditions. They are not only easy to use but also eliminate the chances of making any error in forming test conditions. Hence, it’s always beneficial to use them in Selenium Webdriver projects.

Moreover, in this tutorial, we’ll demonstrate TestNG assertions with the help of a POM framework. We’ll add simple page object tests and call different TestNG methods to assert them.

If you wish to start afresh with Selenium TestNG, then read the below tutorial.

How to Use TestNG Assertions in Selenium.

Table of Index.

How to Use TestNG Assertions in Selenium

How to Use TestNG Assertions.

1- Types of Assertions.

First of all, let us understand what are the different types of assertions available in TestNG and when to use them.

1.1- Hard Assertion.

It is the default assert mechanism built into TestNG’s <org.testng.assert> package. We use it when a test has to stop immediately after the assertion fails.

Here are two scenarios to understand the concept.

Hard Assertion – Scenario(1)

Follow the below code which includes multiple assert calls, all of which get passed and so the test case.

You can see the message “Successfully passed!” getting appeared in the execution output.

Hard Assertion – Scenario(2)

In this scenario, the second assert call fails which leads to the end of the test case.

After the expected string didn’t match the actual string, the execution of test case stopped immediately.

1.2- Soft Assertion.

It is a custom assert mechanism supported by TestNG’s <org.testng.asserts.Softassert> package. We use it when a test has to continue execution even after an assertion fails in the sequence.

Here, we again have two scenarios to explain Soft assertion.

The first scenario.

Follow the below code which creates a Soft assertion object. Then, it includes multiple assert methods followed by a <assertAll()> call. The second assert fails but that doesn’t interrupt the execution. It is because of the <assertAll()> method which lets the other assert calls to complete.

You can cross-check from the output below, that the message <The Last statement gets executed!> appeared there even after one of the assert calls failed.

The second scenario – An issue in using the Soft assertion.

In this example, you can see that there are multiple test cases. They are using the same Soft assertion object. We added it to highlight the issue which occurs when one test failure makes other tests to fail. It happens due to the use of same assert object which evaluates all occurrences of assert methods despite being in different cases.

You can verify from the output below that both cases failed while only the first one had the error.

The second scenario – Right way to use the Soft assertion.

The solution to the previous issue is to create a separate <SoftAssert> object for each test case. You can even use a Factory design pattern to create it on the fly.

Please see the updated code below.

From the output below, you can see now both tests are running without conflicting each other.


2- Get to Know the Assert Methods Available in TestNG.

Let’s now quickly outline the methods that you can call from Selenium code for using TestNG assertions.

Methods Description
1- assertEquals(
String actual,
String expected);
1- It accepts two string arguments.
2- It’ll check whether both are equal, if not it’ll fail the test.
2- assertEquals(
String actual,
String expected,
String message);
1- It accepts three string arguments.
2- It’ll test whether both are same, if not it’ll fail the test.
3- Also, it’ll throw a message that we provide.
3- assertEquals(
boolean actual,
boolean expected);
1- It assumes two boolean arguments.
2- It tests them for equality, if not it’ll fail the test.
4- assertEquals(
java.util.Collection actual,
java.util.Collection expected,
java.lang.String message);
1- It accepts two collection type objects.
2- It checks whether they hold same elements and with the same order.
3- It’ll fail the test if the above condition doesn’t meet.
4- Also, you’ll see the message appear in the report.
5- assertTrue(
1- It accepts one boolean argument.
2- It tests that the given condition is true.
3- If it fails, then an <AssertionError> would occur.
6- assertTrue(
1- It assumes one boolean argument and a message.
2- It asserts that the given condition is true.
3- If it fails, then an <AssertionError> would occur with the message you passed.
7- assertFalse(
1- It accepts one boolean argument.
2- It checks that the given condition is false.
3- If it doesn’t pass, then an <AssertionError> would occur.
8- assertFalse(
1- It assumes one boolean argument and a message.
2- It asserts that the given condition is false.
3- If it fails, then an <AssertionError> would occur with the message you passed.


3- Setup a Demo POM Framework for Using TestNG Assertions.

You’ll need to prepare a simple POM project first to see how to use TestNG assertions. Our demo project will include the following Java files.

  • These Java files will store the element locators and methods.
  • This Java file defines the base test case class to provide common methods.
  • In the below Java file, we write the page object tests to execute.

You can get the source code of the above files from the below post. Open this tutorial and copy/paste the code to your project.

It’s important to mention that we’ll modify these files in the next section. And then, we’ll demonstrate of using the TestNG assertions.


4- Add Page Object Tests and Use TestNG Assertions.

Here, we have given the code for each page object tests. You’ll need to add it to the appropriate files in your project.

4.1- Add Test Methods in <>.

  • From the below code, you can see the last method <String getDropDownText()>.
  • We’ve added it only for the purpose of this post.
  • Later, we’ll call it from the page tests and verify the results using TestNG assertions.
Code Sample.

4.2- Add Page Object Tests in <>.

  • In this module, you can see that we’ve added four new test cases. e.g. <Test-1>, <Test-2>, <Test-3>, and <Test-4>.
  • To verify the test results, we’ll use TestNG assertions.
  • In each of the case, there is a different assertion method call to test the condition.
Code Sample.

4.3- Run POM Tests.

Now it’s time to see the project running. We hope you have merged the above code with POM framework source code. And we are all set to run it. Though, we indeed are and here is a screenshot of the execution that we ran at our end. You should also see a similar report on your side.

Verify reports while using TestNG assertions

Verify reports while using TestNG assertions.

Summary – How to Use TestNG Assertions in Selenium.

Since this post was one of our reader’s choice, so we tried to bring in all the required ingredients into it. Hopefully, it’ll carry its benefits for the other readers as well.

If you had any questions on using TestNG assertions in your projects, then please speak about it. And we’ll try to address it here.




One Response

  1. Yogiraj Reply