Using TestNG Assertions to Verify Tests in POM Framework

Assertions are the best replacement of If-Else blocks for testing conditions. They are not only easy to use but also reduce the chances to make any error while writing conditions. Hence, it’s always beneficial to use them in Selenium Webdriver projects. In this tutorial, we’ll highlight different ways of using TestNG assertions to verify Selenium tests and conditions in a POM framework. If you wish to learn about POM, please follow this link to read the page object model tutorial.

Also, we’ve made this tutorial targeted for TestNG assertions and tried to keep it short. We did so because we wanted our focus on teaching you the best ways of using TestNG assertions in your projects. But those who are beginners in using TestNG would love to go through the step by step instructions for creating a basic TestNG project in Eclipse.

Consequently, you might consider this post as an extension to our earlier tutorial on the Page object model framework. In this POM tutorial, we didn’t use the TestNG assertions but developed a basic page object model application. So it became a natural candidate where we could show how to use the TestNG assertions to validate the page object model tests.

Using TestNG Assertions to Verify Tests in a POM Framework.

1- Introduction to various methods for using TestNG assertions.

First of all, let’s 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- Assert.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- Assert.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- Assert.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- Assert.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.

2- Brief about the Page Object Model test framework.

Now, let’s get a brief summary of the page object model framework that we’ll play with in this tutorial. Please note that we are re-using its code in this tutorial. So you must go through the source code given in this tutorial.

In this project, we’d used the following Java files for executing the POM tests. It’s important to mention them here as we are going to modify the same project files next. And then, we’ll demonstrate of using the TestNG assertions.

1- Following are the Java files to hold the element locators and their methods.


2- This Java file defines the base test case class to provide common methods.


3- In the below Java file, we write the test cases to execute.


From the above list, we’ll only add the assertions related changes to the highlighted files.


3- Implementing TestNG assertions in Selenium Page Object Model framework.

Finally, we’ve come to the point where we’ll make the code changes in the above mentioned Java files. See the below list to review the changes.

3.1- Summary of Code Changes and Updated Test Cases.


In this file, we’ve added the following method which returns the text selected in a dropdown. It will help in using TestNG assertions in our test cases. Code Sample.

3.1.2- MyTest.Java.

This file contains our page object test cases. For demonstration, we’ll add four new test cases covering the TestNG assertions. Here is the sample code for a single test case. You can check full code in the next section. Code Sample.


3.2- Updated <> to get the selected option from a dropdown.

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

3.2.1- Code Sample.

3.3- Updated <> with four new test cases.

1- In this module, you can see that we added four new test cases. e.g. <Test-1>, <Test-2>, <Test-3>, and <Test-4>.
2- And to verify their results, we are using TestNG assertions.
3- In each of the case, there is a different assertion method call to test the condition.

3.3.1- Code Sample.

3.3- Running the Page Object Application.

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.


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.


All the Best,


One Response

  1. Yogiraj Reply

Leave a Reply