Mastering Assertions in Selenium Using TestNG

Meenakshi Agarwal
By
Meenakshi Agarwal
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large...
14 Min Read
How to Use TestNG Assertions.

Assertions guard your automation tests against unexpected behavior. In this tutorial, you’ll learn how to use assertions in Selenium with TestNG, including various methods to assert conditions effectively.

How to Use TestNG Assertions 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. Assertions can let us do that within a single line of code.

What is an Assertion in Selenium?

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 errors in 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.

For a step-by-step guide, read How to Create a TestNG Project in Eclipse.

Types of Assertions in TestNG

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

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.

Scenario(1)

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

package com.techbeamers.hardassertion;

import org.testng.Assert;
import org.testng.annotations.Test;

public class HardAssertion {

    String className = "HardAssertion";

    @Test
    void test_UsingHardAssertion() {
        Assert.assertTrue(true == true);
        Assert.assertEquals("HardAssertion", "HardAssertion");
        Assert.assertEquals(className, "HardAssertion");
        System.out.println("Successfully passed!");
    }
}

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

...
Successfully passed!
[Utils] Attempting to create C:\workspace\work\selenium\TestNGAssertions\test-output\Default suite\Default test.xml
[Utils]   Directory C:\workspace\work\selenium\TestNGAssertions\test-output\Default suite exists: true
PASSED: test_UsingHardAssertion

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================

Scenario(2)

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

package com.techbeamers.hardassertion;

import org.testng.Assert;
import org.testng.annotations.Test;

public class HardAssertion {

    String className = "HardAssertion";

    @Test
    void test_UsingHardAssertion() {
        Assert.assertTrue(true == true);
        Assert.assertEquals("HardAssert", "HardAssertion");
        Assert.assertEquals(className, "HardAssertion");
        System.out.println("Successfully passed!");
    }
}

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

...
FAILED: test_UsingHardAssertion
java.lang.AssertionError: expected [HardAssertion] but found [HardAssert]
...

Soft Assertions

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.

First scenario

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

package com.techbeamers.softassertion;

import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class SoftAssertion {

    SoftAssert softAssert = new SoftAssert();
    String className = "SoftAssertion";

    @Test
    void test_UsingSoftAssertion() {
        softAssert.assertTrue(true == true);
        softAssert.assertEquals("SoftAssert", "SoftAssertion");
        softAssert.assertEquals(className, "SoftAssertion");
        System.out.println("Last statement gets executed!");
        softAssert.assertAll();
    }
}

You can cross-check from the output below that the message “The Last statement gets executed!” appeared there even after one of the asserts calls failed.

...
Last statement gets executed!
[Utils] Attempting to create C:\workspace\work\selenium\TestNGAssertions\test-output\Default suite\Default test.xml
[Utils]   Directory C:\workspace\work\selenium\TestNGAssertions\test-output\Default suite exists: true
FAILED: test_UsingSoftAssertion
java.lang.AssertionError: The following asserts failed:
	expected [SoftAssertion] but found [SoftAssert]
...

Second scenario

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 that occurs when one test failure makes other tests fail. It happens due to the use of the same assert object that evaluates all occurrences of assert methods despite being in different cases.

package com.techbeamers.softassertion;

import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class SoftAssertIssue {

    SoftAssert softAssert = new SoftAssert();
    String className = "SoftAssertion";

    @Test
    void test1_UsingSoftAssertion() {
        softAssert.assertTrue(true == true);
        softAssert.assertEquals("SoftAssert", "SoftAssertion");
        softAssert.assertEquals(className, "SoftAssertion");
        softAssert.assertAll();
    }

    @Test
    void test2_UsingSoftAssertion() {
        softAssert.assertTrue(true == true);
        softAssert.assertEquals("SoftAssertion", "SoftAssertion");
        softAssert.assertEquals(className, "SoftAssertion");
        softAssert.assertAll();
    }
}

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

FAILED: test1_UsingSoftAssertion
java.lang.AssertionError: The following asserts failed:
	expected [SoftAssertion] but found [SoftAssert]
