{"id":3669,"date":"2025-01-17T09:53:08","date_gmt":"2025-01-17T09:53:08","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=3669"},"modified":"2025-01-17T11:02:19","modified_gmt":"2025-01-17T11:02:19","slug":"assert-checkboxes-usage-of-blur-and-clear-methods-in-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/17\/assert-checkboxes-usage-of-blur-and-clear-methods-in-playwright-java\/","title":{"rendered":"Assert Checkboxes, usage of \u201cblur\u201d and \u201cclear\u201d methods in Playwright Java\u00a0"},"content":{"rendered":"<p><em>In this blog we will be utilizing Playwright Java to assert checkboxes and we will also see the usage of blur() and clear() methods.<\/em><\/p>\n<p><strong>Topics that we will cover:<\/strong><\/p>\n<ul>\n<li>Assert checkboxes (using <em>assertThat()<\/em> method)<\/li>\n<li>Assert if checkbox is visible<\/li>\n<li>Usage of <em>blur()<\/em> method<\/li>\n<li>Usage of <em>clear()<\/em><em> <\/em>method<\/li>\n<li>Source code (assert checkboxes)<\/li>\n<li>Source code (blur, clear)<\/li>\n<\/ul>\n<p><strong>Assert checkboxes (using <\/strong><strong><em>assertThat()<\/em><\/strong><strong> method)<\/strong><\/p>\n<p>We will now see how to assert using <em>assertThat()<\/em> method. To utilize <em>assertThat()<\/em>, we have to first import the static package:&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3672\" width=\"605\" data-init-width=\"1176\" height=\"37\" data-init-height=\"72\" title=\"Screenshot 2025-01-17 at 3.26.13\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.26.13\u202fPM.png\" data-width=\"605\" data-height=\"37\" style=\"aspect-ratio: auto 1176 \/ 72;\"><\/span><\/p>\n<p>&nbsp;Go to <a href=\"http:\/\/www.tizag.com\/htmlT\/htmlcheckboxes.php\" style=\"outline: none;\" target=\"_blank\" rel=\"noopener\">http:\/\/www.tizag.com\/htmlT\/htmlcheckboxes.php<\/a>&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3673\" width=\"527\" data-init-width=\"1176\" height=\"755\" data-init-height=\"1684\" title=\"Screenshot 2025-01-17 at 3.27.35\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.27.35\u202fPM.png\" data-width=\"527\" data-height=\"755\" style=\"aspect-ratio: auto 1176 \/ 1684;\"><\/span><\/p>\n<p>&nbsp;Below we can see that \u2018Soccer\u2019 checkbox is checked by default<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3674\" width=\"529\" data-init-width=\"1176\" height=\"681\" data-init-height=\"1514\" title=\"Screenshot 2025-01-17 at 3.28.54\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.28.54\u202fPM.png\" data-width=\"529\" data-height=\"681\" style=\"aspect-ratio: auto 1176 \/ 1514;\"><\/span><\/p>\n<p>&nbsp;Execute.&nbsp;<\/p>\n<p>All assertions pass<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3675\" width=\"546\" data-init-width=\"1176\" height=\"748\" data-init-height=\"1610\" title=\"Screenshot 2025-01-17 at 3.29.41\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.29.41\u202fPM.png\" data-width=\"546\" data-height=\"748\" style=\"aspect-ratio: auto 1176 \/ 1610;\"><\/span><\/p>\n<p>&nbsp;Execute.<\/p>\n<p>As expected, we get assertion error<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3676\" width=\"522\" data-init-width=\"1176\" height=\"684\" data-init-height=\"1540\" title=\"Screenshot 2025-01-17 at 3.41.06\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.41.06\u202fPM.png\" data-width=\"522\" data-height=\"684\" style=\"aspect-ratio: auto 1176 \/ 1540;\"><\/span><\/p>\n<p>Execute, the assertion passes<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3677\" width=\"509\" data-init-width=\"1176\" height=\"426\" data-init-height=\"984\" title=\"Screenshot 2025-01-17 at 3.41.49\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.41.49\u202fPM.png\" data-width=\"509\" data-height=\"426\" style=\"aspect-ratio: auto 1176 \/ 984;\"><\/span><\/p>\n<p>As expected, the letters were inputted by typing in the search field. Playwright focussed on the search field by default<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3679\" width=\"524\" data-init-width=\"1176\" height=\"305\" data-init-height=\"684\" title=\"Screenshot 2025-01-17 at 3.42.40\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.42.40\u202fPM.png\" data-width=\"524\" data-height=\"305\" style=\"aspect-ratio: auto 1176 \/ 684;\"><\/span><\/p>\n<p>&nbsp;The focus on the search field can be eliminated by utilizing the &#8216;blur&#8217; method (see line#34)<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3680\" width=\"601\" data-init-width=\"1176\" height=\"275\" data-init-height=\"538\" title=\"Screenshot 2025-01-17 at 3.43.15\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.43.15\u202fPM.png\" data-width=\"601\" data-height=\"275\" style=\"aspect-ratio: auto 1176 \/ 538;\"><\/span><\/p>\n<p>&nbsp;Execute.&nbsp;<\/p>\n<p>The script failed to input the letter \u2018e\u2019 due to the search field losing focus<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3681\" width=\"586\" data-init-width=\"1176\" height=\"460\" data-init-height=\"924\" title=\"Screenshot 2025-01-17 at 3.43.47\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.43.47\u202fPM.png\" data-width=\"586\" data-height=\"460\" style=\"aspect-ratio: auto 1176 \/ 924;\"><\/span><\/p>\n<p>&nbsp;Execute.&nbsp;<\/p>\n<p>The text gets entered and than gets cleared<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3682\" width=\"563\" data-init-width=\"1176\" height=\"147\" data-init-height=\"308\" title=\"Screenshot 2025-01-17 at 3.44.33\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.44.33\u202fPM.png\" data-width=\"563\" data-height=\"147\" style=\"aspect-ratio: auto 1176 \/ 308;\"><\/span><span><img decoding=\"async\" alt=\"\" data-id=\"3683\" width=\"527\" data-init-width=\"1176\" height=\"237\" data-init-height=\"528\" title=\"Screenshot 2025-01-17 at 3.45.10\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-3.45.10\u202fPM.png\" data-width=\"527\" data-height=\"237\" style=\"aspect-ratio: auto 1176 \/ 528;\"><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Source code (assert checkboxes)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<strong>import<\/strong><strong>static<\/strong> com.microsoft.playwright.assertions.PlaywrightAssertions.<em>assertThat<\/em>;<strong>import<\/strong> org.testng.annotations.AfterMethod;<strong>import<\/strong> org.testng.annotations.BeforeMethod;<strong>import<\/strong> org.testng.annotations.Test;<strong>import<\/strong> com.microsoft.playwright.Browser;<strong>import<\/strong> com.microsoft.playwright.BrowserType;<strong>import<\/strong> com.microsoft.playwright.Page;<strong>import<\/strong> com.microsoft.playwright.Playwright;<strong>public<\/strong><strong>class<\/strong> Blog40_AssertCheckboxes {<strong>private<\/strong> Browser browser;<strong>private<\/strong> Page page;@BeforeMethod<strong>public<\/strong><strong>void<\/strong> setUp() {Playwright playwright = Playwright.<em>create<\/em>();&nbsp;browser = playwright.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));page = browser.newPage();}@Test<strong>public<\/strong><strong>void<\/strong> test_1() {page.navigate(&#8220;http:\/\/www.tizag.com\/htmlT\/htmlcheckboxes.php&#8221;);\/\/assert url<em>assertThat<\/em>(page).hasURL(&#8220;http:\/\/www.tizag.com\/htmlT\/htmlcheckboxes.php&#8221;);\/\/assert title<em>assertThat<\/em>(page).hasTitle(&#8220;HTML Tutorial &#8211; Checkboxes&#8221;);\/\/assert link text<em>assertThat<\/em>(page.locator(&#8220;#menu &gt; a:nth-child(38)&#8221;)).hasText(&#8220;HTML &#8211; Images&#8221;);\/\/assert whether checkbox is checked<em>assertThat<\/em>(page.locator(&#8220;\/\/html\/body\/table[3]\/tbody\/tr[1]\/td[2]\/table\/tbody\/tr\/td\/div[6]\/input[1]&#8221;)).isChecked();<em>assertThat<\/em>(page.locator(&#8220;body &gt; table:nth-child(3) &gt; tbody &gt; tr:nth-child(1) &gt; td:nth-child(2) &gt; table &gt; tbody &gt; tr &gt; td &gt; div:nth-child(15) &gt; input[type=checkbox]:nth-child(6)&#8221;)).isChecked();\/\/assert whether checkbox is visible<em>assertThat<\/em>(page.locator(&#8220;body &gt; table:nth-child(3) &gt; tbody &gt; tr:nth-child(1) &gt; td:nth-child(2) &gt; table &gt; tbody &gt; tr &gt; td &gt; div:nth-child(15) &gt; input[type=checkbox]:nth-child(6)&#8221;)).isVisible();}@AfterMethod<strong>public<\/strong><strong>void<\/strong> tearDown() {page.pause();browser.close();page.close();}}<strong>Source code (blur, clear)<\/strong><strong>package<\/strong> com.rsa.playwrightjava;<strong>import<\/strong> org.testng.annotations.AfterMethod;<strong>import<\/strong> org.testng.annotations.BeforeMethod;<strong>import<\/strong> org.testng.annotations.Test;<strong>import<\/strong> com.microsoft.playwright.Browser;<strong>import<\/strong> com.microsoft.playwright.BrowserType;<strong>import<\/strong> com.microsoft.playwright.Page;<strong>import<\/strong> com.microsoft.playwright.Playwright;<strong>public<\/strong><strong>class<\/strong> Blog40_blur_clear {<strong>private<\/strong> Browser browser;<strong>private<\/strong> Page page;@BeforeMethod<strong>public<\/strong><strong>void<\/strong> setUp() {Playwright playwright = Playwright.<em>create<\/em>();&nbsp;browser = playwright.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));page = browser.newPage();}@Test<strong>public<\/strong><strong>void<\/strong> test_1() {page.navigate(&#8220;https:\/\/www.google.com\/&#8221;);page.keyboard().press(&#8220;r&#8221;);page.keyboard().press(&#8220;a&#8221;);page.keyboard().press(&#8220;h&#8221;);page.keyboard().press(&#8220;u&#8221;);page.keyboard().press(&#8220;l&#8221;);page.keyboard().press(&#8220;s&#8221;);page.keyboard().press(&#8220;h&#8221;);page.locator(&#8220;#APjFqb&#8221;).blur();page.keyboard().press(&#8220;e&#8221;);page.getByTitle(&#8220;Search&#8221;).fill(&#8220;https:\/\/rahulshettyacademy.com\/&#8221;);<strong>try<\/strong> {Thread.<em>sleep<\/em>(5000);} <strong>catch<\/strong> (InterruptedException e) {e.printStackTrace();}page.getByTitle(&#8220;Search&#8221;).clear();}@AfterMethod<strong>public<\/strong><strong>void<\/strong> tearDown() {page.pause();browser.close();page.close();}}Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog we will be utilizing Playwright Java to assert checkboxes and we will also see the usage of blur() and clear() methods. Topics that we will cover: Assert checkboxes (using assertThat() method) Assert if checkbox is visible Usage of blur() method Usage of clear() method Source code (assert checkboxes) Source code (blur, clear) [&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-3669","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\/3669","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=3669"}],"version-history":[{"count":6,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3669\/revisions"}],"predecessor-version":[{"id":3767,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3669\/revisions\/3767"}],"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=3669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}