Blogs

Get useful information on apps testing and development

Guide To TestNG Assertions in Selenium Based Test Automation

We all know that testing is a major part of SDLC which can be performed either manually or in an automated manner. No matter which testing type we adopt, it is important to know where exactly we are getting application blockers while testing. Knowing application blockers becomes a bit easy while testing an application manually as a human touch is involved in it. 

However, when testing an application via automation, we should explicitly adopt a strategy where we can validate whether the expected results meet the actual results or not.

This is where Assertions in automation come into the picture. With the help of assertions, the test execution is expected to throw an exception or halt the execution when the expected condition is not met. Thus, assertions play a very significant role for taking relevant steps when actual results are different from expected results.

 

What are Assertions in TestNG?

 

Irrespective of any programming language, every test automation framework like TestNG, JUnit, NUnit, Nightwatch etc offers a mechanism of assertions for validating the end results of a test scenario. In a test automation framework based on Selenium, the testng assertions would be the primary source of highlighting whether the automated test case is passed or failed.

TestNG provides an Assert class that has multiple methods to raise asserts. To use TestNG assertions, it is important to import required package in your java class : org.testng.Assert

Syntax of Assertion in TestNG :

Below is the generic syntax of testng assertion:

 

Assert.methodName(actual, expected);

  • Assert : This is the class inbuilt in TestNG framework
  • methodName : This is the name of the Assert class method
  • actual : This is the first parameter of the assert method in which the value is passed that the user gets from application under test
  • expected : This is the second parameter of the assert method in which the user passes the expected value 

Let’s have a quick look at the real time example where testng assertions play an important role. Considering the example of login page as login is a common module on which other test cases of any application are highly dependent. Using assertion in testng to validate the login scenario, below will be the steps :

  1. Open the login page
  2. Enter username and password
  3. Click submit
  4. Assert the title of landing page after logging into the system

In the above scenario, the assertion would be applied on the title of the landing page i.e. the page that comes after successfully logging into the application. With the help of Selenium, you can fetch the title of the current page after logging in and apply testng assert to validate if the fetched title matches the expected title that is hardcoded in the test script.

 

Types of Assertions in TestNG

 

There are two types of assertions in testng:

  1. Hard Assertion – Whenever a hard assertion is applied and an assertion statement fails, the assertion in testng throws an exception immediately and terminates the further execution of the same test case and simply continues with the execution of the next test case in the test suite. As soon as the hard assertion condition fails, the test case is marked as failed. 

Example of testng hard assertion using Selenium: 

 

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;

public class TestLogin {

WebDriver driver;

    @BeforeTest
    public void setup() {

  WebDriverManager.chromedriver().setup();
  driver = new ChromeDriver();
  driver.manage().window().maximize();

  driver.get("https://www.pcloudy.com/");
    }

    @Test(priority=0)
    public void testPCloudyLogin(){

  WebElement loginHeader = driver.findElement(By.xpath("//a[text()='Login']"));
  loginHeader.click();
 
  WebElement username = driver.findElement(By.id("userId"));
  username.sendKeys("ramit.dhamija@gmail.com");
  WebElement password = driver.findElement(By.name("password"));
  password.sendKeys("ramit9876");
  WebElement loginButton = driver.findElement(By.id("loginSubmitBtn"));
  loginButton.click();

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 
    String expectedTitle = "Mobile App Testing, Continuous Testing Cloud, Mobile Testing Tools | pCloudy";
    String actualTitle = driver.getTitle();

    Assert.assertEquals(actualTitle,expectedTitle, "pCloudy Login Test Failed");
    }
   
    @AfterTest
    public void tearDown() {
 
  if(driver!=null)
  {
  driver.quit();
  }
    }

}

  1. Soft Assertions – These are opposite to hard assertions in which testng continues to the next step of the test case even if the assertion condition fails. 