...
FAILED: test2_UsingSoftAssertion
java.lang.AssertionError: The following asserts failed:
	expected [SoftAssertion] but found [SoftAssert]
...
===============================================
    Default test
    Tests run: 2, Failures: 2, Skips: 0
===============================================

Third scenario

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 instantiate it on the fly.

Please see the updated code below.

package com.techbeamers.softassertion;

import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class SoftAssertSolution {

    SoftAssert softAssert1 = new SoftAssert();
    SoftAssert softAssert2 = new SoftAssert();
    String className = "SoftAssertion";

    @Test
    void test1_UsingSoftAssertion() {
        softAssert1.assertTrue(true == true);
        softAssert1.assertEquals("SoftAssert", "SoftAssertion");
        softAssert1.assertEquals(className, "SoftAssertion");
        softAssert1.assertAll();
    }

    @Test
    void test2_UsingSoftAssertion() {
        softAssert2.assertTrue(true == true);
        softAssert2.assertEquals("SoftAssertion", "SoftAssertion");
        softAssert2.assertEquals(className, "SoftAssertion");
        softAssert2.assertAll();
    }
}

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

...
PASSED: test2_UsingSoftAssertion
FAILED: test1_UsingSoftAssertion
java.lang.AssertionError: The following asserts failed:
	expected [SoftAssertion] but found [SoftAssert]
...
===============================================
    Default test
    Tests run: 2, Failures: 1, Skips: 0
===============================================

Assert Methods Available in TestNG

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

Sr.MethodsDescription
1.assertEquals(
String actual,
String expected);
1- It accepts 2 string arguments.
2- It’ll check whether both are equal. If not, it’ll not pass the test.
2.assertEquals(
String actual,
String expected,
String message);
1- It accepts 3 string arguments.
2- It’ll test whether both are the same. If not, the test will not pass.
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 not pass 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 the same elements and in 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(
condition);
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(
condition,
message);
1- It assumes one boolean argument and a message.
2- It asserts that the given condition is true.
3- In case of failure, an AssertionError occurs with the message you passed.
7.assertFalse(
condition);
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 will occur with the message you passed.
8.assertFalse(
condition,
message);
1- It assumes one boolean argument and a message.
2- It asserts that the given condition is false.
3- In case of failure, an AssertionError occurs with the message you passed.
TestNG Assert Methods

Practical Examples of Assert in Selenium

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.
    • HomePage.java
    • ChapterFirstPage.java
    • ChapterSecondPage.java
  • This Java file defines the base test case class to provide standard methods.
    • TestBase.java
  • In the below Java file, we write the page object tests to execute.
    • MyTest.java

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.

For maintainable test scripts, learn the Page Object Model (POM) in Selenium.

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

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

Add TestNG Test Methods

We’ll first add Test Methods in ChapterFirstPage.java.

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

Code Sample

// ChapterFirstPage.java
package com.techbeamers.page;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;

public class ChapterFirstPage {

	private WebDriver driver;

	@FindBy(id = "secondajaxbutton")
	WebElement secondajax;

	@FindBy(xpath = "//select[@id='selecttype']")
	WebElement dropdown;

	@FindBy(id = "verifybutton")
	WebElement verifybutton;

	ChapterFirstPage(WebDriver driver) {
		this.driver = driver;
	}

	// Method-1.
	ChapterFirstPage clickSecondAjaxButton() {
		secondajax.click();
		return PageFactory.initElements(driver, ChapterFirstPage.class);
	}


	// Method-2.
	ChapterFirstPage clickSecondAjaxButton1(String data1) {
		System.out.println(data1);
		return PageFactory.initElements(driver, ChapterFirstPage.class);
	}


	// Method-3.
	ChapterFirstPage selectDropDown(String value) {
		new Select(dropdown).selectByVisibleText(value);
		return PageFactory.initElements(driver, ChapterFirstPage.class);
	}


	// Method-4.
	ChapterFirstPage verifyButton() {
		verifybutton.click();
		return PageFactory.initElements(driver, ChapterFirstPage.class);
	}


	// Method-5.
	String getDropDownText() {
		return new Select(dropdown).getFirstSelectedOption().getText();
	}
}

