Guide To TestNG Assertions in Selenium Based Test Automation — pCloudy

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 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 in 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 test 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 the required package in your java class: org.testing.Assert

Syntax of Assertion in TestNG :

Below is the generic syntax of testing 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:

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(); } } }

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:

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:

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.

Originally published at https://www.pcloudy.com.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
pCloudy

pCloudy is the most powerful cloud-based App Testing Platform. Brand Marketing @ pCloudy (www.pcloudy.com)