How To Use TestNG Assertions in Selenium | Soft & Hard Assertions
By Bhau Automation • Learn Hard and Soft Assertions in Selenium Java with practical code examples.
🎯 What You Will Learn
- What are Assertions in TestNG
- Difference between Hard and Soft Assertions
- When to use assertEquals, assertTrue, and assertFalse
- How to implement SoftAssert class
- Practical Selenium example with TestNG Assertions
💡 Pro Tip: Assertions are the heart of test validation in Selenium. Use them to verify expected outcomes effectively.
📘 What Are TestNG Assertions?
Assertions in TestNG are used to validate the actual result against the expected result. They help in determining whether a test has passed or failed.
TestNG provides two main types of assertions: Hard Assertions and Soft Assertions.
🔹 Hard Assertion
Definition: Stops test execution immediately when an assertion fails.
- Execution stops at the failed assertion.
- Throws an
AssertionErrorright away.
import org.testng.Assert;
import org.testng.annotations.Test;
public class HardAssertionExample {
@Test
public void verifyTitle() {
String actualTitle = "Bhau Automation";
String expectedTitle = "Bhau Automation";
Assert.assertEquals(actualTitle, expectedTitle);
System.out.println("This line will execute only if assertion passes");
}
}
Syntax:
Assert.assertEquals(actual, expected);
🔹 Soft Assertion
Definition: Allows the test to continue even after assertion failures.
At the end of the test, softAssert.assertAll() is used to collect all assertion results.
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class SoftAssertionExample {
@Test
public void verifyLogin() {
SoftAssert softAssert = new SoftAssert();
String actualTitle = "Automation";
String expectedTitle = "Bhau Automation";
softAssert.assertEquals(actualTitle, expectedTitle, "Title mismatch!");
System.out.println("This line will still execute even if assertion fails.");
softAssert.assertAll(); // Marks the test as failed if any assertion failed
}
}
Syntax:
SoftAssert softAssert = new SoftAssert(); softAssert.assertEquals(actual, expected); softAssert.assertAll();
🧠 Commonly Used TestNG Assertions
| Assertion | Description | Example |
|---|---|---|
assertEquals(actual, expected) | Checks if two values are equal. | Assert.assertEquals("Hello","Hello"); |
assertEquals(actual, expected, message) | Compares two values and prints a custom failure message. | Assert.assertEquals("Hi","Hello","Values not equal!"); |
assertTrue(condition) | Verifies that the condition is true. | Assert.assertTrue(loginSuccess); |
assertTrue(condition, message) | Verifies that the condition is true with a message. | Assert.assertTrue(isDisplayed, "Element not visible"); |
assertFalse(condition) | Verifies that the condition is false. | Assert.assertFalse(isError); |
assertFalse(condition, message) | Verifies that the condition is false with a message. | Assert.assertFalse(isError, "Unexpected error occurred"); |
💻 Example: Selenium with TestNG Assertions
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestNGAssertionDemo {
@Test
public void verifyPageTitle() {
WebDriver driver = new ChromeDriver();
driver.get("https://bhauautomation.com");
String actualTitle = driver.getTitle();
String expectedTitle = "Bhau Automation";
Assert.assertEquals(actualTitle, expectedTitle, "Page title does not match!");
driver.quit();
}
}
🎥 Watch the Complete Video Tutorial
👉 Watch on YouTube: How To Use TestNG Assertions in Selenium Java
🎓 Key Takeaways
- Use Hard Assertions for strict test validations.
- Use Soft Assertions when you want to continue test execution.
- Always call
assertAll()in SoftAssert to validate all conditions. - Assertions improve test reliability and debugging.
⚡ Next Steps: Practice using all TestNG assertions in different scenarios to master real-world test validation.
🚀 Created with ❤️ by Bhau Automation