Playwright module

Playwright module provides a method to launch a browser instance. The following is a typical example of using Playwright to drive automation:

  1. const { chromium, firefox, webkit } = require('playwright');
  2. (async () => {
  3. const browser = await chromium.launch(); // Or 'firefox' or 'webkit'.
  4. const page = await browser.newPage();
  5. await page.goto('http://example.com');
  6. // other actions...
  7. await browser.close();
  8. })();

By default, the playwright NPM package automatically downloads browser executables during installation. The playwright-core NPM package can be used to skip automatic downloads.

playwright.chromium

This object can be used to launch or connect to Chromium, returning instances of ChromiumBrowser.

playwright.devices

Returns a list of devices to be used with browser.newContext([options]) or browser.newPage([options]). Actual list of devices can be found in src/deviceDescriptors.ts.

  1. const { webkit, devices } = require('playwright');
  2. const iPhone = devices['iPhone 6'];
  3. (async () => {
  4. const browser = await webkit.launch();
  5. const context = await browser.newContext({
  6. ...iPhone
  7. });
  8. const page = await context.newPage();
  9. await page.goto('http://example.com');
  10. // other actions...
  11. await browser.close();
  12. })();

playwright.errors

Playwright methods might throw errors if they are unable to fulfill a request. For example, page.waitForSelector(selector[, options]) might fail if the selector doesn’t match any nodes during the given timeframe.

For certain types of errors Playwright uses specific error classes. These classes are available via playwright.errors.

An example of handling a timeout error:

  1. try {
  2. await page.waitForSelector('.foo');
  3. } catch (e) {
  4. if (e instanceof playwright.errors.TimeoutError) {
  5. // Do something if this is a timeout.
  6. }
  7. }

playwright.firefox

This object can be used to launch or connect to Firefox, returning instances of FirefoxBrowser.

playwright.selectors

Selectors can be used to install custom selector engines. See Working with selectors for more information.

playwright.webkit

This object can be used to launch or connect to WebKit, returning instances of WebKitBrowser.