How To Use Assertions In TestNG With Selenium

What are Assertions in TestNG?

Every test automation framework (e.g., TestNG, JUnit, PyTest, etc.) provides a mechanism to raise asserts. Hence, the generic concept of Asserts and Verify in Selenium is synonymous across different automation frameworks.

  • Expected Title: Most Powerful Cross Browser Testing Tool Online | LambdaTest
  • Test case result: Passed, as fetched title matches the expected title
  • Expected Title: Most Powerful Cross Browser Testing Tool Online
  • Test case result: Failed, as fetched title does not match with the expected title

Syntax of TestNG assertions

The Assert package in TestNG provides methods(or options) to raise assertions. Shown below is the generic syntax of TestNG assertions:

Assert.methodName(actual, expected);
  • methodName is the name of the method that can be used for implementing TestNG assertions
  • actual is the first parameter that describes the value that the user gets during the execution of the test script
  • expected is the second parameter that describes what the user should get to validate the functionality of the test case

What are Assertion errors?

An Assertion Error, which is a subclass of the Error class, is thrown whenever an issue is encountered during the process of Selenium automation testing.

package com.lambdatest;
import java.net.URL;
import java.util.ArrayList;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

public class AssertionTestOnLambdatest
{
private static RemoteWebDriver driver;
private static String status;

@Test
public void hardAssertion() throws Exception
{
String username = "Enter your user name";
String accesskey = "Enter your access key";
String gridURL = "@hub.lambdatest.com/wd/hub";

DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability("browserName", "chrome");
capabilities.setCapability("version", "latest");
capabilities.setCapability("platform", "win10");
capabilities.setCapability("build", "AssertionError Test");
capabilities.setCapability("name", "AssertionError Test");
capabilities.setCapability("visual", true);
driver=new RemoteWebDriver(new URL("https://"+username+":"+accesskey+gridURL), capabilities);

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

String expectedtitle="Most Powerful Cross Browser Testing Tool Online | LambdaTe";

String actualtitle = driver.getTitle();
ArrayList<String> exceptionCapture = new ArrayList<>();
try
{
Assert.assertEquals(actualtitle, expectedtitle);
status="passed";
}
catch(AssertionError e)
{
status = "failed";
exceptionCapture.add(e.getMessage());
((JavascriptExecutor) driver).executeScript("lambda-exceptions", exceptionCapture);
}
}

@AfterTest
public void tearDown() throws Exception
{

if (driver != null) {
((JavascriptExecutor) driver).executeScript("lambda-status=" + status);
driver.quit();
}

}
}
mvn test –P single

Different Methods in TestNG assertions

TestNG provides different methods to validate the test result during script execution. In this section, we will see the most commonly used assertion methods available for TestNG assertions

Assertion Messages in TestNG Tests

TestNG provides the flexibility to add a custom message as a parameter inside the test method. The message helps in better understanding and tracking the reason behind the test failure.

Assert.methodName(actual, expected, message);
package com.lambdatest;

import java.net.URL;
import java.util.ArrayList;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

public class AssertionTestOnLambdatest
{

private static RemoteWebDriver driver;
private static String status;

@Test
public void hardAssertion() throws Exception
{
String username = "Enter your user name";
String accesskey = "Enter your access key";
String gridURL = "@hub.lambdatest.com/wd/hub";

DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability("browserName", "chrome");
capabilities.setCapability("version", "latest");
capabilities.setCapability("platform", "win10");
capabilities.setCapability("build", "AssertionError Test");
capabilities.setCapability("name", "AssertionError Test");
capabilities.setCapability("visual", true);
driver=new RemoteWebDriver(new URL("https://"+username+":"+accesskey+gridURL), capabilities);

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

String expectedtitle="Most Powerful Cross Browser Testing Tool Online | LambdaTe;

String actualtitle = driver.getTitle();
ArrayList<String> exceptionCapture = new ArrayList<>();
try
{
Assert.assertEquals(actualtitle, expectedtitle);
status="passed";
}
catch(AssertionError e)
{
status = "failed";
exceptionCapture.add("Title not matched"+" "+e.getMessage());
((JavascriptExecutor) driver).executeScript("lambda-exceptions", exceptionCapture);
}
}

@AfterTest
public void tearDown() throws Exception
{

if (driver != null) {
((JavascriptExecutor) driver).executeScript("lambda-status=" + status);
driver.quit();
}

}
}

Different types of TestNG Assertions

Soft Assertions and Hard Assertions are the two main types of Assertions in TestNG. The different types of asserts are defined based on the necessity that the user wants to halt the execution or continue the execution in case there is a failure.

Hard Assertions in TestNG

Hard TestNG Assertions should be used when failure in test execution should halt the overall execution of the test suite. By default, TestNG assertions are ‘hard’ in nature. Hence, you need to import the org.testng.asserts.SoftAssert package if you want to raise Soft TestNG Assertions in your test.

package com.lambdatest;
import java.net.URL;
import java.util.ArrayList;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

