{"id":3384,"date":"2025-01-16T07:57:47","date_gmt":"2025-01-16T07:57:47","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/browser-based-authentication-and-handling-shadow-root-elements-in-playwright-java\/"},"modified":"2025-01-16T07:57:47","modified_gmt":"2025-01-16T07:57:47","slug":"browser-based-authentication-and-handling-shadow-root-elements-in-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/browser-based-authentication-and-handling-shadow-root-elements-in-playwright-java\/","title":{"rendered":"Browser based Authentication and handling shadow root elements in 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=\"3384\" id=\"tve_editor\">\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><em>In this blog we will see how Playwright Java enables browser-based authentication and provides the capability to handle shadow root elements.<\/em><em>\u00a0<\/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>Handle browser based authentication<\/li>\n<li>Handle Shadow root elements<\/li>\n<li>Source code (browser based auth)<\/li>\n<li>Source code (shadow root)<\/li>\n<\/ul>\n<p><strong>Handle browser based authentication<\/strong><\/p>\n<p>Navigate to <a class=\"\" href=\"https:\/\/the-internet.herokuapp.com\/\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/the-internet.herokuapp.com\/<\/a>\u00a0<\/p>\n<p>You would see<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946e20bb91\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3387\" data-css=\"tve-u-1946e20e1ce\" data-height=\"278\" data-id=\"3387\" data-init-height=\"714\" data-init-width=\"1168\" data-width=\"454\" decoding=\"async\" height=\"278\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.28.44\u202fPM.png\" style=\"aspect-ratio: auto 1168 \/ 714;\" title=\"Screenshot 2025-01-16 at 1.28.44\u202fPM\" width=\"454\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0 Notice above. \u2018Sign in\u2019 popup asks user to enter login credentials.\u00a0<\/p>\n<p>The browser generates the authentication, making it a Browser-based Authentication example.\u00a0<\/p>\n<p>We cannot inspect the locators of username\/password fields. You cannot right click and inspect these elements. These are not webelements.<br \/>We have to handle these by another mechanism. Let us carry out the steps to accomplish this task.\u00a0<\/p>\n<p>We will proceed with the login process by providing the specific username and password, which in this case is set as &#8216;admin&#8217;<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946e2195ef\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3388\" data-css=\"tve-u-1946e21a6f3\" data-height=\"194\" data-id=\"3388\" data-init-height=\"448\" data-init-width=\"1168\" data-width=\"507\" decoding=\"async\" height=\"194\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.29.34\u202fPM.png\" style=\"aspect-ratio: auto 1168 \/ 448;\" title=\"Screenshot 2025-01-16 at 1.29.34\u202fPM\" width=\"507\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0The browser authenticates the login credentials, see below<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946e224d76\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3389\" data-css=\"tve-u-1946e225d2a\" data-height=\"617\" data-id=\"3389\" data-init-height=\"1340\" data-init-width=\"1168\" data-width=\"538\" decoding=\"async\" height=\"617\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.30.25\u202fPM.png\" style=\"aspect-ratio: auto 1168 \/ 1340;\" title=\"Screenshot 2025-01-16 at 1.30.25\u202fPM\" width=\"538\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0To handle this error we will create a new browser context and set the credentials as shown below<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946e22e6c0\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3390\" data-css=\"tve-u-1946e22f8aa\" data-height=\"585\" data-id=\"3390\" data-init-height=\"1290\" data-init-width=\"1168\" data-width=\"530\" decoding=\"async\" height=\"585\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.31.10\u202fPM.png\" style=\"aspect-ratio: auto 1168 \/ 1290;\" title=\"Screenshot 2025-01-16 at 1.31.10\u202fPM\" width=\"530\"\/><\/span><\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946e235dfb\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3391\" data-css=\"tve-u-1946e236c0c\" data-height=\"333\" data-id=\"3391\" data-init-height=\"690\" data-init-width=\"1168\" data-width=\"564\" decoding=\"async\" height=\"333\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.31.49\u202fPM.png\" style=\"aspect-ratio: auto 1168 \/ 690;\" title=\"Screenshot 2025-01-16 at 1.31.49\u202fPM\" width=\"564\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>There is no error now and the authentication is successful<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946e2401a7\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3392\" data-css=\"tve-u-1946e2414b4\" data-height=\"207\" data-id=\"3392\" data-init-height=\"458\" data-init-width=\"1168\" data-width=\"529\" decoding=\"async\" height=\"207\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.32.31\u202fPM.png\" style=\"aspect-ratio: auto 1168 \/ 458;\" title=\"Screenshot 2025-01-16 at 1.32.31\u202fPM\" width=\"529\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<strong>Handle Shadow root elements<\/strong><\/p>\n<p>Go to <a class=\"\" href=\"https:\/\/books-pwakit.appspot.com\/explore?q\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/books-pwakit.appspot.com\/explore?q<\/a>=<\/p>\n<p>Inspect search field<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946e24a9c3\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3393\" data-css=\"tve-u-1946e24c988\" data-height=\"650\" data-id=\"3393\" data-init-height=\"1520\" data-init-width=\"1186\" data-width=\"507\" decoding=\"async\" height=\"650\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.33.06\u202fPM.png\" style=\"aspect-ratio: auto 1186 \/ 1520;\" title=\"Screenshot 2025-01-16 at 1.33.06\u202fPM\" width=\"507\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0In selenium, it\u2019s quite cumbersome to locate this element. However using playwright we can simply locate the shadow root field and can type the text\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946e254343\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3394\" data-css=\"tve-u-1946e2554ba\" data-height=\"648\" data-id=\"3394\" data-init-height=\"1344\" data-init-width=\"1186\" data-width=\"572\" decoding=\"async\" height=\"648\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.33.53\u202fPM.png\" style=\"aspect-ratio: auto 1186 \/ 1344;\" title=\"Screenshot 2025-01-16 at 1.33.53\u202fPM\" width=\"572\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<strong>Source code (browser based auth)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Browser;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserContext;<\/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><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog22_BrowserBasedAuth {<\/p>\n<p><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>static<\/strong><br \/>\n<strong>void<\/strong> main(String[] args) {<\/p>\n<p>Playwright pw = Playwright.<em>create<\/em>();\u00a0<\/p>\n<p>Browser browser = pw.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<p>BrowserContext context = browser.newContext(<strong>new<\/strong> Browser.NewContextOptions().setHttpCredentials(&#8220;admin&#8221;,&#8221;admin&#8221;));<\/p>\n<p><\/p>\n<p>\/\/Page page = browser.newPage();<\/p>\n<p>Page page = context.newPage();<\/p>\n<p>page.navigate(&#8220;https:\/\/the-internet.herokuapp.com\/basic_auth&#8221;);<\/p>\n<p><\/p>\n<p>page.pause();<\/p>\n<p>page.close();<\/p>\n<p>context.close();<\/p>\n<p>browser.close();<\/p>\n<p>pw.close();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><\/p>\n<p><strong>Source code (shadow root)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/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><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog22_ShadowRoot {<\/p>\n<p><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>static<\/strong><br \/>\n<strong>void<\/strong> main(String[] args) {<\/p>\n<p>Playwright pw = Playwright.<em>create<\/em>();\u00a0<\/p>\n<p>Browser browser = pw.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<p><\/p>\n<p>Page page = browser.newPage();<\/p>\n<p>page.navigate(&#8220;https:\/\/books-pwakit.appspot.com\/explore?q=&#8221;);<\/p>\n<p>page.locator(&#8220;#input&#8221;).fill(&#8220;rahulshettyacademy.com&#8221;); \/\/shadow dom element<\/p>\n<p><\/p>\n<p>page.pause();<\/p>\n<p>page.close();<\/p>\n<p>browser.close();<\/p>\n<p>pw.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 see how Playwright Java enables browser-based authentication and provides the capability to handle shadow root elements.\u00a0 Topics that we will cover: Handle browser based authentication Handle Shadow root elements Source code (browser based auth) Source code (shadow root) Handle browser based authentication Navigate to https:\/\/the-internet.herokuapp.com\/\u00a0 You would see \u00a0 Notice [&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-3384","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\/3384","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=3384"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3384\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}