Playwright

Playwright Integration

Q: How do I integrate Stoobly with Playwright tests?

A: Create a Stoobly instance and Playwright interceptor, then call withPage() and apply() in your beforeEach hook.

Example:

import { test, expect } from '@playwright/test';
import Stoobly from 'stoobly';

const stoobly = new Stoobly();
const interceptor = stoobly.playwrightInterceptor({
  scenarioKey: '<SCENARIO-KEY>',
  urls: [new RegExp('https://api.example.com/.*')],
});

test.describe('My Tests', () => {
  test.beforeEach(async ({ page }, testInfo) => {
    // Required: Set page and test title
    await interceptor.withPage(page).apply();
    interceptor.withTestTitle(testInfo.title);
  });

  test('can fetch data', async ({ page }) => {
    await page.goto('https://example.com');
    // Your test code here
  });
});

Q: Why do I need to call withPage() and withTestTitle() in Playwright?

A: Playwright doesn't provide a global API to auto-detect the current page or test title, so you must explicitly set them in beforeEach.

Example:

Q: When should I use withContext() instead of withPage()?

A: Use withContext() when you need to intercept requests from all pages in a browser context, including new pages created during tests. Use withPage() when you only want to intercept requests from a specific page.

Key differences:

  • withPage() - Intercepts requests only from the specified page. New pages created in the same context will NOT be intercepted.

  • withContext() - Intercepts requests from all pages in the browser context, including pages created with context.newPage(), browser extensions, and service workers.

  • Both together - You can use both withContext() and withPage() to ensure all pages are intercepted.

Example:

Use withContext() when:

  1. Your tests create multiple pages (popups, new tabs)

  2. You're testing browser extensions that make background requests

  3. You're testing service workers

  4. You want consistent interception across all pages without calling withPage() for each

Use withPage() when:

  1. You only work with the test fixture page

  2. You want fine-grained control over which pages are intercepted

  3. You're following the typical Playwright test pattern with a single page

Q: How do I record requests in Playwright tests?

A: Use applyRecord() instead of apply() to enable recording mode.

Example:

Last updated