{"id":3631,"date":"2025-01-17T09:29:53","date_gmt":"2025-01-17T09:29:53","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/17\/soft-assertion-using-playwright-java\/"},"modified":"2025-01-17T09:29:53","modified_gmt":"2025-01-17T09:29:53","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><html><body><\/p>\n<section class=\"tcb-post-content tcb-shortcode thrv_wrapper\" data-css=\"tve-u-16ec5d248bb\">\n<div class=\"tve_flt tcb-style-wrap\" id=\"tve_flt\">\n<div class=\"tve_shortcode_editor tar-main-content\" data-post-id=\"3631\" id=\"tve_editor\">\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><em>In this blog we will be utilizing Playwright Java to implement soft assertion.<\/em><\/p>\n<div class=\"code-block code-block-2\" style=\"margin: 8px auto; text-align: center; display: block; clear: both;\">\n<p><!-- horizontal responsive 2 --><br \/>\n<ins class=\"adsbygoogle\" data-ad-client=\"ca-pub-3231318496783163\" data-ad-format=\"auto\" data-ad-slot=\"2788468763\" data-full-width-responsive=\"true\" style=\"display:block\"><\/ins>\n<\/div>\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.\u00a0<\/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.\u00a0<\/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 class=\"\" href=\"https:\/\/www.flipkart.com\/account\/login?ret=\/\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/www.flipkart.com\/account\/login?ret=\/<\/a>\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194739b2a0f\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3634\" data-css=\"tve-u-194739b3ac7\" data-height=\"193\" data-id=\"3634\" data-init-height=\"454\" data-init-width=\"1176\" data-width=\"501\" decoding=\"async\" height=\"193\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.00.30\u202fPM.png\" style=\"aspect-ratio: auto 1176 \/ 454;\" title=\"Screenshot 2025-01-17 at 3.00.30\u202fPM\" width=\"501\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0When we click \u2018Request OTP\u2019, we get the text error message that is highlighted below<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194739bb16f\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3635\" data-css=\"tve-u-194739bc762\" data-height=\"547\" data-id=\"3635\" data-init-height=\"1180\" data-init-width=\"1176\" data-width=\"545\" decoding=\"async\" height=\"547\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.01.06\u202fPM.png\" style=\"aspect-ratio: auto 1176 \/ 1180;\" title=\"Screenshot 2025-01-17 at 3.01.06\u202fPM\" width=\"545\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194739c308e\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3636\" data-css=\"tve-u-194739c3efc\" data-height=\"312\" data-id=\"3636\" data-init-height=\"714\" data-init-width=\"1176\" data-width=\"514\" decoding=\"async\" height=\"312\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.01.43\u202fPM.png\" style=\"aspect-ratio: auto 1176 \/ 714;\" title=\"Screenshot 2025-01-17 at 3.01.43\u202fPM\" width=\"514\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<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.\u00a0<\/p>\n<p>In line#29 and 32, we are storing the actual and expected error messages.\u00a0<\/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)\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194739ca2cb\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3637\" data-css=\"tve-u-194739cb10e\" data-height=\"255\" data-id=\"3637\" data-init-height=\"588\" data-init-width=\"1176\" data-width=\"510\" decoding=\"async\" height=\"255\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.02.11\u202fPM.png\" style=\"aspect-ratio: auto 1176 \/ 588;\" title=\"Screenshot 2025-01-17 at 3.02.11\u202fPM\" width=\"510\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0That means, the actual and expected errors do not match. Our intention is to deliberately fail the test by supplying an inaccurate <em>expectedErrorMessage<\/em>.\u00a0<\/p>\n<p>We than create an object of \u2018SoftAssert\u2019 and call the <em>assertEquals()<\/em> method (line#35).\u00a0<\/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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194739d1fba\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3638\" data-css=\"tve-u-194739d3323\" data-height=\"174\" data-id=\"3638\" data-init-height=\"370\" data-init-width=\"1176\" data-width=\"553\" decoding=\"async\" height=\"174\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.02.39\u202fPM.png\" style=\"aspect-ratio: auto 1176 \/ 370;\" title=\"Screenshot 2025-01-17 at 3.02.39\u202fPM\" width=\"553\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0The <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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194739d90e8\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3639\" data-css=\"tve-u-194739da595\" data-height=\"355\" data-id=\"3639\" data-init-height=\"796\" data-init-width=\"1176\" data-width=\"525\" decoding=\"async\" height=\"355\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.03.12\u202fPM.png\" style=\"aspect-ratio: auto 1176 \/ 796;\" title=\"Screenshot 2025-01-17 at 3.03.12\u202fPM\" width=\"525\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute.<\/p>\n<p>Notice that although the assertion failed, the test execution did not terminate in between. The line#37 still got executed\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194739e34a9\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3640\" data-css=\"tve-u-194739e465c\" data-height=\"659\" data-id=\"3640\" data-init-height=\"1410\" data-init-width=\"1176\" data-width=\"550\" decoding=\"async\" height=\"659\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.03.49\u202fPM.png\" style=\"aspect-ratio: auto 1176 \/ 1410;\" title=\"Screenshot 2025-01-17 at 3.03.49\u202fPM\" width=\"550\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<strong>Source code (soft assertion)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/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><br \/>\n<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><br \/>\n<strong>void<\/strong> setUp() {<\/p>\n<p>Playwright playwright = Playwright.<em>create<\/em>();\u00a0<\/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><br \/>\n<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><\/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><br \/>\n<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><\/p>\n<p>Thanks.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"tcb_flag\" style=\"display: none\"><\/div>\n<\/section>\n<p><\/body><\/html><\/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.\u00a0 Thus, in the event of a soft assert failure, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,9],"tags":[],"class_list":["post-3631","post","type-post","status-publish","format-standard","hentry","category-java","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":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3631\/revisions"}],"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}]}}