In this blog we will automatically authenticate by utilizing a Json file within Playwright-Java for seamless verification.
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 authenticate)
Automate simple login scenario
Navigate to https://www.saucedemo.com/

We will be taken to inventory page https://www.saucedemo.com/inventory.html

Click the icon. We will be taken to cart page https://www.saucedemo.com/cart.html


Let us automate only login steps as of now

Store login authentication steps in json file
We will now enhance our code and use the browser context to store the login uthorization steps in a json file

The cookies session can be seen inside the json file

Close json file. Note that we have ‘expires’ information stored as well in the above file. This means that cookie session expires after sometime.
Use json file to authenticate login functionality
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

So we will be using this json file to login the app.
In line#22 we are landing onto inventory page and to cart page in line#27.
Execute. Notice below that inventory and cart pages open without the need to login


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.
Source code (login using playwright)
package com.rsa.playwrightjava;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class Blog21_LoginSauceDemo {
public static void main(String[] args) {
Playwright pw = Playwright.create();
Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
BrowserContext context = browser.newContext();
Page page = context.newPage();
page.navigate("https://www.saucedemo.com/");
page.locator("[data-test=\"username\"]").click();
page.locator("[data-test=\"username\"]").fill("standard_user");
page.locator("[data-test=\"username\"]").press("Tab");
page.locator("[data-test=\"password\"]").fill("secret_sauce");
page.locator("[data-test=\"login-button\"]").click();
page.pause();
page.close();
context.close();
browser.close();
pw.close();
}
}
Source code (generate json)
package com.rsa.playwrightjava;
import java.nio.file.Paths;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class Blog21_AuthenticationCreateJson {
public static void main(String[] args) {
Playwright pw = Playwright.create();
Browser browser = pw.chromium().launch(new BrowserType
.LaunchOptions().setHeadless(false));
BrowserContext context = browser.newContext();
Page page = context.newPage();
page.navigate("https://www.saucedemo.com/");
page.locator("[data-test=\"username\"]").click();
page.locator("[data-test=\"username\"]").fill("standard_user");
page.locator("[data-test=\"username\"]").press("Tab");
page.locator("[data-test=\"password\"]").fill("secret_sauce");
page.locator("[data-test=\"login-button\"]").click();
//Save authorization data into a json file
context.storageState(new BrowserContext.StorageStateOptions().setPath(Paths.get("auth.json")));
page.close();
context.close();
browser.close();
pw.close();
}
}
Source code (using json to automatically authenticate)
package com.rsa.playwrightjava;
import java.nio.file.Paths;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class Blog21_AuthRunJson {
public static void main(String[] args) {
Playwright pw = Playwright.create();
Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.setStorageStatePath(Paths.get("auth.json")));
Page page = context.newPage();
System.out.println("Inside inventory page");
page.navigate("https://www.saucedemo.com/inventory.html");
page.pause();
System.out.println("Inside cart page");
page.navigate("https://www.saucedemo.com/cart.html");
page.pause();
page.close();
context.close();
browser.close();
pw.close();
}
}
Thanks.