Add Page Object Tests

We’ll now add Page Object Tests in MyTest.java.

  • 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 the TestNG assertions.
  • In each of the cases, there is a different assertion method called to test the condition.
Code Sample
// MyTest.java
package com.techbeamers.scripts;

import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;
import org.testng.annotations.Test;

import com.techbeamers.page.HomePage;
import com.techbeamers.util.TestBase;

public class MyTest extends TestBase {

	// Test-0. (disabled)
	@Test(enabled = false)
	void testPageObject() throws Exception {

		homePage = PageFactory.initElements(driver, HomePage.class);

		driver.get(baseUrl);

		chapterSecond = homePage.clickChapterSecond();
		chapterSecond.clickbut2();
		chapterSecond.clickRandom();
		String data = chapterSecond.getTest();
		homePage = chapterSecond.clickIndex();

		chapterFirstPage = homePage.clickChapterFirst();
		chapterFirstPage.clickSecondAjaxButton();
		chapterFirstPage.clickSecondAjaxButton1(data);
		chapterFirstPage.selectDropDown("Selenium Core");
		chapterFirstPage.verifyButton();
	}

	// Test-1.
	@Test
	void testAssertSuccess() throws Exception {

		homePage = PageFactory.initElements(driver, HomePage.class);

		driver.get(baseUrl);
		Thread.sleep(1000); // Intentional pause.

		chapterFirstPage = homePage.clickChapterFirst();
		Thread.sleep(500); // Intentional pause.

		chapterFirstPage.selectDropDown("Selenium Core");
		Thread.sleep(1000); // Intentional pause.

		Assert.assertEquals("Selenium Core", chapterFirstPage.getDropDownText());
	}

	// Test-2.
	@Test
	void testAssertTrue() throws Exception {

		homePage = PageFactory.initElements(driver, HomePage.class);

		driver.get(baseUrl);
		Thread.sleep(1000); // Intentional pause.

		chapterFirstPage = homePage.clickChapterFirst();
		Thread.sleep(500); // Intentional pause.

		chapterFirstPage.selectDropDown("Selenium Core");
		Thread.sleep(1000); // Intentional pause.

		Assert.assertTrue(chapterFirstPage.getDropDownText().equalsIgnoreCase("Selenium Core"));
	}

	// Test-3.
	@Test
	void testAssertFailure() throws Exception {

		homePage = PageFactory.initElements(driver, HomePage.class);

		driver.get(baseUrl);
		Thread.sleep(1000); // Intentional pause.

		chapterFirstPage = homePage.clickChapterFirst();
		Thread.sleep(500); // Intentional pause.

		chapterFirstPage.selectDropDown("Selenium Core");
		Thread.sleep(1000); // Intentional pause.

		Assert.assertEquals("Selenium RC", chapterFirstPage.getDropDownText());
	}

	// Test-4.
	@Test
	void testAssertFailureWithMessage() throws Exception {

		homePage = PageFactory.initElements(driver, HomePage.class);

		driver.get(baseUrl);
		Thread.sleep(1000); // Intentional pause.

		chapterFirstPage = homePage.clickChapterFirst();
		Thread.sleep(500); // Intentional pause.

		chapterFirstPage.selectDropDown("Selenium Core");
		Thread.sleep(1000); // Intentional pause.

		Assert.assertEquals("Selenium RC", chapterFirstPage.getDropDownText(), "Invalid Component Selected!!!\n\t");
	}
}

Execute POM Tests

Now it’s time to see the project running. We hope you have merged the above code with the 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.

Reports after using assert in Selenium
Verify reports while using TestNG assertions

Key Takeaways from Assertion in Selenium

Today, you learned how to implement TestNG assertions in a Selenium Java project. Since this post was one of our readers’ choices, we tried to bring all the required ingredients into it. Hopefully, it’ll carry its benefits for the other readers as well.

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

Lastly, our site needs your support to remain free. Share this post on social media (Facebook/Twitter) if you gained some knowledge from this tutorial.

Enjoy Testing!

Share This Article
Subscribe
Notify of
guest

0 Comments
Newest
Oldest
Inline Feedbacks
View all comments