Friday, September 26, 2025

Reuse authenticated browser state for others tests in Playwright

 Perform following steps,

  1. Create folder playwright/.auth outside tests
  2. Create folder setup inside tests
  3. Create a test file inside setup folder "file_name.setup.ts"
    1. Read userID and password from a file
    2. Login and write the storage state in user.json file
      • File directory will be "playwright/.auth/user.json"
    3. await page.context().storageState({path: above_dir_path_var})
  4. Create a project in playwright.config.ts
    • { name: 'auth-setup', testMatch: 'ts_file_name_of_auth_setup' }
  5. Add dependency and storage state in working project(s)
    • use: { ..., storageState: 'directory_of_user.json' }, dependencies: ['name_of_auth_project']

No comments:

Post a Comment