In this blog we will be utilizing Playwright Java to implement soft assertion.

Topics that we will cover:

  • Soft assertion
  • Source code (soft assertion)

Soft assertion

A “Soft Assert” is an Assertion type that allows the test case to continue executing even if an assertion fails. 

Thus, in the event of a soft assert failure, TestNG does not promptly classify the test case as unsuccessful; instead, it proceeds with executing the test case until completion. 

TestNG generates reports for all failed assertions at the conclusion of the test execution.

To understand soft assertion, let us go to https://www.flipkart.com/account/login?ret=/ 

 When we click ‘Request OTP’, we get the text error message that is highlighted below

 //button[normalize-space()='Request OTP']

So we can use the above relative xpath to identify the OTP button.

Similarly let us inspect the errored text message

 //span[contains(text(),'Please enter valid Email ID/Mobile number')]

So we can use the above relative xpath to identify the error text message.

See below snapshot. 

In line#29 and 32, we are storing the actual and expected error messages. 

We have intentionally made a mistake in expected error message (instead of ‘enter’ we have written ‘ener’, see line#32) 

 That means, the actual and expected errors do not match. Our intention is to deliberately fail the test by supplying an inaccurate expectedErrorMessage

We than create an object of ‘SoftAssert’ and call the assertEquals() method (line#35). 

The assertEquals() method is used to verify if two objects are equal. If they are not, an “Assertion Error” is thrown along with the specified message

 The assertAll() method in TestNG (line#38) is utilized to guarantee the execution of all assertions defined within a test method. It also provides a consolidated report of all failures instead of halting after the first failure.

So our main code snippet looks like below

 Execute.

Notice that although the assertion failed, the test execution did not terminate in between. The line#37 still got executed 

 Source code (soft assertion)

package com.rsa.playwrightjava;


import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Test;

import org.testng.asserts.SoftAssert;

import com.microsoft.playwright.Browser;

import com.microsoft.playwright.BrowserType;

import com.microsoft.playwright.Page;

import com.microsoft.playwright.Playwright;

public class Blog37_Verify_SoftAssertion {

private Browser browser;

private Page page;

@BeforeMethod

public void setUp() {

Playwright playwright = Playwright.create(); 

browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));

page = browser.newPage();

}

@Test

public void test_1() {

page.navigate("https://www.flipkart.com/account/login?ret=/");

page.locator("//button[normalize-space()='Request OTP']").click();

String actualErrorMessage = page.locator("//span[contains(text(),'Please enter valid Email ID/Mobile number')]").textContent();

//System.out.println("The actual error message is -->" + actualErrorMessage);

String expectedErrorMessage = "Please ener valid Email ID/Mobile number";

SoftAssert soft = new SoftAssert();

soft.assertEquals(actualErrorMessage, expectedErrorMessage, "Match");


System.out.println("The execution is complete");

soft.assertAll();

}

@AfterMethod

public void tearDown() {

page.pause();

browser.close();

page.close();

}

}


Thanks.


Tags


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}