Soft Assertion in Playwright Java

Profile picture for user arilio666

In this article, we will use how to soft assert using Playwright Java.

TestNG is a testing framework used to write and run automated tests. TestNG has the most reliable ability, which is the ability to do assertions. This allows us to verify the expected behavior of the code.

Soft Assertion:

  • Soft Assert is a type of Assertion that does not stop the execution of the test case as soon as an assertion fails. 
  • In other words, if a soft assert fails, TestNG does not immediately mark the test case as a failure but continues executing the test case until the end. 
  • At the end , TestNG reports all failed assertions.

        page.navigate("https://www.programsbuzz.com/user/login");
        page.locator("#edit-name").type("nana");
        page.locator("#edit-pass").type("nnv");
        page.locator("(//input[@type='submit'])[2]").click();
        String actualText = page.locator("//a[normalize-space()='Forgot your password?']").textContent();
        System.out.println(actualText);
        String expectedText = "Forgot your password";
        SoftAssert soft = new SoftAssert();
        soft.assertEquals(actualText, expectedText, "Matched");

        System.out.println("This part is executed");
        soft.assertAll();
        
  • We have used it to assert the error message "Forgot your password?".
  • We will purposefully fail the test here by giving the wrong expectedText.
  • The test has been executed, and the Assertion failed message is updated.
  • We can see that soft Assertion is working as expected without compromising the whole test.