⚠️ This project is not ready for production. Stay tuned! ⚠️
🎭 Playwright test runner 
Cross-browser end-to-end testing for web apps. Browser automation with Playwright, Jest-like assertions and built-in support for TypeScript.
Playwright test runner is available in preview and minor breaking changes could happen. We welcome your feedback to shape this towards 1.0.
Get started
Installation
npm i -D @playwright/test
Write a test
Create tests/foo.spec.ts
to define your test. The test function uses the page
argument for browser automation.
import { test, expect } from "@playwright/test";
test("is a basic test with the page", async ({ page }) => {
await page.goto("https://playwright.dev/");
const name = await page.innerText(".navbar__title");
expect(name).toBe("Playwright");
});
The test runner provides browser primitives as arguments to your test functions. Test functions can use one or more of these arguments.
page
: Instance of Page. Each test gets a new isolated page to run the test.context
: Instance of BrowserContext. Each test gets a new isolated context to run the test. The page
object belongs to this context. Learn how to configure context creation.browser
: Instance of Browser. Browsers are shared across tests to optimize resources. Learn how to configure browser launch.browserName
: The name of the browser currently running the test. Either chromium
, firefox
or webkit
.
You can now run the test using the underlying Folio command line:
npx folio -c tests
Tests and assertions syntax
- Use
test
to write test functions. Run a single test with test.only
and skip a test with test.skip
. - Use
test.describe
to group related tests together. - Use
test.beforeAll
and test.afterAll
hooks to set up and tear down resources shared between tests. - Use
test.beforeEach
and test.afterEach
hooks to set up and tear down resources for each test individually. - For assertions, use the
expect
API.
const { test, expect } = require("@playwright/test");
test.describe("feature foo", () => {
test.beforeEach(async ({ page }) => {
await page.goto("https://my.start.url/feature-foo");
});
test("is working correctly", async ({ page }) => {
expect(page.url()).toBe("https://my.start.url/feature-foo");
});
});
Write a configuration file
Create config.ts
to configure your tests: specify browser launch options, run tests in multiple browsers and much more. Here is an example configuration that runs every test in Chromium, Firefox and WebKit.
import { PlaywrightTestProject } from "@playwright/test";
const projects: PlaywrightTestProject[] = [
{
name: 'chromium',
use: {
browserName: 'chromium',
headless: true,
viewport: { width: 1280, height: 720 },
},
},
{
name: 'webkit',
use: {
browserName: 'webkit',
headless: true,
viewport: { width: 1280, height: 720 },
},
},
{
name: 'firefox',
use: {
browserName: 'firefox',
headless: true,
viewport: { width: 1280, height: 720 },
},
}
];
module.exports = {
timeout: 30000,
projects,
};
Run the test suite
Tests can be run in single or multiple browsers, in parallel or sequentially.
$ npx folio --config=config.ts
$ npx folio --config=config.ts --project=chromium
$ npx folio --config=config.ts --workers=5
$ npx folio --config=config.ts --workers=1
$ npx folio --config=config.ts --retries=2
$ npx folio --help
Refer to the command line documentation for all options.
Configure NPM scripts
Save the run command as an NPM script.
{
"scripts": {
"test": "npx folio --config=config.ts"
}
}
Examples
Multiple pages
The default context
argument is a BrowserContext. Browser contexts are isolated execution environments that can host multiple pages. See multi-page scenarios for more examples.
import { test } from "@playwright/test";
test("tests on multiple web pages", async ({ context }) => {
const pageFoo = await context.newPage();
const pageBar = await context.newPage();
});
Mobile emulation
use
section in the configuration file can be used to configure mobile emulation in the default context
.
// config.ts
import { PlaywrightTestProject } from "@playwright/test";
+ import { devices } from "playwright";
const projects: PlaywrightTestProject[] = [
{
name: 'chromium',
use: {
browserName: 'chromium',
headless: true,
- viewport: { width: 1280, height: 720 },
+ ...devices["Pixel 2"],
},
},
{
name: 'webkit',
use: {
browserName: 'webkit',
headless: true,
- viewport: { width: 1280, height: 720 },
+ ...devices["iPhone 11"],
},
},
{
name: 'firefox',
use: {
browserName: 'firefox',
headless: true,
viewport: { width: 1280, height: 720 },
},
}
];
module.exports = {
timeout: 30000, // Each test is given 30 seconds.
projects,
};
Network mocking
Define a custom route that mocks network calls for a browser context.
import { test, expect } from "@playwright/test";
test.beforeEach(async ({ context }) => {
await context.route(/.css/, route => route.abort());
});
test("loads page without css", async ({ page }) => {
await page.route(/.png/, route => route.abort());
await page.goto("https://stackoverflow.com");
});
Visual comparisons
The expect
API supports visual comparisons with toMatchSnapshot
. This uses the pixelmatch library, and you can pass threshold
as an option.
import { test, expect } from "@playwright/test";
test("compares page screenshot", async ({ page }) => {
await page.goto("https://stackoverflow.com");
const screenshot = await page.screenshot();
expect(screenshot).toMatchSnapshot(`test.png`, { threshold: 0.2 });
});
On first execution, this will generate golden snapshots. Subsequent runs will compare against the golden snapshots. To update golden snapshots with new actuals, run with the --update-snapshots
flag.
npx folio --update-snapshots
Configuration
Modify options
You can modify browser launch options, context creation options and testing options either globally in the configuration file, or locally in the test file.
Playwright test runner is based on the Folio framework, so it supports any configuration available in Folio, and adds a lot of Playwright-specific options.
Globally in the configuration file
You can specify different options for each browser using projects in the configuration file. Below is an example that changes some global testing options, and Chromium browser configuration.
// config.ts
import { PlaywrightTestProject } from "@playwright/test";
const projects: PlaywrightTestProject[] = [
{
name: 'chromium',
use: {
browserName: 'chromium',
- headless: true,
- viewport: { width: 1280, height: 720 },
+
+ // Launch options:
+ headless: false,
+ slowMo: 50,
+
+ // Context options:
+ viewport: { width: 800, height: 600 },
+ ignoreHTTPSErrors: true,
+
+ // Testing options:
+ video: 'retain-on-failure', },
},
{
name: 'webkit',
use: {
browserName: 'webkit',
headless: true,
viewport: { width: 1280, height: 720 },
},
},
{
name: 'firefox',
use: {
browserName: 'firefox',
headless: true,
viewport: { width: 1280, height: 720 },
},
}
];
module.exports = {
- timeout: 30000, // Each test is given 30 seconds.
+ timeout: 90000, // Each test is given 90 seconds.
+ retries: 2, // Failing tests will be retried at most two times.
projects,
};
Locally in the test file
With test.use()
you can override some options for a file, or a describe
block.
import { test, expect } from "@playwright/test";
test.use({ viewport: { width: 600, height: 900 } });
test('my test', async ({ page }) => {
});
Available options
See the full list of launch options in browserType.launch()
documentation.
See the full list of context options in browser.newContext()
documentation.
Available testing options:
screenshot: 'off' | 'on' | 'only-on-failure'
- Whether to capture a screenshot after each test, off by default.
off
- Do not capture screenshots.on
- Capture screenshot after each test.only-on-failure
- Capture screenshot after each test failure.
video: 'off' | 'on' | 'retain-on-failure' | 'retry-with-video'
- Whether to record video for each test, off by default.
off
- Do not record video.on
- Record video for each test.retain-on-failure
- Record video for each test, but remove all videos from successful test runs.retry-with-video
- Record video only when retrying a test.
Most notable testing options from Folio documentation:
retries: number
- Each failing test will be retried up to the certain number of times.testDir: string
- Directory where test runner should search for test files.timeout: number
- Timeout in milliseconds for each test.workers: number
- The maximum number of worker processes to run in parallel.
Skip tests with annotations
The Playwright test runner can annotate tests to skip under certain parameters. This is enabled by Folio annotations.
test("should be skipped on firefox", async ({ page, browserName }) => {
test.skip(browserName === "firefox", "optional description for the skip");
});
Export JUnit report
The Playwright test runner supports various reporters, including exporting as a JUnit compatible XML file.
// config.ts
import { PlaywrightTestProject } from "@playwright/test";
const projects: PlaywrightTestProject[] = [
{
name: 'chromium',
use: {
browserName: 'chromium',
headless: true,
viewport: { width: 1280, height: 720 },
},
},
{
name: 'webkit',
use: {
browserName: 'webkit',
headless: true,
viewport: { width: 1280, height: 720 },
},
},
{
name: 'firefox',
use: {
browserName: 'firefox',
headless: true,
viewport: { width: 1280, height: 720 },
},
}
];
module.exports = {
timeout: 30000, // Each test is given 30 seconds.
+ reporter: [
+ 'list', // Terminal output
+ { name: 'junit', outputFile: 'report.xml' }, // And junit report
+ ],
projects,
};