{"id":3631,"date":"2025-01-17T09:29:53","date_gmt":"2025-01-17T09:29:53","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=3631"},"modified":"2025-01-17T10:59:07","modified_gmt":"2025-01-17T10:59:07","slug":"soft-assertion-using-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/17\/soft-assertion-using-playwright-java\/","title":{"rendered":"Soft Assertion using Playwright Java"},"content":{"rendered":"<p><em>In this blog we will be utilizing Playwright Java to implement soft assertion.<\/em><\/p>\n<p><strong>Topics that we will cover:<\/strong><\/p>\n<ul>\n<li>Soft assertion<\/li>\n<li>Source code (soft assertion)<\/li>\n<\/ul>\n<p><strong>Soft assertion<\/strong><\/p>\n<p>A \u201cSoft Assert\u201d is an Assertion type that allows the test case to continue executing even if an assertion fails.&nbsp;<\/p>\n<p>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.&nbsp;<\/p>\n<p>TestNG generates reports for all failed assertions at the conclusion of the test execution.<\/p>\n<p>To understand soft assertion, let us go to <a href=\"https:\/\/www.flipkart.com\/account\/login?ret=\/\" style=\"outline: none;\" target=\"_blank\" rel=\"noopener\">https:\/\/www.flipkart.com\/account\/login?ret=\/<\/a>&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3634\" width=\"501\" data-init-width=\"1176\" height=\"193\" data-init-height=\"454\" title=\"Screenshot 2025-01-17 at 3.00.30\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.00.30\u202fPM.png\" data-width=\"501\" data-height=\"193\" style=\"aspect-ratio: auto 1176 \/ 454;\"><\/span><\/p>\n<p>&nbsp;When we click \u2018Request OTP\u2019, we get the text error message that is highlighted below<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3635\" width=\"545\" data-init-width=\"1176\" height=\"547\" data-init-height=\"1180\" title=\"Screenshot 2025-01-17 at 3.01.06\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.01.06\u202fPM.png\" data-width=\"545\" data-height=\"547\" style=\"aspect-ratio: auto 1176 \/ 1180;\"><\/span><\/p>\n<p>&nbsp;<em>\/\/button[normalize-space()=&#8217;Request OTP&#8217;]<\/em><\/p>\n<p>So we can use the above relative xpath to identify the OTP button.<\/p>\n<p>Similarly let us inspect the errored text message<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3636\" width=\"514\" data-init-width=\"1176\" height=\"312\" data-init-height=\"714\" title=\"Screenshot 2025-01-17 at 3.01.43\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.01.43\u202fPM.png\" data-width=\"514\" data-height=\"312\" style=\"aspect-ratio: auto 1176 \/ 714;\"><\/span><\/p>\n<p>&nbsp;<em>\/\/span[contains(text(),&#8217;Please enter valid Email ID\/Mobile number&#8217;)]<\/em><\/p>\n<p>So we can use the above relative xpath to identify the error text message.<\/p>\n<p>See below snapshot.&nbsp;<\/p>\n<p>In line#29 and 32, we are storing the actual and expected error messages.&nbsp;<\/p>\n<p>We have intentionally made a mistake in expected error message (instead of \u2018enter\u2019 we have written \u2018ener\u2019, see line#32)&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3637\" width=\"510\" data-init-width=\"1176\" height=\"255\" data-init-height=\"588\" title=\"Screenshot 2025-01-17 at 3.02.11\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.02.11\u202fPM.png\" data-width=\"510\" data-height=\"255\" style=\"aspect-ratio: auto 1176 \/ 588;\"><\/span><\/p>\n<p>&nbsp;That means, the actual and expected errors do not match. Our intention is to deliberately fail the test by supplying an inaccurate <em>expectedErrorMessage<\/em>.&nbsp;<\/p>\n<p>We than create an object of \u2018SoftAssert\u2019 and call the <em>assertEquals()<\/em> method (line#35).&nbsp;<\/p>\n<p>The <em>assertEquals()<\/em> method is used to verify if two objects are equal. If they are not, an \u201cAssertion Error\u201d is thrown along with the specified message<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3638\" width=\"553\" data-init-width=\"1176\" height=\"174\" data-init-height=\"370\" title=\"Screenshot 2025-01-17 at 3.02.39\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.02.39\u202fPM.png\" data-width=\"553\" data-height=\"174\" style=\"aspect-ratio: auto 1176 \/ 370;\"><\/span><\/p>\n<p>&nbsp;The <em>assertAll()<\/em> 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.<\/p>\n<p>So our main code snippet looks like below<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3639\" width=\"525\" data-init-width=\"1176\" height=\"355\" data-init-height=\"796\" title=\"Screenshot 2025-01-17 at 3.03.12\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.03.12\u202fPM.png\" data-width=\"525\" data-height=\"355\" style=\"aspect-ratio: auto 1176 \/ 796;\"><\/span><\/p>\n<p>&nbsp;Execute.<\/p>\n<p>Notice that although the assertion failed, the test execution did not terminate in between. The line#37 still got executed&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3640\" width=\"550\" data-init-width=\"1176\" height=\"659\" data-init-height=\"1410\" title=\"Screenshot 2025-01-17 at 3.03.49\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.03.49\u202fPM.png\" data-width=\"550\" data-height=\"659\" style=\"aspect-ratio: auto 1176 \/ 1410;\"><\/span><\/p>\n<p>&nbsp;<strong>Source code (soft assertion)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<\/p>\n<p><strong>import<\/strong> org.testng.annotations.AfterMethod;<\/p>\n<p><strong>import<\/strong> org.testng.annotations.BeforeMethod;<\/p>\n<p><strong>import<\/strong> org.testng.annotations.Test;<\/p>\n<p><strong>import<\/strong> org.testng.asserts.SoftAssert;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Browser;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<p><strong>public<\/strong><strong>class<\/strong> Blog37_Verify_SoftAssertion {<\/p>\n<p><strong>private<\/strong> Browser browser;<\/p>\n<p><strong>private<\/strong> Page page;<\/p>\n<p>@BeforeMethod<\/p>\n<p><strong>public<\/strong><strong>void<\/strong> setUp() {<\/p>\n<p>Playwright playwright = Playwright.<em>create<\/em>();&nbsp;<\/p>\n<p>browser = playwright.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<p>page = browser.newPage();<\/p>\n<p>}<\/p>\n<p>@Test<\/p>\n<p><strong>public<\/strong><strong>void<\/strong> test_1() {<\/p>\n<p>page.navigate(&#8220;https:\/\/www.flipkart.com\/account\/login?ret=\/&#8221;);<\/p>\n<p>page.locator(&#8220;\/\/button[normalize-space()=&#8217;Request OTP&#8217;]&#8221;).click();<\/p>\n<p>String actualErrorMessage = page.locator(&#8220;\/\/span[contains(text(),&#8217;Please enter valid Email ID\/Mobile number&#8217;)]&#8221;).textContent();<\/p>\n<p>\/\/System.out.println(&#8220;The actual error message is &#8211;&gt;&#8221; + actualErrorMessage);<\/p>\n<p>String expectedErrorMessage = &#8220;Please ener valid Email ID\/Mobile number&#8221;;<\/p>\n<p>SoftAssert soft = <strong>new<\/strong> SoftAssert();<\/p>\n<p>soft.assertEquals(actualErrorMessage, expectedErrorMessage, &#8220;Match&#8221;);<\/p>\n<\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(&#8220;The execution is complete&#8221;);<\/p>\n<p>soft.assertAll();<\/p>\n<p>}<\/p>\n<p>@AfterMethod<\/p>\n<p><strong>public<\/strong><strong>void<\/strong> tearDown() {<\/p>\n<p>page.pause();<\/p>\n<p>browser.close();<\/p>\n<p>page.close();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/p>\n<p>Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 \u201cSoft Assert\u201d is an Assertion type that allows the test case to continue executing even if an assertion fails.&nbsp; Thus, in the event of a soft assert failure, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":750,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-3631","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-playwright","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3631"}],"version-history":[{"count":6,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3631\/revisions"}],"predecessor-version":[{"id":3761,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3631\/revisions\/3761"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/750"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}