playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Question]: playwright config set in config.ts file overwrites context set in fixture?

Open archlichking opened this issue 5 years ago • 3 comments

Your question

I have a question and I cannot find an answer to it from doc. I have a playwright config file, like this

// playwright.config.ts
const config: PlaywrightTestConfig = {
  testDir: 'tests',
  testMatch: '**/*.test.ts',
  workers: 4,
  reporter: 'list',
  use: {
    headless: true,
    video: 'on',
    contextOptions: {
      recordVideo: {
        dir: 'test-results/video',
      },
    },
  },
}

This config file is loaded when test runs via

npx playwright test --config=playwright.config.ts --project="safari@iphone 12pm"

In our test we want to test scenarios for both logged in user and guest, so I create separate test fixtures according to https://playwright.dev/docs/test-fixtures this link.

// guest fixture
export const visitorTestFixture = base.extend({
  page: async ({ page, video }, use, testInfo) => {
    await use(page);
    ...
  }
})
// logged in fixture
export const userTestFixture = base.extend({
  contextOptions: { storageState: './state.json' },
  page: async ({ page, video }, use, testInfo) => {
    await use(page);
    ...
  }
})

Our website uses cookies to authenticate. What I found was, if contextOptions are enabled in both config file and fixtures, then cookies aren't load in test. I guess the contextOptions in config file has the highest priority here.

I don't know if this is the expected behavior. What is the recommended way here if I need to add some contextOptions on the fly?

archlichking avatar Jul 08 '21 00:07 archlichking

@archlichking can you share complete example where it is failing? I tried this pwtest.zip and it passed.

yury-s avatar Jul 08 '21 12:07 yury-s

@yury-s ahhh, thanks. The difference is here, instead of

// logged in fixture
export const userTestFixture = base.extend({
  contextOptions: { storageState: './state.json' },
  page: async ({ page, video }, use, testInfo) => {
    await use(page);
    ...
  }
})

your example uses

// guest fixture
const test = base.extend({
  storageState: './tests/state.json', //<------------- difference
  page: async ({ page, video }, use, testInfo) => {
    await use(page);
  },
});

Your example works, I'll use this pattern. Thanks, we can close this ticket now.

archlichking avatar Jul 08 '21 17:07 archlichking

The test runner should throw an error like Error: browser.newContext: ENOENT: no such file or directory, open './state.json' if the file doesn't exist so you probably had both state.json and tests/state.json.

yury-s avatar Jul 09 '21 07:07 yury-s