TestNG Assertions in Selenium Java – Hard vs Soft Assertions (assertEquals, assertTrue)
By Bhau Automation • Learn Hard & Soft Assertions in TestNG with Selenium
🎯 What You Will Learn in This Video
- What are assertions in TestNG?
- Difference between Hard Assertion and Soft Assertion
- How to use assertEquals, assertTrue, assertFalse
- Practical Selenium examples with TestNG assertions
- Common mistakes and best practices
- Interview questions on TestNG assertions
💡 Pro Tip: Use Hard Assertions for critical validations and Soft Assertions when you want to collect multiple failures in a single test run.
📌 What are Assertions in TestNG?
Assertions in TestNG are used to validate expected vs actual results during Selenium test execution. If an assertion fails, TestNG marks the test as failed.
❌ Hard Assertion
Hard Assertion stops test execution immediately when an assertion fails.
Assert.assertEquals(actual, expected); Assert.assertTrue(condition);
✅ Soft Assertion
Soft Assertion allows test execution to continue even after assertion failures. All failures are reported at the end using assertAll().
SoftAssert softAssert = new SoftAssert(); softAssert.assertEquals(actual, expected); softAssert.assertTrue(condition); softAssert.assertAll();
🧪 Commonly Used TestNG Assertions
- assertEquals(actual, expected) – Validates equality
- assertEquals(actual, expected, message) – With custom failure message
- assertTrue(condition) – Validates true condition
- assertTrue(condition, message) – With message
- assertFalse(condition) – Validates false condition
- assertFalse(condition, message) – With message
💻 Practical Selenium Example with Assertions
@Test
public void loginTest() {
String actualTitle = driver.getTitle();
String expectedTitle = "Dashboard";
Assert.assertEquals(actualTitle, expectedTitle, "Title mismatch!");
}
📚 Prerequisites (Recommended)
- TestNG Framework & Annotations – Part 1
- TestNG Annotations Execution Flow – Part 2
- TestNG Parameterization – Part 3
- TestNG DataProvider – Part 6
❓ Common Interview Questions
Q: What is the difference between Hard Assert and Soft Assert?
A: Hard Assert stops execution immediately on failure, while Soft Assert continues and reports all failures at the end.
Q: Why do we use assertAll() in SoftAssert?
A: assertAll() is mandatory to throw assertion failures collected during test execution.
🎥 Watch the Complete Video Tutorial
👉 Watch on YouTube: TestNG Assertions in Selenium Java (Hard & Soft Assertions)
🎓 Key Takeaways
- Assertions are core to validating Selenium test results
- Use Hard Assertions for critical failures
- Use Soft Assertions to validate multiple conditions in one test
- Assertions are frequently asked in Selenium interviews
🚀 Created with ❤️ by Bhau Automation