</>

Technology

TestNG

Difficulty

Intermediate

Interview Question

What is the difference between SoftAssert and HardAssert in TestNG with real examples?

Answer

SoftAssert vs HardAssert in TestNG

HardAssert (Default) — Stops on First Failure

Java
import org.testng.Assert;

@Test
public void hardAssertExample() {
    driver.get("https://automateqa.online/profile");

    Assert.assertEquals(getFieldValue("name"),  "AutomateQA Team"); // if FAILS → test stops here
    Assert.assertEquals(getFieldValue("email"), "team@automateqa.online"); // NEVER REACHED
    Assert.assertEquals(getFieldValue("role"),  "Admin");            // NEVER REACHED
}
// Result: Only the first failure is reported. Other fields not validated.

SoftAssert — Collects All Failures

Java
import org.testng.asserts.SoftAssert;

@Test
public void softAssertExample() {
    SoftAssert soft = new SoftAssert();
    driver.get("https://automateqa.online/profile");

    soft.assertEquals(getFieldValue("name"),  "AutomateQA Team");      // recorded
    soft.assertEquals(getFieldValue("email"), "team@automateqa.online");// recorded
    soft.assertEquals(getFieldValue("role"),  "Admin");                 // recorded
    soft.assertTrue(isProfileImageVisible(),  "Profile image missing"); // recorded

    soft.assertAll(); // NOW reports ALL failures at once
}
// Result: All 4 assertions evaluated; report shows all that failed.

Real-World: Registration Form Validation

Java
@Test
public void verifyRegistrationForm() {
    SoftAssert soft = new SoftAssert();

    driver.findElement(By.id("submitBtn")).click(); // submit empty form

    // Validate ALL error messages in one test
    soft.assertTrue(isErrorVisible("name"),       "Name field error missing");
    soft.assertTrue(isErrorVisible("email"),      "Email field error missing");
    soft.assertTrue(isErrorVisible("password"),   "Password field error missing");
    soft.assertTrue(isErrorVisible("phone"),      "Phone field error missing");
    soft.assertEquals(getErrorText("email"),      "Please enter a valid email");
    soft.assertEquals(getErrorText("password"),   "Password must be 8+ characters");

    soft.assertAll(); // report all field validation failures
}

Common Mistake — Forgetting assertAll()

Java
@Test
public void badSoftAssert() {
    SoftAssert soft = new SoftAssert();
    soft.assertTrue(false, "This failure is recorded but...");
    // FORGOT soft.assertAll() — test PASSES despite failure!
}

SoftAssert in Page Objects

Java
public class ProfilePage {
    SoftAssert soft = new SoftAssert();

    public ProfilePage verifyName(String expected) {
        soft.assertEquals(getField("name"), expected, "Name mismatch");
        return this; // fluent
    }
    public ProfilePage verifyEmail(String expected) {
        soft.assertEquals(getField("email"), expected, "Email mismatch");
        return this;
    }
    public void assertAll() { soft.assertAll(); }
}

// In test:
new ProfilePage(driver)
    .verifyName("AutomateQA Team")
    .verifyEmail("team@automateqa.online")
    .assertAll();

When to Use Each

ScenarioUse
Login — if wrong URL, rest makes no senseHardAssert
Form field validation — check all errorsSoftAssert
Navigation — wrong page = test meaninglessHardAssert
Dashboard widget values — check all at onceSoftAssert
Precondition checksHardAssert
Multi-field UI regressionSoftAssert

Follow AutomateQA

Related Topics