To implement soft assertion in testng, we use a SoftAssert class and it’s method assertAll() to throw all the exceptions collected during the test case execution. The soft assert basically performs assertion and if a condition fails to meet, it doesn’t throw an exception immediately, instead it continues with the next statement of the same test case until the method assertAll() gets called to throw all the caught exceptions. 

Test script to soft assert the previously discussed login test case:

 

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

import io.github.bonigarcia.wdm.WebDriverManager;

public class TestLogin {

WebDriver driver;
SoftAssert softassert;

    @BeforeTest
    public void setup() {

  WebDriverManager.chromedriver().setup();
  driver = new ChromeDriver();
  softassert = new SoftAssert();
  driver.manage().window().maximize();

  driver.get("https://www.pcloudy.com/");
    }

    @Test(priority=0)
    public void testPCloudyLogin(){

  WebElement loginHeader = driver.findElement(By.xpath("//a[text()='Login']"));
  loginHeader.click();
 
  WebElement username = driver.findElement(By.id("userId"));
  username.sendKeys("ramit.dhamija@gmail.com");
  WebElement password = driver.findElement(By.name("password"));
  password.sendKeys("ramit9876");
  WebElement loginButton = driver.findElement(By.id("loginSubmitBtn"));
  loginButton.click();

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 
    String expectedTitle = "Mobile App Testing, Continuous Testing Cloud, Mobile Testing Tools | pCloudy";
    String actualTitle = driver.getTitle();

    softassert.assertEquals(actualTitle,expectedTitle, "pCloudy Login Test Failed");
    System.out.println("Soft Assertion statement is executed");
 
    softassert.assertAll();
    }
   
    @AfterTest
    public void tearDown() {
 
  if(driver!=null)
  {
  driver.quit();
  }
    }

}

 

When to use Hard and Soft Assertion?

 

As we now know about the hard and soft assertions, let’s discuss this further in a differential way:

 

 
Hard Assertions Soft Assertions
Use case: Terminates the test case execution with exception as soon as the assertion condition doesn’t meet. Use case: Validates all the assertion conditions, collects exceptions in case the assertion condition doesn’t meet and throws all exceptions when assertAll() method is called.
When to use: The scenario in which hard assertion is used best would be the login test scenario where if the login test fails, the test case execution must be terminated with an exception as there is no point of moving further without logging into the system. When to use: Soft Assertion is best used in cases where the test statements of a test case are not dependent on each other. For example, if you are validating a form in which there are multiple fields to be validated, hence it is recommended to soft assert all the fields and then call assertAll() to throw all exceptions at the end of the test case.

TestNG Assert Methods

 

Probably all the testng assert methods work in the same way to validate the test methods. However, different assert methods can accept different parameters, hence, assertion in testng have to be chosen wisely according to the requirement as testng assertions are the one that provide a final result of the test case. 

Below we would be discussing most of the commonly used assertions in testng framework:

  1. Assert.assertEqual(String actual, String expected): This assertion method accepts two parameters i.e. the actual value and expected value to validate if the actual string is equal to the expected string or not. The assertion exception is thrown if both the strings are not equal.

  1. Assert.assertEqual(String actual, String expected, String message): This assertion method is similar to the assertion method discussed above, the only difference is that this method can accept one more string parameter as a message. In case the assertion condition is not met, the assertion error is thrown along with a message passed here.

  1. Assert.assertEquals(boolean actual, boolean expected): This assertion method accepts two boolean values and validates if both are equal or not.

  1. Assert.assertTrue(condition): This assertion method is used to assert whether the condition passed in a parameter returns true or not. If the condition returns false, the assertion error is thrown. 

  1. Assert.assertTrue(condition, message): This assertion method is similar to the assertion method discussed in previous one, the only difference is that this method can accept one more string parameter as a message. In case the assertion condition is passed as false, the assertion error is thrown along with a message passed here.

  1. Assert.assertFalse(condition): This assertion method is used to assert whether the condition passed in a parameter returns false or not. If the condition returns true, the assertion error is thrown. 

  1. Assert.assertFalse(condition, message): This assertion method is similar to the assertion method discussed in the previous one, the only difference is that this method can accept one more string parameter as a message. In case the assertion condition is passed as true, the assertion error is thrown along with a message passed here.

  1. Assert.assertNull(condition): This assertion method is used to assert whether the condition passed in a parameter returns null or not. If the condition doesn’t return null, the assertion error is thrown. 

  1. Assert.assertNotNull(condition): This assertion method is used to assert whether the condition passed in a parameter returns value except null or not. If the condition returns null, the assertion error is thrown. 

 

