Skip to main content
Version: 1.15

Authentication

Playwright can be used to automate scenarios that require authentication.

Tests written with Playwright execute in isolated clean-slate environments called browser contexts. This isolation model improves reproducibility and prevents cascading test failures. New browser contexts can load existing authentication state. This eliminates the need to login in every context and speeds up test execution.

Note: This guide covers cookie/token-based authentication (logging in via the app UI). For HTTP authentication use browser.newContext([options]).

Automate logging in#

The Playwright API can automate interaction with a login form. See Input guide for more details.

The following example automates login on GitHub. Once these steps are executed, the browser context will be authenticated.

const page = await context.newPage();await page.goto('https://github.com/login');
// Interact with login formawait page.click('text=Login');await page.fill('input[name="login"]', USERNAME);await page.fill('input[name="password"]', PASSWORD);await page.click('text=Submit');// Verify app is logged in

These steps can be executed for every browser context. However, redoing login for every test can slow down test execution. To prevent that, we will reuse existing authentication state in new browser contexts.

Reuse authentication state#

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 prepopulated 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.

The following code snippet retrieves state from an authenticated context and creates a new context with that state.

// Save storage state into the file.await context.storageState({ path: 'state.json' });
// Create a new context with the saved storage state.const context = await browser.newContext({ storageState: 'state.json' });

Code generation#

Logging in via the UI and then reusing authentication state can be combined to implement login once and run multiple scenarios. The lifecycle looks like:

  1. Run tests (for example, with npm run test).
  2. Login via UI and retrieve authentication state.
  3. In each test, load authentication state in beforeEach or beforeAll step.

This approach will also work in CI environments, since it does not rely on any external state.

Reuse authentication in Playwright Test#

When using Playwright Test, you can log in once in the global setup and then reuse authentication state in tests. That way all your tests are completely isolated, yet you only waste time logging in once for the entire test suite run.

First, introduce the global setup that would sign in once. In this example we use the baseURL and storageState options from the configuration file.

// global-setup.tsimport { chromium, FullConfig } from '@playwright/test';
async function globalSetup(config: FullConfig) {  const { baseURL, storageState } = config.projects[0].use;  const browser = await chromium.launch();  const page = await browser.newPage();  await page.goto(baseURL!);  await page.fill('input[name="user"]', 'user');  await page.fill('input[name="password"]', 'password');  await page.click('text=Sign in');  await page.context().storageState({ path: storageState as string });  await browser.close();}
export default globalSetup;

Next specify globalSetup, baseURL and storageState in the configuration file.

// playwright.config.tsimport { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {  globalSetup: require.resolve('./global-setup'),  use: {    baseURL: 'http://localhost:3000/',    storageState: 'state.json',  },};export default config;

Tests start already authenticated because we specify storageState that was populated by global setup.

import { test } from '@playwright/test';
test('test', async ({ page }) => {  await page.goto('/');  // You are signed in!});

API reference#

Session storage#

Rarely, session storage is used for storing information associated with the logged-in state. Session storage is specific to a particular domain and is not persisted across page loads. Playwright does not provide API to persist session storage, but the following snippet can be used to save/load session storage.

// Get session storage and store as env variableconst sessionStorage = await page.evaluate(() => JSON.stringify(sessionStorage));process.env.SESSION_STORAGE = sessionStorage;
// Set session storage in a new contextconst sessionStorage = process.env.SESSION_STORAGE;await context.addInitScript(storage => {  if (window.location.hostname === 'example.com') {    const entries = JSON.parse(storage);    Object.keys(entries).forEach(key => {      window.sessionStorage.setItem(key, entries[key]);    });  }}, sessionStorage);

API reference#

Multi-factor authentication#

Accounts with multi-factor authentication (MFA) cannot be fully automated, and need manual intervention. Persistent authentication can be used to partially automate MFA scenarios.

Persistent authentication#

Note that persistent authentication is not suited for CI environments since it relies on a disk location. User data directories are specific to browser types and cannot be shared across browser types.

User data directories can be used with the browserType.launchPersistentContext(userDataDir[, options]) API.

const { chromium } = require('playwright');
const userDataDir = '/path/to/directory';const context = await chromium.launchPersistentContext(userDataDir, { headless: false });// Execute login steps manually in the browser window

Lifecycle#

  1. Create a user data directory on disk 2. Launch a persistent context with the user data directory and login the MFA account. 3. Reuse user data directory to run automation scenarios.

API reference#