{"id":3369,"date":"2025-01-16T05:53:01","date_gmt":"2025-01-16T05:53:01","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=3369"},"modified":"2025-01-17T10:43:43","modified_gmt":"2025-01-17T10:43:43","slug":"authenticate-automatically-using-json-file-in-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/authenticate-automatically-using-json-file-in-playwright-java\/","title":{"rendered":"Authenticate automatically using Json file in Playwright-Java"},"content":{"rendered":"<p><em>In this blog we will automatically authenticate by utilizing a Json file within Playwright-Java for seamless verification.<\/em><em>&nbsp;<\/em><\/p>\n<p><strong>Topics that we will cover:<\/strong><\/p>\n<ul>\n<li>Automate simple login scenario<\/li>\n<li>Store login authentication steps in json file<\/li>\n<li>Use json file to authenticate login functionality<\/li>\n<li>Source code (login using playwright)<\/li>\n<li>Source code (generate json)<\/li>\n<li>Source code (using json to automatically authenticate)<\/li>\n<\/ul>\n<p><strong>Automate simple login scenario<\/strong><\/p>\n<p>Navigate to <a href=\"https:\/\/www.saucedemo.com\/\" style=\"outline: none;\" target=\"_blank\" rel=\"noopener\">https:\/\/www.saucedemo.com\/<\/a>&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3372\" width=\"479\" data-init-width=\"1160\" height=\"511\" data-init-height=\"1238\" title=\"Screenshot 2025-01-16 at 11.24.00\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.24.00\u202fAM.png\" data-width=\"479\" data-height=\"511\" style=\"aspect-ratio: auto 1160 \/ 1238;\"><\/span><\/p>\n<p>&nbsp;We will be taken to inventory page <a href=\"https:\/\/www.saucedemo.com\/inventory.html\" style=\"outline: none;\" target=\"_blank\" rel=\"noopener\">https:\/\/www.saucedemo.com\/inventory.html<\/a>&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3373\" width=\"513\" data-init-width=\"1168\" height=\"625\" data-init-height=\"1424\" title=\"Screenshot 2025-01-16 at 11.25.01\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.25.01\u202fAM.png\" data-width=\"513\" data-height=\"625\" style=\"aspect-ratio: auto 1168 \/ 1424;\"><\/span><\/p>\n<p>Click the icon. We will be taken to cart page <a href=\"https:\/\/www.saucedemo.com\/cart.html\" style=\"outline: none;\" target=\"_blank\" rel=\"noopener\">https:\/\/www.saucedemo.com\/cart.html<\/a>&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3374\" width=\"541\" data-init-width=\"1168\" height=\"48\" data-init-height=\"104\" title=\"Screenshot 2025-01-16 at 11.25.53\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.25.53\u202fAM.png\" data-width=\"541\" data-height=\"48\" style=\"aspect-ratio: auto 1168 \/ 104;\"><\/span><span><img decoding=\"async\" alt=\"\" data-id=\"3375\" width=\"479\" data-init-width=\"1168\" height=\"609\" data-init-height=\"1484\" title=\"Screenshot 2025-01-16 at 11.26.43\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.26.43\u202fAM.png\" data-width=\"479\" data-height=\"609\" style=\"aspect-ratio: auto 1168 \/ 1484;\"><\/span><\/p>\n<p>Let us automate only login steps as of now<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3376\" width=\"571\" data-init-width=\"1168\" height=\"840\" data-init-height=\"1718\" title=\"Screenshot 2025-01-16 at 11.27.35\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.27.35\u202fAM.png\" data-width=\"571\" data-height=\"840\" style=\"aspect-ratio: auto 1168 \/ 1718;\"><\/span><\/p>\n<p>&nbsp;<strong>Store login authentication steps in json file<\/strong><\/p>\n<p>We will now enhance our code and use the browser context to store the login uthorization steps in a json file<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3377\" width=\"563\" data-init-width=\"1168\" height=\"612\" data-init-height=\"1270\" title=\"Screenshot 2025-01-16 at 11.28.43\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.28.43\u202fAM.png\" data-width=\"563\" data-height=\"612\" style=\"aspect-ratio: auto 1168 \/ 1270;\"><\/span><\/p>\n<p>The cookies session can be seen inside the json file<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3378\" width=\"543\" data-init-width=\"1168\" height=\"132\" data-init-height=\"284\" title=\"Screenshot 2025-01-16 at 11.29.53\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.29.53\u202fAM.png\" data-width=\"543\" data-height=\"132\" style=\"aspect-ratio: auto 1168 \/ 284;\"><\/span><\/p>\n<p>&nbsp;Close json file. Note that we have \u2018expires\u2019 information stored as well in the above file. This means that cookie session expires after sometime.&nbsp;<\/p>\n<p><strong>Use json file to authenticate login functionality<\/strong><\/p>\n<p>See below, we are now creating a new browser using by calling the json file and than create a new page using this browser context<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3379\" width=\"536\" data-init-width=\"1168\" height=\"209\" data-init-height=\"456\" title=\"Screenshot 2025-01-16 at 11.30.37\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.30.37\u202fAM.png\" data-width=\"536\" data-height=\"209\" style=\"aspect-ratio: auto 1168 \/ 456;\"><\/span><\/p>\n<p>&nbsp;So we will be using this json file to login the app.<\/p>\n<p>In line#22 we are landing onto inventory page and to cart page in line#27.<\/p>\n<p>Execute. Notice below that inventory and cart pages open without the need to login<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3380\" width=\"531\" data-init-width=\"1168\" height=\"716\" data-init-height=\"1576\" title=\"Screenshot 2025-01-16 at 11.32.20\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.32.20\u202fAM.png\" data-width=\"531\" data-height=\"716\" style=\"aspect-ratio: auto 1168 \/ 1576;\"><\/span><span><img decoding=\"async\" alt=\"\" data-id=\"3381\" width=\"482\" data-init-width=\"1168\" height=\"87\" data-init-height=\"210\" title=\"Screenshot 2025-01-16 at 11.33.04\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.33.04\u202fAM.png\" data-width=\"482\" data-height=\"87\" style=\"aspect-ratio: auto 1168 \/ 210;\"><\/span><\/p>\n<p>&nbsp;This feature allows for the reuse of login code across various scripts, eliminating the need to rewrite the same code repeatedly. By calling the JSON file, we can streamline the process and improve efficiency.<\/p>\n<p><strong>Source code (login using playwright)<\/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.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>\n<p><strong>public<\/strong><strong>class<\/strong> Blog21_LoginSauceDemo {<\/p>\n<\/p>\n<p><strong>public<\/strong><strong>static<\/strong><strong>void<\/strong> main(String[] args) {<\/p>\n<p>Playwright pw = Playwright.<em>create<\/em>();<\/p>\n<p>Browser browser = pw.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<\/p>\n<p>BrowserContext context = browser.newContext();<\/p>\n<p>Page page = context.newPage();<\/p>\n<p>page.navigate(&#8220;https:\/\/www.saucedemo.com\/&#8221;);<\/p>\n<p>page.locator(&#8220;[data-test=&#8221;username&#8221;]&#8221;).click();<\/p>\n<p>&nbsp; &nbsp; page.locator(&#8220;[data-test=&#8221;username&#8221;]&#8221;).fill(&#8220;standard_user&#8221;);<\/p>\n<p>&nbsp; &nbsp; page.locator(&#8220;[data-test=&#8221;username&#8221;]&#8221;).press(&#8220;Tab&#8221;);<\/p>\n<p>&nbsp; &nbsp; page.locator(&#8220;[data-test=&#8221;password&#8221;]&#8221;).fill(&#8220;secret_sauce&#8221;);<\/p>\n<p>&nbsp; &nbsp; page.locator(&#8220;[data-test=&#8221;login-button&#8221;]&#8221;).click();<\/p>\n<p>&nbsp; &nbsp;<\/p>\n<p>&nbsp; &nbsp; page.pause();<\/p>\n<p>&nbsp; &nbsp; page.close();<\/p>\n<p>&nbsp; &nbsp; context.close();<\/p>\n<p>&nbsp; &nbsp; browser.close();<\/p>\n<p>&nbsp; &nbsp; pw.close();<\/p>\n<p>&nbsp; &nbsp;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/p>\n<p><strong>Source code (generate json)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<\/p>\n<p><strong>import<\/strong> java.nio.file.Paths;<\/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>\n<p><strong>public<\/strong><strong>class<\/strong> Blog21_AuthenticationCreateJson {<\/p>\n<\/p>\n<p><strong>public<\/strong><strong>static<\/strong><strong>void<\/strong> main(String[] args) {<\/p>\n<p>Playwright pw = Playwright.<em>create<\/em>();&nbsp;<\/p>\n<p>Browser browser = pw.chromium().launch(<strong>new<\/strong> BrowserType<\/p>\n<p>.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<\/p>\n<p>BrowserContext context = browser.newContext();&nbsp;<\/p>\n<p>Page page = context.newPage();&nbsp;<\/p>\n<p>page.navigate(&#8220;https:\/\/www.saucedemo.com\/&#8221;);<\/p>\n<p>page.locator(&#8220;[data-test=&#8221;username&#8221;]&#8221;).click();<\/p>\n<p>page.locator(&#8220;[data-test=&#8221;username&#8221;]&#8221;).fill(&#8220;standard_user&#8221;);<\/p>\n<p>page.locator(&#8220;[data-test=&#8221;username&#8221;]&#8221;).press(&#8220;Tab&#8221;);<\/p>\n<p>page.locator(&#8220;[data-test=&#8221;password&#8221;]&#8221;).fill(&#8220;secret_sauce&#8221;);<\/p>\n<p>page.locator(&#8220;[data-test=&#8221;login-button&#8221;]&#8221;).click();<\/p>\n<p>\/\/Save authorization data into a json file<\/p>\n<p>context.storageState(<strong>new<\/strong> BrowserContext.StorageStateOptions().setPath(Paths.<em>get<\/em>(&#8220;auth.json&#8221;)));&nbsp;<\/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>\n<p><strong>Source code (using json to automatically authenticate)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<\/p>\n<p><strong>import<\/strong> java.nio.file.Paths;<\/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>\n<p><strong>public<\/strong><strong>class<\/strong> Blog21_AuthRunJson {<\/p>\n<\/p>\n<p><strong>public<\/strong><strong>static<\/strong><strong>void<\/strong> main(String[] args) {<\/p>\n<p>Playwright pw = Playwright.<em>create<\/em>();<\/p>\n<p>Browser browser = pw.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<\/p>\n<p>BrowserContext context = browser.newContext(<strong>new<\/strong> Browser.NewContextOptions()<\/p>\n<p>.setStorageStatePath(Paths.<em>get<\/em>(&#8220;auth.json&#8221;)));<\/p>\n<p>Page page = context.newPage();<\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(&#8220;Inside inventory page&#8221;);<\/p>\n<p>page.navigate(&#8220;https:\/\/www.saucedemo.com\/inventory.html&#8221;);<\/p>\n<p>page.pause();<\/p>\n<p>System.<strong><em>out<\/em><\/strong>.println(&#8220;Inside cart page&#8221;);<\/p>\n<p>page.navigate(&#8220;https:\/\/www.saucedemo.com\/cart.html&#8221;);<\/p>\n<p>page.pause();<\/p>\n<\/p>\n<p>page.close();<\/p>\n<p>&nbsp; &nbsp; context.close();<\/p>\n<p>&nbsp; &nbsp; browser.close();<\/p>\n<p>&nbsp; &nbsp; pw.close();<\/p>\n<p>&nbsp; &nbsp;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/p>\n<p>Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog we will automatically authenticate by utilizing a Json file within Playwright-Java for seamless verification.&nbsp; Topics that we will cover: Automate simple login scenario Store login authentication steps in json file Use json file to authenticate login functionality Source code (login using playwright) Source code (generate json) Source code (using json to automatically [&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-3369","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\/3369","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=3369"}],"version-history":[{"count":6,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3369\/revisions"}],"predecessor-version":[{"id":3728,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3369\/revisions\/3728"}],"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=3369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}