{"id":3369,"date":"2025-01-16T05:53:01","date_gmt":"2025-01-16T05:53:01","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/authenticate-automatically-using-json-file-in-playwright-java\/"},"modified":"2025-01-16T05:53:01","modified_gmt":"2025-01-16T05:53:01","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><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=\"3369\" id=\"tve_editor\">\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><em>In this blog we will automatically authenticate by utilizing a Json file within Playwright-Java for seamless verification.<\/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>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 class=\"\" href=\"https:\/\/www.saucedemo.com\/\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/www.saucedemo.com\/<\/a>\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946daed983\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3372\" data-css=\"tve-u-1946daeedcf\" data-height=\"511\" data-id=\"3372\" data-init-height=\"1238\" data-init-width=\"1160\" data-width=\"479\" decoding=\"async\" height=\"511\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.24.00\u202fAM.png\" style=\"aspect-ratio: auto 1160 \/ 1238;\" title=\"Screenshot 2025-01-16 at 11.24.00\u202fAM\" width=\"479\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0We will be taken to inventory page <a class=\"\" href=\"https:\/\/www.saucedemo.com\/inventory.html\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/www.saucedemo.com\/inventory.html<\/a>\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946daf8f93\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3373\" data-css=\"tve-u-1946daf9e81\" data-height=\"625\" data-id=\"3373\" data-init-height=\"1424\" data-init-width=\"1168\" data-width=\"513\" decoding=\"async\" height=\"625\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.25.01\u202fAM.png\" style=\"aspect-ratio: auto 1168 \/ 1424;\" title=\"Screenshot 2025-01-16 at 11.25.01\u202fAM\" width=\"513\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>Click the icon. We will be taken to cart page <a class=\"\" href=\"https:\/\/www.saucedemo.com\/cart.html\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/www.saucedemo.com\/cart.html<\/a>\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946db059f9\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3374\" data-css=\"tve-u-1946db074b3\" data-height=\"48\" data-id=\"3374\" data-init-height=\"104\" data-init-width=\"1168\" data-width=\"541\" decoding=\"async\" height=\"48\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.25.53\u202fAM.png\" style=\"aspect-ratio: auto 1168 \/ 104;\" title=\"Screenshot 2025-01-16 at 11.25.53\u202fAM\" width=\"541\"\/><\/span><\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946db14c19\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3375\" data-css=\"tve-u-1946db16034\" data-height=\"609\" data-id=\"3375\" data-init-height=\"1484\" data-init-width=\"1168\" data-width=\"479\" decoding=\"async\" height=\"609\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.26.43\u202fAM.png\" style=\"aspect-ratio: auto 1168 \/ 1484;\" title=\"Screenshot 2025-01-16 at 11.26.43\u202fAM\" width=\"479\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>Let us automate only login steps as of now<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946db1e62b\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3376\" data-css=\"tve-u-1946db20441\" data-height=\"840\" data-id=\"3376\" data-init-height=\"1718\" data-init-width=\"1168\" data-width=\"571\" decoding=\"async\" height=\"840\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.27.35\u202fAM.png\" style=\"aspect-ratio: auto 1168 \/ 1718;\" title=\"Screenshot 2025-01-16 at 11.27.35\u202fAM\" width=\"571\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946db3850f\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3377\" data-css=\"tve-u-1946db39ac3\" data-height=\"612\" data-id=\"3377\" data-init-height=\"1270\" data-init-width=\"1168\" data-width=\"563\" decoding=\"async\" height=\"612\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.28.43\u202fAM.png\" style=\"aspect-ratio: auto 1168 \/ 1270;\" title=\"Screenshot 2025-01-16 at 11.28.43\u202fAM\" width=\"563\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>The cookies session can be seen inside the json file<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946db438a4\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3378\" data-css=\"tve-u-1946db44c6f\" data-height=\"132\" data-id=\"3378\" data-init-height=\"284\" data-init-width=\"1168\" data-width=\"543\" decoding=\"async\" height=\"132\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.29.53\u202fAM.png\" style=\"aspect-ratio: auto 1168 \/ 284;\" title=\"Screenshot 2025-01-16 at 11.29.53\u202fAM\" width=\"543\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Close json file. Note that we have \u2018expires\u2019 information stored as well in the above file. This means that cookie session expires after sometime.\u00a0<\/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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946db49ff8\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3379\" data-css=\"tve-u-1946db4b021\" data-height=\"209\" data-id=\"3379\" data-init-height=\"456\" data-init-width=\"1168\" data-width=\"536\" decoding=\"async\" height=\"209\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.30.37\u202fAM.png\" style=\"aspect-ratio: auto 1168 \/ 456;\" title=\"Screenshot 2025-01-16 at 11.30.37\u202fAM\" width=\"536\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0So 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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946db647e6\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3380\" data-css=\"tve-u-1946db6651c\" data-height=\"716\" data-id=\"3380\" data-init-height=\"1576\" data-init-width=\"1168\" data-width=\"531\" decoding=\"async\" height=\"716\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.32.20\u202fAM.png\" style=\"aspect-ratio: auto 1168 \/ 1576;\" title=\"Screenshot 2025-01-16 at 11.32.20\u202fAM\" width=\"531\"\/><\/span><\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946db6e63b\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3381\" data-css=\"tve-u-1946db6f4e9\" data-height=\"87\" data-id=\"3381\" data-init-height=\"210\" data-init-width=\"1168\" data-width=\"482\" decoding=\"async\" height=\"87\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-11.33.04\u202fAM.png\" style=\"aspect-ratio: auto 1168 \/ 210;\" title=\"Screenshot 2025-01-16 at 11.33.04\u202fAM\" width=\"482\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0This 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><\/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> Blog21_LoginSauceDemo {<\/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>();<\/p>\n<p>Browser browser = pw.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<p><\/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>\u00a0 \u00a0 page.locator(&#8220;[data-test=&#8221;username&#8221;]&#8221;).fill(&#8220;standard_user&#8221;);<\/p>\n<p>\u00a0 \u00a0 page.locator(&#8220;[data-test=&#8221;username&#8221;]&#8221;).press(&#8220;Tab&#8221;);<\/p>\n<p>\u00a0 \u00a0 page.locator(&#8220;[data-test=&#8221;password&#8221;]&#8221;).fill(&#8220;secret_sauce&#8221;);<\/p>\n<p>\u00a0 \u00a0 page.locator(&#8220;[data-test=&#8221;login-button&#8221;]&#8221;).click();<\/p>\n<p>\u00a0 \u00a0<\/p>\n<p>\u00a0 \u00a0 page.pause();<\/p>\n<p>\u00a0 \u00a0 page.close();<\/p>\n<p>\u00a0 \u00a0 context.close();<\/p>\n<p>\u00a0 \u00a0 browser.close();<\/p>\n<p>\u00a0 \u00a0 pw.close();<\/p>\n<p>\u00a0 \u00a0<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><\/p>\n<p><strong>Source code (generate json)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/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><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog21_AuthenticationCreateJson {<\/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<\/p>\n<p>.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<p><\/p>\n<p>BrowserContext context = browser.newContext();\u00a0<\/p>\n<p>Page page = context.newPage();\u00a0<\/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;)));\u00a0<\/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 (using json to automatically authenticate)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/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><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog21_AuthRunJson {<\/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>();<\/p>\n<p>Browser browser = pw.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<p><\/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><\/p>\n<p>page.close();<\/p>\n<p>\u00a0 \u00a0 context.close();<\/p>\n<p>\u00a0 \u00a0 browser.close();<\/p>\n<p>\u00a0 \u00a0 pw.close();<\/p>\n<p>\u00a0 \u00a0<\/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 automatically authenticate by utilizing a Json file within Playwright-Java for seamless verification.\u00a0 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":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,9],"tags":[],"class_list":["post-3369","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\/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":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3369\/revisions"}],"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}]}}