admin管理员组

文章数量:1332374

edit: different contexts in Playwright do have separate IndexedDB instances, all is well

I'm writing a web app that used IndexedDB for local storage, and also syncs with a server. In order to test scenarios with multiple clients, I need to have a single test with two contexts that don't share IndexedDB data.

The "Isolation" page on the docs site suggests that browser contexts should be completely isolated, but when I create two contexts like so:

const context1 = await browser.newContext();
const page1 = await context1.newPage();
const context2 = await browser.newContext();
const page2 = await context2.newPage();

page1 and page2 are accessing the same IndexedDB instance.

Is there a way to accomplish this?

edit: different contexts in Playwright do have separate IndexedDB instances, all is well

I'm writing a web app that used IndexedDB for local storage, and also syncs with a server. In order to test scenarios with multiple clients, I need to have a single test with two contexts that don't share IndexedDB data.

The "Isolation" page on the docs site suggests that browser contexts should be completely isolated, but when I create two contexts like so:

const context1 = await browser.newContext();
const page1 = await context1.newPage();
const context2 = await browser.newContext();
const page2 = await context2.newPage();

page1 and page2 are accessing the same IndexedDB instance.

Is there a way to accomplish this?

Share Improve this question edited Nov 24, 2024 at 19:27 Lukas Bergstrom asked Nov 21, 2024 at 7:03 Lukas BergstromLukas Bergstrom 7574 silver badges4 bronze badges 1
  • This was my mistake - I wasn't properly cleaning out the server backing store, and was re-populating IndexedDB from that. – Lukas Bergstrom Commented Nov 24, 2024 at 19:25
Add a comment  | 

1 Answer 1

Reset to default 1

I think you need to authenticate as two different roles, take a look at Multiple signed in roles, maybe you need to check this scenario: Testing multiple roles together

The key point is that you need to pass a different storageState to each context:

import { test } from '@playwright/test';

test('admin and user', async ({ browser }) => {
  // adminContext and all pages inside, including adminPage, are signed in as "admin".
  const adminContext = await browser.newContext({ storageState: 'playwright/.auth/admin.json' });
  const adminPage = await adminContext.newPage();

  // userContext and all pages inside, including userPage, are signed in as "user".
  const userContext = await browser.newContext({ storageState: 'playwright/.auth/user.json' });
  const userPage = await userContext.newPage();

  // ... interact with both adminPage and userPage ...

  await adminContext.close();
  await userContext.close();
});

本文标签: javascriptHow do I create two contexts in a test with separate IndexedDBsStack Overflow