Back to Browse

Playwright Tutorial: Re-use state & Re-use Authentication

57.1K views
Mar 12, 2023
16:33

Playwright provides a way to reuse the signed-in state in the tests. That way you can log in only once and then skip the log in step for all of the tests. Web apps use cookie-based or token-based authentication, where authenticated state is stored as cookies or in local storage. Playwright provides browserContext.storageState([options]) method that can be used to retrieve storage state from authenticated contexts and then create new contexts with pre-populated state. Cookies and local storage state can be used across different browsers. They depend on your application's authentication model: some apps might require both cookies and local storage. Code Generator video: https://youtu.be/CZb-jjA3P8Y Code to use: await page.goto("https://demoblaze.com/"); await page.locator("#login2").click(); await page.locator("#loginusername").fill("test"); await page.locator("#loginpassword").fill("test"); await page.locator('[onclick="logIn()"]').click(); await expect(page.locator("#logout2")).toBeVisible();

Download

1 formats

Video Formats

360pmp421.2 MB

Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.

Playwright Tutorial: Re-use state & Re-use Authentication | NatokHD