Skip to content

redabacha/polly-adapter-playwright

Repository files navigation

polly-adapter-playwright

npm license

Polly.JS adapter for Playwright. This adapter will attach to a given browser context or page from Playwright for recording and replaying requests.

Installation

npm:

npm install --save-dev polly-adapter-playwright

yarn:

yarn add --dev polly-adapter-playwright

Usage

This adapter works across all browsers supported by Playwright. Simply create a new browser context or page and pass it as an option to this adapter when creating a new Polly instance.

Examples

With a new page:

import { Polly } from '@pollyjs/core';
import { PlaywrightAdapter } from 'polly-adapter-playwright';
import { chromium } from 'playwright';

const browser = await chromium.launch();
const page = await browser.newPage();

// register the playwright adapter so it's accessible by all future polly instances
Polly.register(PlaywrightAdapter);

const polly = new Polly('<Recording Name>', {
  adapters: ['playwright'],
  adapterOptions: {
    playwright: {
      context: page
    }
  }
});

// perform page interactions
...

// cleanup
await polly.stop();
await page.close();

With a new browser context:

import { Polly } from '@pollyjs/core';
import { PlaywrightAdapter } from 'polly-adapter-playwright';
import { chromium } from 'playwright';

const browser = await chromium.launch();
const context = await browser.newContext();

// register the playwright adapter so it's accessible by all future polly instances
Polly.register(PlaywrightAdapter);

const polly = new Polly('<Recording Name>', {
  adapters: ['playwright'],
  adapterOptions: {
    playwright: {
      context
    }
  }
});

const page = await context.newPage();

// perform page interactions
...

// cleanup
await polly.stop();
await page.close();
await context.close();

Options

Name Description
context The browser context or page where requests will be intercepted.
handleFailingRequest Controls how failing requests from Polly will be handled by Playwright. By default this calls route.abort().
modifyResponse Fires before a response is fulfilled for any intercepted request. By default it will modify all responses to allow cross-origin resource sharing by setting the access-control-allow-origin header to *.
routesToIntercept Configures which routes should be intercepted. By default this is set to **\/* which means all routes.
shouldHandleRequest Specifies criteria that should be matched for a request to be intercepted. By default it will only match requests made by fetch or XMLHttpRequest calls.