{"id":3459,"date":"2025-01-16T18:55:58","date_gmt":"2025-01-16T18:55:58","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=3459"},"modified":"2025-01-17T10:47:39","modified_gmt":"2025-01-17T10:47:39","slug":"javascript-dialogs-handling-using-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/javascript-dialogs-handling-using-playwright-java\/","title":{"rendered":"Javascript dialogs handling using Playwright Java"},"content":{"rendered":"<p><em>In this blog we will be utilizing Playwright Java to handle javascript dialogs (alerts, popups, prompts).<\/em><\/p>\n<p><strong>Topics that we will cover:<\/strong><\/p>\n<ul>\n<li>Handle Javascript dialogs (example 1)<\/li>\n<li>Handle Javascript dialogs (example 2)<\/li>\n<li>Source code (example 1)<\/li>\n<li>Source code (example 2)<\/li>\n<\/ul>\n<p><strong>Handle Javascript dialogs (example 1)<\/strong><\/p>\n<p>Go to <a href=\"https:\/\/the-internet.herokuapp.com\/javascript_alerts\" style=\"outline: none;\" target=\"_blank\" rel=\"noopener\">https:\/\/the-internet.herokuapp.com\/javascript_alerts<\/a>&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3462\" width=\"504\" data-init-width=\"1198\" height=\"354\" data-init-height=\"842\" title=\"Screenshot 2025-01-17 at 12.27.04\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.27.04\u202fAM.png\" data-width=\"504\" data-height=\"354\" style=\"aspect-ratio: auto 1198 \/ 842;\"><\/span><\/p>\n<p>To handle this, we can add \u201c<strong><em>onDialog<\/em><\/strong>\u201d listener<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3463\" width=\"540\" data-init-width=\"1198\" height=\"268\" data-init-height=\"594\" title=\"Screenshot 2025-01-17 at 12.28.23\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.28.23\u202fAM.png\" data-width=\"540\" data-height=\"268\" style=\"aspect-ratio: auto 1198 \/ 594;\"><\/span><\/p>\n<p>&nbsp;Whenever the javascript dialog\/alert appears, this particular \u201c<strong><em>onDialog<\/em><\/strong>\u201d listener will be invoked.<\/p>\n<p>In line#18, we have passed the lambda function inside the method parameter.<\/p>\n<p>In line#26, we are clicking the \u2018Ok\u2019 button by calling the accept method.&nbsp;<\/p>\n<p>In line#28, we are printing the message on the dialog box.<\/p>\n<p>In line#31, we are clicking the button to open the alert dialog. Notice that we have placed this after the listener code. So it does not matter where we keep the \u2018click\u2019 method. The listener keeps listening for any alert.<\/p>\n<p>Execute.<\/p>\n<p>Notice below that alert comes up<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3464\" width=\"529\" data-init-width=\"1198\" height=\"588\" data-init-height=\"1332\" title=\"Screenshot 2025-01-17 at 12.29.26\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.29.26\u202fAM.png\" data-width=\"529\" data-height=\"588\" style=\"aspect-ratio: auto 1198 \/ 1332;\"><\/span><\/p>\n<p>We add the lines#32 and 33.&nbsp;<\/p>\n<p>Rest of the logic would remain same<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3465\" width=\"552\" data-init-width=\"1198\" height=\"569\" data-init-height=\"1234\" title=\"Screenshot 2025-01-17 at 12.30.37\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.30.37\u202fAM.png\" data-width=\"552\" data-height=\"569\" style=\"aspect-ratio: auto 1198 \/ 1234;\"><\/span><\/p>\n<p><strong>Handle Javascript dialogs (example 2)<\/strong><\/p>\n<p>Let us see another example, go to <a href=\"https:\/\/mail.rediff.com\/cgi-bin\/login.cgi\" style=\"outline: none;\" target=\"_blank\" rel=\"noopener\">https:\/\/mail.rediff.com\/cgi-bin\/login.cgi<\/a>&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3466\" width=\"521\" data-init-width=\"1198\" height=\"333\" data-init-height=\"766\" title=\"Screenshot 2025-01-17 at 12.31.28\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.31.28\u202fAM.png\" data-width=\"521\" data-height=\"333\" style=\"aspect-ratio: auto 1198 \/ 766;\"><\/span><\/p>\n<p>The below code will click the \u2018Ok\u2019 button and will capture the text of this alert box<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3467\" width=\"545\" data-init-width=\"1198\" height=\"313\" data-init-height=\"688\" title=\"Screenshot 2025-01-17 at 12.32.04\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.32.04\u202fAM.png\" data-width=\"545\" data-height=\"313\" style=\"aspect-ratio: auto 1198 \/ 688;\"><\/span><\/p>\n<p>&nbsp;Execute.<\/p>\n<p>The popup comes up<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3468\" width=\"573\" data-init-width=\"1198\" height=\"342\" data-init-height=\"716\" title=\"Screenshot 2025-01-17 at 12.32.53\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.32.53\u202fAM.png\" data-width=\"573\" data-height=\"342\" style=\"aspect-ratio: auto 1198 \/ 716;\"><\/span><\/p>\n<p>&nbsp;<strong>Source code (example 1)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<\/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>\n<p><strong>public<\/strong><strong>class<\/strong> Blog26_JSDialogs_Example1 {<\/p>\n<p><strong>public<\/strong><strong>static<\/strong><strong>void<\/strong> main(String[] args) {<\/p>\n<p>Playwright playwright = Playwright.<em>create<\/em>();&nbsp;<\/p>\n<p>Browser browser = playwright.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<\/p>\n<p>Page page = browser.newPage();<\/p>\n<p>page.navigate(&#8220;https:\/\/the-internet.herokuapp.com\/javascript_alerts&#8221;);<\/p>\n<p>\/\/JS Alert, confirm, prompt<\/p>\n<p>page.onDialog(dialog -&gt; {<\/p>\n<p><strong>try<\/strong> {<\/p>\n<p>Thread.<em>sleep<\/em>(3000);<\/p>\n<p>}<strong>catch<\/strong> (InterruptedException e) {<\/p>\n<p>e.printStackTrace();<\/p>\n<p>}<\/p>\n<p>dialog.accept();<\/p>\n<\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(dialog.message());<\/p>\n<p>});<\/p>\n<\/p>\n<p>page.locator(&#8220;text=Click for JS Alert&#8221;).click();<\/p>\n<p>page.locator(&#8220;text=Click for JS Confirm&#8221;).click();<\/p>\n<p>page.locator(&#8220;text=Click for JS Prompt&#8221;).click();<\/p>\n<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/p>\n<p><strong>Source code (example 2)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<\/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>\n<p><strong>public<\/strong><strong>class<\/strong> Blog26_JSDialogs_Example2 {<\/p>\n<p><strong>public<\/strong><strong>static<\/strong><strong>void<\/strong> main(String[] args) {<\/p>\n<p>Playwright playwright = Playwright.<em>create<\/em>();&nbsp;<\/p>\n<p>Browser browser = playwright.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<\/p>\n<p>Page page = browser.newPage();<\/p>\n<p>page.navigate(&#8220;https:\/\/mail.rediff.com\/cgi-bin\/login.cgi&#8221;);<\/p>\n<p>\/\/JS Alert<\/p>\n<p>page.onDialog(dialog -&gt; {<\/p>\n<p><strong>try<\/strong> {<\/p>\n<p>Thread.<em>sleep<\/em>(3000);<\/p>\n<p>}<strong>catch<\/strong> (InterruptedException e) {<\/p>\n<p>e.printStackTrace();<\/p>\n<p>}<\/p>\n<p>dialog.accept();<\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(dialog.message());<\/p>\n<p>});<\/p>\n<p>page.locator(&#8220;text=Sign in&#8221;).click();<\/p>\n<\/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 handle javascript dialogs (alerts, popups, prompts). Topics that we will cover: Handle Javascript dialogs (example 1) Handle Javascript dialogs (example 2) Source code (example 1) Source code (example 2) Handle Javascript dialogs (example 1) Go to https:\/\/the-internet.herokuapp.com\/javascript_alerts&nbsp; To handle this, we can add \u201conDialog\u201d listener [&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-3459","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\/3459","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=3459"}],"version-history":[{"count":6,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3459\/revisions"}],"predecessor-version":[{"id":3737,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3459\/revisions\/3737"}],"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=3459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}