Using Assertions in Selenium Java with TestNG

 Assertions play a crucial role in test automation by verifying whether the expected outcomes match the actual results. In Selenium Java, TestNG is a popular testing framework that offers a robust set of assertions to validate the behavior of web applications. Proper use of assertions helps in identifying bugs early and ensures that the application under test behaves as intended. In this blog, we’ll explore how to use assertions in Selenium Java with TestNG effectively.


What Are Assertions?

Assertions are statements in test scripts that check if a given condition is true or false. If the condition evaluates to true, the test continues. If it evaluates to false, the test fails at that point.

Assertions help answer the critical question: “Did the application behave as expected?”

For example, after logging into a website, you might assert whether the user’s name appears on the home page.


Types of Assertions in TestNG

TestNG provides two main types of assertions:

Hard Assertions (from org.testng.Assert)

Soft Assertions (from org.testng.asserts.SoftAssert)


1. Hard Assertions

Hard assertions immediately halt test execution if the assertion fails.

Example:

java


import org.testng.Assert;

import org.testng.annotations.Test;


public class LoginTest {


    @Test

    public void verifyLoginTitle() {

        String actualTitle = "Dashboard";

        String expectedTitle = "Dashboard";

        Assert.assertEquals(actualTitle, expectedTitle, "Title does not match!");

    }

}


Common hard assertion methods:

Assert.assertEquals(actual, expected)

Assert.assertTrue(condition)

Assert.assertFalse(condition)

Assert.assertNotNull(object)

Assert.fail("Forcefully fail the test")

These are mostly used when the next steps depend on the current validation.


2. Soft Assertions

Soft assertions allow the test to continue even after an assertion fails. All assertions are collected and reported at the end of the test using assertAll().


Example:

java


import org.testng.annotations.Test;

import org.testng.asserts.SoftAssert;


public class ProfileTest {


    @Test

    public void verifyProfileDetails() {

        SoftAssert softAssert = new SoftAssert();


        softAssert.assertEquals("John", "John", "Name mismatch");

        softAssert.assertTrue(25 > 20, "Age condition failed");

        softAssert.assertEquals("USA", "Canada", "Country mismatch");


        System.out.println("Test continues despite failed assertion...");


        softAssert.assertAll(); // Collates results and marks test as failed if any assertion failed

    }

}

Best Practice: Use soft assertions when you want to verify multiple conditions within a single test without halting execution.


Using Assertions with Selenium

When integrated with Selenium WebDriver, assertions validate web elements, page titles, URLs, etc.


Example:

java


WebDriver driver = new ChromeDriver();

driver.get("https://example.com");


String actualTitle = driver.getTitle();

Assert.assertEquals(actualTitle, "Example Domain", "Title mismatch");


WebElement heading = driver.findElement(By.tagName("h1"));

Assert.assertTrue(heading.isDisplayed(), "Heading is not visible");


driver.quit();


Best Practices for Assertions

Use clear and meaningful messages in assertions to ease debugging.

Use hard assertions when test execution should stop on failure.

Use soft assertions to validate multiple fields without stopping execution.

Avoid putting multiple hard assertions in one test without dependencies.


Conclusion

Assertions are fundamental to validating the accuracy of your tests. With TestNG, Selenium Java testers have access to powerful hard and soft assertions to ensure comprehensive test coverage. By using assertions wisely, you can build robust test scripts that catch issues early and contribute to more reliable software releases.


 Learn Selenium with Java Training

Read More: Automating Login Functionality Using Selenium Java

Visit Our IHUB Talent Institute Hyderabad
Get Direction 

Comments

Popular posts from this blog

How to Use Tosca's Test Configuration Parameters

Installing Java and Eclipse IDE for Selenium Automation

How Flutter Works Behind the Scenes