Best practices for using TestNG in Selenium automation:



Structure Test Suites: Organize your test suites based on logical groupings, such as functional areas or test priorities. Divide tests into smaller, focused suites rather than having one large suite. This improves maintainability and allows for easier test management.

Use Descriptive Test Names: Give meaningful names to your test methods using descriptive language. This makes it easier to understand the purpose of each test and quickly identify failures or issues.

Leverage TestNG Annotations: Utilize TestNG annotations to control the test execution flow and define preconditions and postconditions. Annotations like @BeforeMethod, @AfterMethod, @BeforeClass, and @AfterClass help in setting up and tearing down test environments effectively.

Group and Prioritize Tests: Use TestNG’s @Test annotation’s groups and priority attributes to categorize and prioritize your tests. This allows you to run specific groups of tests based on requirements or execute critical tests first.

Data-Driven Testing: Employ TestNG’s data provider feature for data-driven testing. Separate test data from the test logic by fetching data from external sources like Excel, CSV files, or databases. This enhances test coverage and allows for easy maintenance of test data.

Test Dependencies: Utilize TestNG’s dependsOnMethods or dependsOnGroups attributes to manage dependencies between test methods or groups. This ensures that tests run in a specific order, facilitating smoother test execution.

Parameterized Tests: Use TestNG’s @Parameters annotation to create parameterized tests. This allows you to execute the same test method with different input values, reducing code duplication and making tests more versatile.

TestNG Assertions: Leverage TestNG’s assertion methods (assertEquals(), assertTrue(), etc.) to validate expected outcomes in your tests. Use descriptive messages in assertions to provide clear failure information.

TestNG Listeners: Implement TestNG listeners to enhance reporting and add custom functionalities. TestNG provides various listeners, such as ITestListener and ISuiteListener, which allow you to capture test results, take screenshots, log additional information, or perform custom actions during test execution.
Parallel Test Execution: Utilize TestNG’s parallel execution feature to run tests in parallel, leveraging the available resources and reducing overall test execution time. Configure parallelism at the test method, class, or suite level based on the system capabilities and test requirements.

TestNG Reporters: Extend TestNG reports by implementing custom reporters. TestNG provides options like IReporter and ITestReporter that allow you to generate customized test reports with additional information or integrate with other reporting frameworks.

Continuous Integration: Integrate TestNG with your preferred CI/CD tools like Jenkins or Bamboo for seamless automation and continuous testing. Configure your CI/CD pipeline to trigger TestNG test executions automatically whenever new code changes are pushed.

Maintain Test Environment Independence: Design your tests to be independent of specific test environments. Avoid hard-coding environment-specific details, such as URLs or credentials, by utilizing configuration files, environment variables, or test data providers.

Regular Test Maintenance: Maintain your test suites by reviewing and updating them periodically. Remove redundant or obsolete tests, update assertions as per application changes, and ensure tests are kept up to date with the evolving requirements.

 

Conclusion

 

Assertions are the core part of any test method, hence understanding the use case of assertion in testng is very important to develop an efficient and robust test automation suite.  The above discussed assertions of testng are most commonly used to validate test methods. There are many more assertions in testng which you can find at testng assertions official document

 


Automation Practices

Shivani Sinha

She is a Product Marketer with over 9 years of diversified experience across content, branding, and product marketing. Her experience ranges from global brands to nimble startups. She is a custodian of Brand & Content, telling stories about the brand that delights customers and provides compelling business value.

Recent Posts