Assertions are used to validate expected results against the actual behavior of an application when using Selenium with TestNG. They play a crucial role in ensuring the accuracy and reliability of test cases.
There are two main types of assertions available in TestNG: Hard Assertions and Soft Assertions.
Hard Assertions immediately fail the test execution when a mismatch is found, throwing an exception and stopping further execution of the test.
Soft Assertions, on the other hand, allow the test to continue even if an assertion fails. All assertion results are collected, and the test is marked as failed only when assertAll() is invoked at the end.
Below are examples demonstrating both Hard and Soft Assertions in Selenium using Java and TestNG.
Hard Assertion in selenium with Testng using Java:
Hard assertions can be implemented using the Assert class in TestNG. One of the most commonly used methods is assertEquals(), which compares the expected and actual values. If they do not match, the test immediately fails and execution stops.
Example Code:
package tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class VerifyTitle {
WebDriver driver = null;
@Test
public void hardAssertion() {
try {
driver = new ChromeDriver();
driver.get("https://www.google.com");
String expTitle = "Google Local";
String actualTitle = "";
actualTitle = driver.getTitle();
Assert.assertEquals(expTitle, actualTitle);
} catch (Exception ex) {
System.out.print(ex.getMessage());
}
}
}
As we see in the image below test execution is failed due to mismatch with title ‘Yahoo’ and ‘Google’

SoftAssertion in selenium with Testng using Java:
Soft assertions can be implemented using the SoftAssert class in TestNG. In this approach, multiple assertions are performed without immediately stopping the test execution when a failure occurs. Instead, all assertions are evaluated, and the final result is reported when the assertAll() method is invoked at the end.
Example Code:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class HardAndSoftAssertion {
WebDriver driver = null;
@Test
public void verifySoftAssertion() {
try {
driver = new ChromeDriver();
driver.get("https://www.google.com");
String expTitle = "Yahoo";
String actualTitle = "";
actualTitle = driver.getTitle();
SoftAssert softassert = new SoftAssert();
softassert.assertEquals(expTitle, actualTitle);
softassert.assertAll();
} catch (Exception ex) {
System.out.print(ex.getMessage());
}
}
}
As shown in the screenshot below, the test execution is marked as passed even though there is a mismatch between the titles “Google” and “Yahoo”. This happens because Soft Assertions were used and the assertAll() method was not invoked, so the failure was not reported.

For YouTube Video Click On: What is the difference between soft and hard assertion