@cloudflare/playwright
v1.4.0
Published
Playwright for Cloudflare Browser Run (formerly Browser Rendering)
Keywords
Readme
Playwright for Browser Run
This package is a port of Playwright modified to be compatible with Cloudflare Workers and Browser Run.
Getting Started
Create a Cloudflare Worker:
npm create cloudflare@latest -- cf-playwright-workerInstallation
npm install @cloudflare/playwrightConfiguration
Add the browser binding to your wrangler.toml:
compatibility_flags = ["nodejs_compat"]
browser = { binding = "MYBROWSER" }TypeScript
The package types use the Browser Run types of the Workers runtime, for example BrowserRun and BrowserRunAcquireGuardrails. Your project gets these types from wrangler types with Wrangler 4.134.0 or later, or from @cloudflare/workers-types 5.20260917.1 or later.
If your types are older, TypeScript reports Cannot find name 'BrowserRun'. With skipLibCheck, TypeScript does not report an error, but it also does not check these types. To update the types, run:
npx wrangler typesCDP Protocol Support
Browser Run now has full CDP support, so starting with @cloudflare/playwright version 1.3.0, the library uses the standard CDP (Chrome DevTools Protocol) internally to communicate with Browser Run.
Everything should work the same way, but if you encounter any issues, please report them. You can also downgrade to a previous playwright version by using compatibility_date prior to 2026-03-17 or by adding the no_websocket_standard_binary_type flag:
compatibility_date = "2026-03-16"
# or
compatibility_flags = ["nodejs_compat", "no_websocket_standard_binary_type", ...]See cloudflare/browser-run#193 and cloudflare/workerd#6442 for details.
Examples
Screenshot
import { launch } from '@cloudflare/playwright';
export default {
async fetch(request, env): Promise<Response> {
const browser = await launch(env.MYBROWSER);
const page = await browser.newPage();
await page.goto('https://demo.playwright.dev/todomvc');
const TODO_ITEMS = [
'buy some cheese',
'feed the cat',
'book a doctors appointment',
];
const newTodo = page.getByPlaceholder('What needs to be done?');
for (const item of TODO_ITEMS) {
await newTodo.fill(item);
await newTodo.press('Enter');
}
const img = await page.screenshot();
await browser.close();
return new Response(img, {
headers: {
'Content-Type': 'image/png',
},
});
},
} satisfies ExportedHandler<Env>;Trace
import fs from 'fs';
import { launch } from '@cloudflare/playwright';
export default {
async fetch(request, env): Promise<Response> {
const browser = await launch(env.MYBROWSER);
const page = await browser.newPage();
await page.context().tracing.start({ screenshots: true, snapshots: true });
// ... do something, screenshot for example
// For now, fs only supports writing into /tmp
await page.context().tracing.stop({ path: '/tmp/trace.zip' });
await browser.close();
const file = await fs.promises.readFile('/tmp/trace.zip');
return new Response(file, {
status: 200,
headers: {
'Content-Type': 'application/zip',
},
});
},
} satisfies ExportedHandler<Env>;Assertions
import { launch } from '@cloudflare/playwright';
import { expect } from '@cloudflare/playwright/test';
export default {
async fetch(request, env): Promise<Response> {
const browser = await launch(env.MYBROWSER);
const page = await browser.newPage();
await page.goto('https://demo.playwright.dev/todomvc');
const TODO_ITEMS = [
'buy some cheese',
'feed the cat',
'book a doctors appointment',
];
const newTodo = page.getByPlaceholder('What needs to be done?');
for (const item of TODO_ITEMS) {
await newTodo.fill(item);
await newTodo.press('Enter');
}
await expect(page.getByTestId('todo-title')).toHaveCount(TODO_ITEMS.length);
await Promise.all(
TODO_ITEMS.map((value, index) =>
expect(page.getByTestId('todo-title').nth(index)).toHaveText(value)
)
);
await browser.close();
return new Response('All assertions passed!');
},
} satisfies ExportedHandler<Env>;Unsupported Features
The following capabilities are not fully supported, but we're actively working on them:
- Playwright Test except Assertions
- Components
- Firefox, Android and Electron, as well as different versions of Chrome
- Videos
Documentation
More information in the Browser Run developer docs.
