Assertions are how your test decides "pass" or "fail." Without assertions, you are just clicking around and hoping for the best. TestNG gives you two types: Hard Assert (stops immediately on failure) and Soft Assert (collects all failures, reports at the end).
Think of it like a building inspector. A Hard Assert inspector finds a cracked foundation and immediately stops the inspection — nothing else matters if the foundation is broken. A Soft Assert inspector checks everything — cracked window, leaky pipe, faulty wiring — and gives you the full list at the end.
Hard Assert uses the Assert class. When an assertion fails, the test STOPS immediately. No further code in that test method runs.
import org.testng.Assert;
@Test
public void testLoginFlow() {
driver.findElement(By.id("username")).sendKeys("testuser");
driver.findElement(By.id("password")).sendKeys("pass123");
driver.findElement(By.cssSelector("button[type='submit']")).click();
// Hard Assert — if this fails, test STOPS here
Assert.assertTrue(
driver.getCurrentUrl().contains("/dashboard"),
"Should redirect to dashboard after login"
);
// These lines NEVER execute if the above assertion fails
WebElement welcomeMsg = driver.findElement(By.id("welcome"));
Assert.assertTrue(welcomeMsg.isDisplayed(), "Welcome message visible");
Assert.assertEquals(welcomeMsg.getText(), "Welcome, testuser!");
}import org.testng.Assert;
// assertEquals — Check exact match
Assert.assertEquals(driver.getTitle(), "Banking Portal");
Assert.assertEquals(items.size(), 5, "Cart should have 5 items");
// assertTrue / assertFalse — Check conditions
Assert.assertTrue(element.isDisplayed(), "Logo should be visible");
Assert.assertFalse(submitBtn.isEnabled(), "Button should be disabled");
// assertNotNull / assertNull — Check for null
Assert.assertNotNull(driver.findElement(By.id("logo")),
"Logo element must exist");
Assert.assertNull(errorMessage, "No error should appear");
// assertNotEquals — Check values are different
Assert.assertNotEquals(oldBalance, newBalance,
"Balance should change after transfer");
// assertSame — Check same object reference (rarely used)
Assert.assertSame(page1, page2, "Should be the exact same object");ALWAYS include the third parameter — the error message. Assert.assertTrue(isDisplayed) gives you "expected true but found false." Assert.assertTrue(isDisplayed, "Login button should be visible on the page") tells you exactly what went wrong. This saves hours of debugging.
Soft Assert lets you check multiple things and see ALL failures, not just the first one. The test continues even when an assertion fails. At the end, you call assertAll() to report everything.
import org.testng.asserts.SoftAssert;
@Test
public void testDashboardPage() {
SoftAssert soft = new SoftAssert();
driver.get("https://www.testerrank.com/banking/dashboard");
// Check multiple things — test continues even if one fails
soft.assertEquals(driver.getTitle(), "Dashboard - Banking Portal",
"Page title mismatch");
WebElement username = driver.findElement(By.id("user-name"));
soft.assertTrue(username.isDisplayed(),
"Username should be visible");
WebElement balance = driver.findElement(By.id("balance"));
soft.assertFalse(balance.getText().isEmpty(),
"Balance should not be empty");
WebElement lastLogin = driver.findElement(By.id("last-login"));
soft.assertTrue(lastLogin.getText().contains("2024"),
"Last login should show the year");
WebElement navMenu = driver.findElement(By.id("nav-menu"));
soft.assertTrue(navMenu.isDisplayed(),
"Navigation menu should be visible");
// CRITICAL: You MUST call assertAll() at the end!
soft.assertAll();
}If you forget to call softAssert.assertAll() at the end, ALL failures are silently ignored. Your test will always pass, even when things are broken. This is the #1 mistake with Soft Assert. I have seen this in production frameworks where tests were "green" for months while the app was broken.
| Use Hard Assert When | Use Soft Assert When |
|---|---|
| A failure makes the rest of the test pointless | Checking multiple independent fields on a page |
| Login failed → skip dashboard checks | Verify title, header, footer, sidebar together |
| Page did not load → skip element checks | Form validation — check all error messages |
| Preconditions must be met | Dashboard — check all widgets and counts |
| Critical path validation | You want the FULL picture of failures |
Senior engineers combine both. Hard Assert for critical preconditions, Soft Assert for page-level verification:
@Test
public void testDashboardAfterLogin() {
// Step 1: Login — HARD assert (if this fails, skip everything)
driver.findElement(By.id("username")).sendKeys("testuser");
driver.findElement(By.id("password")).sendKeys("pass123");
driver.findElement(By.cssSelector("button[type='submit']")).click();
Assert.assertTrue(
driver.getCurrentUrl().contains("/dashboard"),
"Login must succeed before checking dashboard"
);
// Step 2: Verify dashboard — SOFT assert (check everything)
SoftAssert soft = new SoftAssert();
soft.assertEquals(driver.getTitle(), "Dashboard",
"Title mismatch");
soft.assertTrue(
driver.findElement(By.id("balance")).isDisplayed(),
"Balance should be visible");
soft.assertTrue(
driver.findElement(By.id("transactions")).isDisplayed(),
"Transactions should be visible");
soft.assertTrue(
driver.findElement(By.id("quick-transfer")).isDisplayed(),
"Quick transfer should be visible");
soft.assertAll();
}Q: What is the difference between Hard Assert and Soft Assert?
A: Hard Assert (Assert class) stops the test immediately on the first failure — no further code executes. Soft Assert (SoftAssert class) collects all failures and continues execution. You call softAssert.assertAll() at the end to report all failures together. I use Hard Assert for critical preconditions like verifying login succeeded. I use Soft Assert when verifying multiple independent elements on a page — like checking title, header, balance, and navigation all at once. Important: forgetting assertAll() silently ignores all Soft Assert failures.
Key Point: Hard Assert stops on first failure. Soft Assert collects all failures and reports at the end. Always call softAssert.assertAll().