Enterprise Java

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 AssertionsSoft 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.
  2. 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.
  3. Assert.assertEquals(boolean actual, boolean expected): This assertion method accepts two boolean values and validates if both are equal or not.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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.
  9. 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.

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

Published on Java Code Geeks with permission by Ramit Dhamija, partner at our JCG program. See the original article here: Guide To TestNG Assertions in Selenium Based Test Automation

Opinions expressed by Java Code Geeks contributors are their own.

Ramit Dhamija

Working as an Automation Expert at LambdaTest and has recently started the professional journey. Excels in Java test automation.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button