3

Guide to TestNG Assertions in Selenium Based Test Automation

 1 year ago
source link: https://dzone.com/articles/guide-to-testng-assertions-in-selenium-based-test
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Guide to TestNG Assertions in Selenium Based Test Automation

With the help of assertions, the test execution is expected to throw an exception or halt the execution when the common condition is not met.

Join the DZone community and get the full member experience.

Join For Free

We all know that testing is a significant part of SDLC, which can be performed manually or automated. No matter which testing type we adopt, knowing where we are getting application blockers while testing is essential. Learning application blockers becomes a bit easy while manually testing an application as human touch is involved.

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 common condition is not met. Thus, assertions play a significant role in taking appropriate 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 results of a test scenario. For example, 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 essential to import the 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 into the 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 the application under the 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 essential role. Considering the example of a login page as login is a standard 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 the landing page after logging into the system

In the above scenario, the assertion would be applied to the landing page's title, i.e., the page that comes after successfully logging into the application. With the help of Selenium, you can fetch the current page's title after logging in and applying 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 problematic assertion is applied, and an assertion statement fails, the report in testng throws an exception immediately, terminates the further execution of the same test case, and continues with the execution of the following test case in the test suite. As soon as the problematic assertion condition fails, the test case is marked as failed.

Example of testng problematic 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("[email protected]");
  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 complex 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 its method assertAll() to throw all the exceptions collected during the test case execution. The soft assert performs assertion. If a condition fails to meet, it doesn’t throw an exception immediately; instead, it continues with the following 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("[email protected]");
  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:

TestNG Assert Methods

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

Below we will 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 exact string is equal to the expected string. 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. If 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 the previous one; the only difference is that this method can accept one more string parameter as a message. If 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 asserts 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. If 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 asserts 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 asserts whether the condition passed in a parameter returns a 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 testing, which you can find at testng assertions official document.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK