In this tutorial, will discuss about Hard Assert and Soft Assert in TestNG. Before starting with Hard and Soft Assert, go through What is Assert in TestNG.
What is Hard Assert?
Hard Assertion throws AssertionError immediately when an Assert Condition fails and moves to next @Test method
Suppose, there are 2 assertions in a Test and the first assertion fails, then HardAssertion do not execute the second Assertion Condition and declare the test as failed
As you can see in the below example, there are 2 assert conditions under Test – AssertionFailure(). As first Assert Condition fails, it moved directly to second test without executing another Assert Condition
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class HardAssertionDemo {
@Test
public void AssertionFailure() {
System.setProperty("webdriver.gecko.driver", "src\\test\\resources\\webdrivers\\window\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://duckduckgo.com/");
String actualTitle = "DuckDuckGo — Privacy, simplified";
String expectedTitle = driver.getTitle();
String expectedText = driver.findElement(By.xpath("/html/body/div/div[2]/div/div[3]/div[1]")).getText();
/* Hard Assert */
Assert.assertEquals(expectedTitle, actualTitle, "Incorrect page title");
Assert.assertEquals("Privacy Protection For Any Device", expectedText);
}
@Test
public void print() {
System.out.println("Hard Assertion is displayed");
}
}



What is Soft Assert?
To overcome above mentioned problem, there is another type of assertion called Soft Assert
Soft Assert does not throw an exception when an Assert Condition fails and continue with the next step after the Assert Condition.
Soft assert does not include by default in TestNG. For this, you need to include the package org.testng.asserts.SoftAssert;
SoftAssert softAssertion = new SoftAssert();
Create an object of SoftAssertion to run Assert Conditions
Below is an example of Soft Assert
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class SoftAssertionDemo {
@Test
public void AssertionFailure() {
SoftAssert softAssertion = new SoftAssert();
System.setProperty("webdriver.gecko.driver", "src\\test\\resources\\webdrivers\\window\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://duckduckgo.com/");
String actualTitle = "DuckDuckGo — Privacy, simplified";
String expectedTitle = driver.getTitle();
String expectedText1 = driver.findElement(By.xpath("/html/body/div/div[2]/div/div[3]/div[1]")).getText();
/* Soft Assert */
softAssertion.assertEquals(expectedTitle, actualTitle, "Incorrect page title");
softAssertion.assertEquals("Privacy Protection For Any Device", expectedText1);
}
@Test
public void print() {
System.out.println("Soft Assertion is displayed");
}
}



AssertAll
If there is any exception and you want to throw it then you need to use assertAll() method as a last statement in the @Test and test suite again continue with next @Test as it is.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class SoftAssertionDemo {
@Test
public void AssertionFailure() {
SoftAssert softAssertion = new SoftAssert();
System.setProperty("webdriver.gecko.driver", "src\\test\\resources\\webdrivers\\window\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://duckduckgo.com/");
String actualTitle = "DuckDuckGo — Privacy, simplified";
String expectedTitle = driver.getTitle();
String expectedText1 = driver.findElement(By.xpath("/html/body/div/div[2]/div/div[3]/div[1]")).getText();
/* Soft Assert */
softAssertion.assertEquals(expectedTitle, actualTitle, "Incorrect page title");
softAssertion.assertEquals("Privacy Protection For Any Device", expectedText1);
softAssertion.assertAll();
}
@Test
public void print() {
System.out.println("Soft Assertion is displayed");
}
}


