InfoQ Homepage News Microsoft Releases Playwright Cross-Browser End-to-End Test Runner
Microsoft Releases Playwright Cross-Browser End-to-End Test Runner
This item in japanese
Nov 12, 2020 2 min read
Write for InfoQ
Feed your curiosity. Help 550k+ globalsenior developers
each month stay ahead.Get in touch
Microsoft released (in preview) a dedicated test runner for Playwright, its test automation tool. The Playwright test runner provides zero-config cross-browser end-to-end testing for web apps, Jest-like assertions, and built-in support for TypeScript. The new test runner leverages Folio, a customizable test framework to build higher-level test frameworks.
While testing libraries are actively developed by the community (e.g., playwright-testing-library, or jest-playwright), some developers expressed interest on Reddit for an integrated testing library built specifically for Playwright. Microsoft released in preview the Playwright official test runner after using it internally for a few months.
The Playwright test runner provides it
, describe
, and expect
APIs that can be used as follows:
import { it, expect, describe } from "@playwright/test";
describe("feature foo", () => {
it("compares page screenshot", async ({ page, browserName }) => {
await page.goto("https://stackoverflow.com");
const screenshot = await page.screenshot();
expect(screenshot).toMatchSnapshot(`test-${browserName}.png`, { threshold: 0.2 });
});
});
As the previous code illustrates, the tests defined with it
use an asynchronous function that is passed a set of arguments. The default arguments are page
(an instance of a Playwright Page object); context
(an instance of BrowserContext) and contextOptions
; and browser
and browserOptions
. The documentation provides examples of usage of the default arguments to handle testing multiple pages, produce visual snapshots and make visual comparisons, emulate mobile devices, and mock network calls.
Tests can be run on a single or multiple browsers, in headful or headless mode. Tests may also be slowed down, have screenshots saved in a separate directory in case of failure, or be video-recorded.
For advanced configurations and fixtures, developers may use Folio APIs. Folio self-describes as "a customizable test framework to build your own test frameworks". An example of using Folio to configure viewport size and HTTPS error handling is as follows:
// test/fixtures.ts
import { folio as baseFolio } from "@playwright/test";
import { BrowserContextOptions } from "playwright";
const builder = baseFolio.extend();
builder.contextOptions.override(async ({ contextOptions }, runTest) => {
const modifiedOptions: BrowserContextOptions = {
...contextOptions, // default
viewport: { width: 1440, height: 900 }
}
await runTest(modifiedOptions);
});
const folio = builder.build();
export const it = folio.it;
export const expect = folio.expect;
// test/index.spec.ts
The previous fixture file (test/fixtures.ts
) creates and exports fixture-ready it
and expect
methods to be used in test specifications as follows:
// test/index.spec.ts
import { it, expect } from "./fixtures";
// Test functions go here
it("should have modified viewport", async ({ context }) => {
// ...
});
Folio additionally supports test annotations to deal with failures, flakiness, and tests that are not yet ready.
The Playwright test runner can be installed as follows:
npm i -D @playwright/test
Folio is actively developed and pending release. Folio is an open-source project under the Apache 2 license. The Playwright test runner is nearing the release of its first major iteration. Feedback is welcome and should follow the contribution guidelines and code of conduct.
This content is in the Web Development topic
Related Topics:
-
Related Editorial
-
Related Sponsors
-
Popular across InfoQ
-
AWS Introduces ECS Managed Instances for Containerized Applications
-
Producing a Better Software Architecture with Residuality Theory
-
GitHub Introduces New Embedding Model to Improve Code Search and Context
-
Google DeepMind Introduces CodeMender, an AI Agent for Automated Code Repair
-
Building Distributed Event-Driven Architectures across Multi-Cloud Boundaries
-
Elena Samuylova on Large Language Model (LLM)-Based Application Evaluation and LLM as a Judge
-
Related Content
The InfoQ Newsletter
A round-up of last week’s content on InfoQ sent out every Tuesday. Join a community of over 250,000 senior developers. View an example