public class HardAssertTest
{
private static RemoteWebDriver driver;
private static String status;
@Test
public void hardAssertion() throws Exception
{
String username = "user_name";
//Enter your access key
String accesskey = "access_key";
//Enter Grid URL
String gridURL = "@hub.lambdatest.com/wd/hub";
//Set Desired capabilities to run on Lambdatest platform
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", "chrome"); //Set Browser Name
capabilities.setCapability("version", "latest"); //Set browser version
capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one
capabilities.setCapability("build", "Hard Assert Test");
capabilities.setCapability("name", "Hard Assert Test");
capabilities.setCapability("visual", true);

driver = new RemoteWebDriver(new URL("https://"+username+":"+accesskey+gridURL), capabilities);
driver.get("https://www.lambdatest.com/");
// Define expected title
String expectedtitle = "Most Powerful Cross Browser Testing Tool Online | LambdaTe";
// Extract Actual title
String actualtitle = driver.getTitle();
ArrayList<String> exceptionCapture = new ArrayList<>();
try
{
//Assertion applied to validate the result
Assert.assertEquals(actualtitle, expectedtitle);
System.out.println("This statement will not be executed because the previous statement is failed");
status="passed";
}
catch(AssertionError e)
{
//Logic to capture Assertion error message with user customized message in exception tab on lambdatest
status = "failed";
exceptionCapture.add("Title not matched"+" "+e.getMessage());
((JavascriptExecutor) driver).executeScript("lambda-exceptions", exceptionCapture);
}
}
@AfterTest
public void tearDown() throws Exception
{
if (driver != null)
{
((JavascriptExecutor) driver).executeScript("lambda-status=" + status);
driver.quit();
}
}
}

Soft Assertions in TestNG

Soft TestNG Assertions are preferred in scenarios where a failure in the test should not result in halting the execution of the other tests in the test suite. Use soft assertions when the failure is not a major one, thereby having minimal (to no) impact on the subsequent tests.

package com.lambdatest;

import java.net.URL;
import java.util.ArrayList;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class SoftAssertTest
{
private static RemoteWebDriver driver;
private static String status;
@Test
public void softAssertion() throws Exception
{
String username = "user_name";

//Enter your access key
String accesskey = "access_key";

//Enter Grid URL
String gridURL = "@hub.lambdatest.com/wd/hub";

//Set Desired capabilities to run on Lambdatest platform
DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability("browserName", "chrome"); //Set Browser Name
capabilities.setCapability("version", "latest"); //Set browser version
capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one
capabilities.setCapability("build", "Hard Assert Test");
capabilities.setCapability("name", "Hard Assert Test");
capabilities.setCapability("visual", true);

driver = new RemoteWebDriver(new URL("https://"+username+":"+accesskey+gridURL), capabilities);

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

// Creating the object of SoftAssert Class
SoftAssert softassert = new SoftAssert();

// Define expected title
String expectedtitle = "Most Powerful Cross Browser Testing Tool Online | LambdaTe";

// Extract Actual title
String actualtitle = driver.getTitle();
// Soft Assertion applied to validate the result
softassert.assertEquals(actualtitle, expectedtitle,"Title is not Matched");

System.out.println("This statement will be executed even the previous statement is failed");
}
@AfterTest
public void tearDown() throws Exception
{
if (driver != null)
{
driver.quit();
}
}
}

Custom Soft Assertions in TestNG

In the previous sections, we demonstrated the implementation of different TestNG asserts, but there might be a need to write our assertions (called Custom Assertions).

package com.lt.assertions;

import org.openqa.selenium.WebElement;
import org.testng.asserts.SoftAssert;

public class CustomAssertion extends SoftAssert
{
// We created assertButton in which we will validate whether element is button or not
public void assertButton(WebElement element)
{
// Extract the tagname as well as type of element
String tagname = element.getTagName();
String type = element.getAttribute("type");

// Verify whether tag is button and type is submit
if(tagname.equals("button") && type.equals("submit"))
{
System.out.println("Element is Button, So Assertion Passed");
}
else
{
// If assertion is failed then we will throw AssertionError
throw new AssertionError("Element is not Button, So Assertion Failed");
}
}
}
package com.lt.assertions;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class TestAssertion
{
public static void main(String[] args)
{
RemoteWebDriver driver;

String username = "user_name";
//Enter your access key
String accesskey = "access_key";
//Enter Grid URL
String gridURL = "@hub.lambdatest.com/wd/hub";
//Set Desired capabilities to run on Lambdatest platform
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", "chrome"); //Set Browser Name
capabilities.setCapability("version", "latest"); //Set browser version
capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one
capabilities.setCapability("build", "Hard Assert Test");
capabilities.setCapability("name", "Hard Assert Test");
capabilities.setCapability("visual", true);
driver = new RemoteWebDriver(new URL("https://"+username+":"+accesskey+gridURL), capabilities);

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

// Inspect the “Free Testing Button” element
WebElement freetestingbutton = driver.findElement(By.xpath("(//button[contains(text(),\"Start Free Testing\")])[1]"));

// Creating the object of CustomAssertion class
CustomAssertion customassert = new CustomAssertion();

// Calling the custom created method with the help of object
customassert.assertButton(freetestingbutton);
}
}
// Inspect the “Free Testing Button” element
WebElement freetestingbutton = driver.findElement(By.tagName("p"));

Conclusion

In this TestNG tutorial with a focus on TestNG assert, we looked at different asserts and constraints helpful in Selenium automation testing. The reason for using assert in test code is to pause the execution as soon as assert is encountered.

--

--

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