{"id":3573,"date":"2025-01-17T08:47:08","date_gmt":"2025-01-17T08:47:08","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=3573"},"modified":"2025-01-17T10:55:45","modified_gmt":"2025-01-17T10:55:45","slug":"handle-checkboxes-using-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/17\/handle-checkboxes-using-playwright-java\/","title":{"rendered":"Handle Checkboxes using Playwright Java"},"content":{"rendered":"<p><em>In this blog we will be utilizing Playwright Java to handle checkboxes.<\/em><\/p>\n<p><strong>Topics that we will cover:<\/strong><\/p>\n<ul>\n<li>\u201c<em>check()<\/em>\u201d method to select a checkbox<\/li>\n<li>\u201c<em>locator()<\/em> method clubbed with <em>check()<\/em>\u201d method to select a checkbox<\/li>\n<li>Use the &#8220;<em>click()<\/em>&#8221; function to pick a checkbox<\/li>\n<li>\u201c<em>uncheck()<\/em>\u201d method to uncheck a checkbox<\/li>\n<li>\u201c<em>locator()<\/em> method clubbed with <em>click()<\/em>\u201d method to select a checkbox<\/li>\n<li>Source code<\/li>\n<\/ul>\n<p><strong>\u201c<\/strong><strong><em>check()<\/em><\/strong><strong>\u201d method to select a checkbox&nbsp;<\/strong><\/p>\n<p>Go to <a href=\"https:\/\/rahulshettyacademy.com\/AutomationPractice\/\" style=\"outline: none;\">https:\/\/rahulshettyacademy.com\/AutomationPractice\/<\/a>&nbsp;<\/p>\n<p>Observe below that none of the checkboxes are selected by default<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3576\" width=\"489\" data-init-width=\"1170\" height=\"189\" data-init-height=\"452\" title=\"Screenshot 2025-01-17 at 2.17.58\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.17.58\u202fPM.png\" data-width=\"489\" data-height=\"189\" style=\"aspect-ratio: auto 1170 \/ 452;\"><\/span><\/p>\n<p>&nbsp;Let us see how to select a checkbox, for example \u2018Option1\u2019.<\/p>\n<p>Let us inspect \u2018Option1\u2019<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3577\" width=\"502\" data-init-width=\"1170\" height=\"221\" data-init-height=\"514\" title=\"Screenshot 2025-01-17 at 2.18.37\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.18.37\u202fPM.png\" data-width=\"502\" data-height=\"221\" style=\"aspect-ratio: auto 1170 \/ 514;\"><\/span><\/p>\n<p>&nbsp;Let us use \u201ccheck()\u201d method to select the checkbox and pass the \u2018id\u2019 attribute (line#29)<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3580\" width=\"625\" data-init-width=\"1170\" height=\"783\" data-init-height=\"1466\" title=\"Screenshot 2025-01-17 at 2.19.43\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.19.43\u202fPM-2.png\" data-width=\"625\" data-height=\"783\" style=\"aspect-ratio: auto 1170 \/ 1466;\"><\/span><\/p>\n<p><strong>\u201c<\/strong><strong><em>locator()<\/em><\/strong><strong> method clubbed with <\/strong><strong><em>check()<\/em><\/strong><strong>\u201d method to select a checkbox<\/strong><\/p>\n<p>We can also use the \u201c<em>locator()<\/em>\u201d method clubbed with \u201c<em>check()<\/em>\u201d method to select a checkbox (line#32)&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3581\" width=\"498\" data-init-width=\"1170\" height=\"566\" data-init-height=\"1330\" title=\"Screenshot 2025-01-17 at 2.22.01\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.22.01\u202fPM.png\" data-width=\"498\" data-height=\"566\" style=\"aspect-ratio: auto 1170 \/ 1330;\"><\/span><\/p>\n<p>&nbsp;<strong>Use the &#8220;<\/strong><strong><em>click()<\/em><\/strong><strong>&#8221; function to pick a checkbox<\/strong><\/p>\n<p>We can also use the \u201c<em>click()<\/em>\u201d method to select a checkbox (see line#35)<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3582\" width=\"532\" data-init-width=\"1170\" height=\"643\" data-init-height=\"1414\" title=\"Screenshot 2025-01-17 at 2.22.41\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.22.41\u202fPM.png\" data-width=\"532\" data-height=\"643\" style=\"aspect-ratio: auto 1170 \/ 1414;\"><\/span><\/p>\n<p>&nbsp;<strong>\u201c<\/strong><strong><em>uncheck()<\/em><\/strong><strong>\u201d method to uncheck a checkbox<\/strong><\/p>\n<p>We can uncheck a checkbox using \u201c<em>uncheck()<\/em>\u201d method<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3583\" width=\"486\" data-init-width=\"1170\" height=\"467\" data-init-height=\"1124\" title=\"Screenshot 2025-01-17 at 2.23.26\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.23.26\u202fPM.png\" data-width=\"486\" data-height=\"467\" style=\"aspect-ratio: auto 1170 \/ 1124;\"><\/span><\/p>\n<p>&nbsp;<strong>\u201c<\/strong><strong><em>locator()<\/em><\/strong><strong> method clubbed with <\/strong><strong><em>click()<\/em><\/strong><strong>\u201d method to select a checkbox<\/strong><\/p>\n<p>We can also use the \u201c<em>locator()<\/em>\u201d method clubbed with \u201c<em>click()<\/em>\u201d method to select a checkbox<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3584\" width=\"766\" data-init-width=\"1170\" height=\"907\" data-init-height=\"1386\" title=\"Screenshot 2025-01-17 at 2.24.05\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-2.24.05\u202fPM.png\" data-width=\"766\" data-height=\"907\" style=\"aspect-ratio: auto 1170 \/ 1386;\"><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>So this is how we can use the various methods to select a checkbox.<\/p>\n<p><strong>Source code<\/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> com.microsoft.playwright.Browser;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Locator;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<\/p>\n<p><strong>public<\/strong><strong>class<\/strong> Blog33_Checkboxes_PWJava {<\/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:\/\/rahulshettyacademy.com\/AutomationPractice\/&#8221;);<\/p>\n<p>\/\/Method 1<\/p>\n<p>page.check(&#8220;[id=&#8217;checkBoxOption1&#8242;]&#8221;);<\/p>\n<p>page.uncheck(&#8220;[id=&#8217;checkBoxOption1&#8242;]&#8221;);<\/p>\n<p>\/\/Method 2<\/p>\n<p>page.locator(&#8220;[id=&#8217;checkBoxOption2&#8242;]&#8221;).check();<\/p>\n<p>\/\/Method 3<\/p>\n<p>page.click(&#8220;[id=&#8217;checkBoxOption3&#8242;]&#8221;);<\/p>\n<p>\/\/Method 4<\/p>\n<p>page.locator(&#8220;[id=&#8217;checkBoxOption1&#8242;]&#8221;).click();<\/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>Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog we will be utilizing Playwright Java to handle checkboxes. Topics that we will cover: \u201ccheck()\u201d method to select a checkbox \u201clocator() method clubbed with check()\u201d method to select a checkbox Use the &#8220;click()&#8221; function to pick a checkbox \u201cuncheck()\u201d method to uncheck a checkbox \u201clocator() method clubbed with click()\u201d method to select [&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-3573","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\/3573","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=3573"}],"version-history":[{"count":6,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3573\/revisions"}],"predecessor-version":[{"id":3752,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3573\/revisions\/3752"}],"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=3573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}