Rayrun
← Back to Discord Forum

Authentication state is loaded but only for the first run of worker processes

Hello there, I have gone through the official documentation to authenticate users (https://playwright.dev/docs/auth) and to prepare the browser state for all tests and, it does work fine.

My problem is when a worker finished running a test successfully and it's reused to run a new test, this other test does not load the authentication state. What can I do to make sure I'm loading the authentication file every time a test runs?

I need one account across all tests.

Thank you all!

This thread is trying to answer question "What can I do to make sure I'm loading the authentication file every time a test runs in Playwright?"

17 replies

@_ed.s

Refer playwright.config.ts // Use prepared auth state. storageState: 'playwright/.auth/user.json',

Hi @jaratlanta , I have that property in my config before and it does not fix the behavior I'm expecting. I'l post my configs

playwright.config.ts

image.png

auth.setup.ts

image.png

home.spec.ts

image.png

When I run the tests with only 1 worker the first test passes but the next one doesn't, because I have to re-login... As you can see in the other image the tests passed but I increased the number of workers. I'm not sure why is this happening

image.png
image.png

@_ed.s do you see file getting created? Can you for test hardcode path c:/temp/auth.json use this in both places and see if that works then atleast you know its path issue?

@jaratlanta thanks for replying: Yes, it's actually created (authentication.json) after the auth.setup.ts file runs. I have tried hardcoding the path of the auth file but no difference 😕

From arrcile NOTE: I decided not to use set storageState within my playwright.config.ts file. This is an option that can be used but I want more control over my tests and state.

test.use({ storageState: ".auth/customer01.json" });

Thanks @jaratlanta. I'll read this article and try what suggest here. I'll keep you updated

@jaratlanta I tried what is suggested in the article but not success... I also did a few modifications but didn't work :/. I ended up doing the login process before each test in order to continue

@_ed.s I’m sorry to hear that ! If I get some time will cook something with public canned user site login if any present !

@jaratlanta Thank you! I appreciate all your time trying to help me!

Answer

Setting Up Authentication for Every Test Run in Playwright

To ensure your authentication file is loaded for every test in Playwright, you'll need to create a setup project in your Playwright configuration file (playwright.config.ts). This project will handle authentication before all tests.

Creating the Authentication Setup

First, define a test file (auth.setup.ts) in your setup project. This file will prepare the authenticated browser state for all other tests.

// auth.setup.ts
import { setup } from '@playwright/test';

setup(async ({ page }) => {
  await page.goto('https://ray.run/login');
  await page.getByLabel('Username or email address').fill('username');
  await page.getByLabel('Password').fill('password');
  await page.getByRole('button', { name: 'Sign in' }).click();
  await page.waitForURL('https://ray.run/');
});

Saving the Authenticated State

After authentication, save the authenticated state as storageState using page.context().storageState({ path: authFile }). This saves the browser's storage state into a JSON file specified by authFile.

// auth.setup.ts
import { setup } from '@playwright/test';

setup(async ({ page }) => {
  // ...authentication steps...
  await page.context().storageState({ path: 'playwright/.auth/user.json' });
});

Using the Authenticated State in Tests

In your testing projects (e.g., "chromium"), specify 'playwright/.auth/user.json' as the value for 'storageState'. This ensures each testing project uses the authenticated state when running tests.

// playwright.config.ts
import { PlaywrightTestConfig } from '@playwright/test';

const config: PlaywrightTestConfig = {
  projects: [
    {
      name: 'chromium',
      use: {
        storageState: 'playwright/.auth/user.json',
      },
    },
    // ...other projects...
  ],
};

export default config;

By following these steps, your authentication file will be loaded every time a test runs in Playwright.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.