Lukas Bergstrom
Lukas Bergstrom

Reputation: 757

How do I create two contexts in a test with separate IndexedDBs?

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?

Upvotes: 0

Views: 59

Answers (1)

Damian Peralta
Damian Peralta

Reputation: 1876

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();
});

Upvotes: 1

Related Questions