πŸ’‘ If you like this website, please share it with your friends and network! πŸš€
Back to All Questions
Question 72 of 100
Authentication
Advanced

How would you test multi-user scenarios?

The Answer

Create multiple browser contexts, each with different `storageState` (representing different users), and interact with them concurrently in a single test to verify real-time interactions.

Deep Dive Explanation

This pattern is unique to Playwright β€” no other browser automation tool supports true multi-user concurrent testing in a single test process this elegantly. It's especially powerful for testing WebSocket/real-time features, collaborative apps, and role-based workflows.

example.spec.ts
test('admin sends alert, user receives it', async ({ browser }) => {
  // Create two isolated sessions
  const adminContext = await browser.newContext({
    storageState: 'playwright/.auth/admin.json'
  });
  const userContext = await browser.newContext({
    storageState: 'playwright/.auth/user.json'
  });

  const adminPage = await adminContext.newPage();
  const userPage = await userContext.newPage();

  await adminPage.goto('/admin');
  await userPage.goto('/dashboard');

  // Admin sends notification
  await adminPage.getByRole('button', { name: 'Broadcast Alert' }).click();
  await adminPage.getByRole('textbox', { name: 'Message' }).fill('System maintenance in 5 mins');
  await adminPage.getByRole('button', { name: 'Send' }).click();

  // Verify user receives it (real-time)
  await expect(userPage.getByText('System maintenance in 5 mins')).toBeVisible({ timeout: 10000 });

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