Rayrun
← Back to Discord Forum

Need help with project dependency and storage state

Hi all,

I have just been playing around with setup dependencies and have a setup project that will log in as my admin user, save the storage state and do a couple of other setup type stuff.

I then have a 'teardown' project which 'setup' will run after all tests.

Then I have my chromium project which has a dependency on the 'setup' project.

When I run my test, setup runs and saves the state as expected. The actual test then runs/passed, and then the teardown does it's thing, all good.

However, If I introduce another browser project for firefox (or any other browser) into my playwright.config.ts, things go wrong.

Basically, the setup runs, chromium test runs/passes, firefox tests runs/fails (because it does not appear to be using the storage state and just sits at login screen, then teardown runs.

I am struggling to understand why firefox is not appearing to use storage state. If I remove the chromium project, firefox works OK and does use the storage state.

Here is my playwright.config.ts and as you can see, both 'chromium' and 'firefox' projects have a dependency on 'setup' and both have storageState set.

Can anyone explain what I am doing wrong here please? Splitting post as it is too long....

This thread is trying to answer question "Why is Firefox not using the storage state when running tests with Chromium?"

7 replies
import { PlaywrightTestConfig } from '@playwright/test';
import * as path from 'path';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const dotenv = require('dotenv');
dotenv.config();

export const STORAGE_STATE = path.join(__dirname, '/auth/adminLogin.json');

const config: PlaywrightTestConfig = {
  reporter: [['list'], ['html', { open: 'never' }]],
  workers: 1,
  globalTimeout: 0,
  expect: { timeout: 10000 },
  timeout: 5 * 60 * 2000,
  reportSlowTests: null,
  use: {
    baseURL: process.env.BASE_URL,
    headless: false,
    actionTimeout: 10000,
    navigationTimeout: 50000,
    viewport: { width: 1600, height: 900 },
  },
  projects: [
    {
      name: 'setup',
      testMatch: 'tests/dependencies/setup/setupAdmin.spec.ts',
      teardown: 'teardown',
    },
    {
      name: 'teardown',
      testMatch: 'tests/dependencies/teardown/teardown.spec.ts',
    },
    {
      name: 'chromium',
      dependencies: ['setup'],
      use: {
        browserName: 'chromium',
        screenshot: 'only-on-failure',
        trace: 'on',
        launchOptions: {
          args: ['--disable-extensions '],
        },
        storageState: STORAGE_STATE,
      },
    },
    {
      name: 'firefox',
      dependencies: ['setup'],
      use: {
        browserName: 'firefox',
        screenshot: 'only-on-failure',
        trace: 'on',
        storageState: STORAGE_STATE,
      },
    },
  ],
};
export default config;

setupAdmin.spec.ts

import { test } from '../../fixtures/base';
import { STORAGE_STATE } from '../../../playwright.config';

test.describe(`Setup`, () => {
  test(`Setup admin user and save logged in state`, async ({ page, loginPage }) => {
    await loginPage.loginAsAdmin();
    await page.context().storageState({ path: STORAGE_STATE });
  });
});

Simple test:

import { test } from '../../fixtures/base';

test.describe(`Login tests`, () => {
  test(`Login as admin`, async ({ page, graphitePages }) => {
    await page.goto('/bc/bc.cgi');
    await graphitePages.homePage.clickListingTab();
    await graphitePages.homePage.logout();
  });
});

Fixed it. The 'logout' in my test was the problem.

Can you show your teardown spec? Just curios how do you do that?

@gosha2456

teardown.spec.ts

import { test } from '../../fixtures/base';
import { STORAGE_STATE } from '../../../playwright.config';
import fs from 'fs';

test.describe(`Teardown`, () => {
  test(`Nuke admin storage state`, () => {
    fs.unlink(STORAGE_STATE, (err) => {
      if (err) throw err;
    });
  });
});

Thank you!

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.