A high-level API to automate web browsers and comprehensive framework for web testing
npx @tessl/cli install tessl/npm-playwright@1.55.0Playwright is a comprehensive framework for web testing and automation that enables cross-browser testing across Chromium, Firefox, and WebKit with a single API. It provides both a high-level testing framework (Playwright Test) and low-level automation capabilities, featuring headless execution support, reliable element interactions, automatic waiting mechanisms, and powerful debugging tools.
npm install playwrightimport { chromium, firefox, webkit, devices } from 'playwright';For testing framework:
import { test, expect } from 'playwright/test';CommonJS:
const { chromium, firefox, webkit } = require('playwright');
const { test, expect } = require('playwright/test');import { chromium } from 'playwright';
(async () => {
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://example.com');
await page.click('button');
await page.fill('input[name="email"]', 'user@example.com');
await page.screenshot({ path: 'screenshot.png' });
await browser.close();
})();import { test, expect } from 'playwright/test';
test('basic test', async ({ page }) => {
await page.goto('https://example.com');
await expect(page.locator('h1')).toHaveText('Welcome');
await page.click('button');
await expect(page.locator('.result')).toBeVisible();
});Playwright is structured around several key components:
Core browser automation functionality for launching browsers, managing contexts, and controlling pages across Chromium, Firefox, and WebKit.
const chromium: BrowserType;
const firefox: BrowserType;
const webkit: BrowserType;
interface BrowserType {
launch(options?: LaunchOptions): Promise<Browser>;
launchPersistentContext(userDataDir: string, options?: BrowserContextOptions & LaunchOptions): Promise<BrowserContext>;
connect(wsEndpoint: string, options?: ConnectOptions): Promise<Browser>;
}Page navigation, element interaction, and content manipulation with automatic waiting and cross-frame support.
interface Page {
goto(url: string, options?: PageGotoOptions): Promise<Response | null>;
locator(selector: string): Locator;
click(selector: string, options?: PageClickOptions): Promise<void>;
fill(selector: string, value: string, options?: PageFillOptions): Promise<void>;
screenshot(options?: PageScreenshotOptions): Promise<Buffer>;
}
interface Locator {
click(options?: LocatorClickOptions): Promise<void>;
fill(value: string, options?: LocatorFillOptions): Promise<void>;
textContent(): Promise<string | null>;
isVisible(): Promise<boolean>;
}HTTP request interception, response mocking, and API testing capabilities with full request/response control.
const request: APIRequest;
interface APIRequestContext {
get(url: string, options?: APIRequestContextOptions): Promise<APIResponse>;
post(url: string, options?: APIRequestContextOptions): Promise<APIResponse>;
put(url: string, options?: APIRequestContextOptions): Promise<APIResponse>;
delete(url: string, options?: APIRequestContextOptions): Promise<APIResponse>;
}
interface Route {
fulfill(response: RouteResponse): Promise<void>;
abort(errorCode?: string): Promise<void>;
continue(overrides?: RouteContinueOptions): Promise<void>;
}Comprehensive test framework with fixtures for browser automation, parallel execution, and extensive assertion library.
const test: TestType<PlaywrightTestArgs & PlaywrightTestOptions, PlaywrightWorkerArgs & PlaywrightWorkerOptions>;
const expect: Expect<{}>;
interface PlaywrightTestArgs {
page: Page;
context: BrowserContext;
}
interface PlaywrightWorkerArgs {
browser: Browser;
browserName: string;
playwright: typeof import('playwright-core');
}
function defineConfig(config: PlaywrightTestConfig): PlaywrightTestConfig;Mobile browser emulation, device-specific testing, and Android automation for comprehensive cross-platform testing.
const devices: Devices;
const _android: Android;
interface Devices {
[key: string]: DeviceDescriptor;
}
interface DeviceDescriptor {
userAgent: string;
viewport: ViewportSize;
deviceScaleFactor: number;
isMobile: boolean;
hasTouch: boolean;
}
interface Android {
devices(): Promise<AndroidDevice[]>;
connect(wsEndpoint: string): Promise<AndroidDevice>;
}Screenshot comparison, video recording, tracing, and debugging tools for test development and maintenance.
interface Page {
screenshot(options?: PageScreenshotOptions): Promise<Buffer>;
}
interface Tracing {
start(options?: TracingStartOptions): Promise<void>;
stop(options?: TracingStopOptions): Promise<void>;
}
interface Video {
path(): Promise<string>;
delete(): Promise<void>;
}interface LaunchOptions {
headless?: boolean;
executablePath?: string;
args?: string[];
ignoreDefaultArgs?: boolean | string[];
proxy?: ProxySettings;
downloadsPath?: string;
chromiumSandbox?: boolean;
}
interface BrowserContextOptions {
viewport?: ViewportSize | null;
userAgent?: string;
locale?: string;
timezoneId?: string;
geolocation?: Geolocation;
permissions?: string[];
httpCredentials?: HTTPCredentials;
offline?: boolean;
ignoreHTTPSErrors?: boolean;
}
interface ViewportSize {
width: number;
height: number;
}
interface Geolocation {
latitude: number;
longitude: number;
accuracy?: number;
}
interface HTTPCredentials {
username: string;
password: string;
}