Skip to content

Commit 541b434

Browse files
celsopedrosousakathaylkodster28
authored
Adds Playwright documentation (#21402)
* Adds Playwright documentation * Fixes code examples and active limits * fixes trace imports * Changelog entry * Update src/content/docs/browser-rendering/platform/playwright.mdx Co-authored-by: Pedro Sousa <[email protected]> * Update src/content/docs/browser-rendering/platform/playwright.mdx Co-authored-by: Pedro Sousa <[email protected]> * better wording * Update 2025-04-04-playwright-beta.mdx small wording changes * Update src/content/changelog/browser-rendering/2025-04-04-playwright-beta.mdx * Update 2025-04-04-playwright-beta.mdx added closing parenthesis --------- Co-authored-by: Celso Martinho <[email protected]> Co-authored-by: Pedro Sousa <[email protected]> Co-authored-by: Kathy <[email protected]> Co-authored-by: Kody Jackson <[email protected]>
1 parent a39c1bb commit 541b434

File tree

2 files changed

+363
-0
lines changed

2 files changed

+363
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
title: Playwright for Browser Rendering now available
3+
description: Developers can now use the Playwright library for browser automation with Browser Rendering
4+
products:
5+
- browser-rendering
6+
date: 2025-04-04T14:00:00Z
7+
---
8+
9+
We're excited to share that you can now use Playwright's browser automation [capabilities](https://playwright.dev/docs/api/class-playwright) from Cloudflare [Workers](/workers/).
10+
11+
[Playwright](https://playwright.dev/) is an open-source package developed by Microsoft that can do browser automation tasks; it's commonly used to write software tests, debug applications, create screenshots, and crawl pages. Like [Puppeteer](/browser-rendering/platform/puppeteer/), we [forked](https://github.com/cloudflare/playwright) Playwright and modified it to be compatible with Cloudflare Workers and [Browser Rendering](https://developers.cloudflare.com/browser-rendering/).
12+
13+
Below is an example of how to use Playwright with Browser Rendering to test a TODO application using assertions:
14+
15+
```ts title="Assertion example"
16+
import type { Fetcher } from '@cloudflare/workers-types';
17+
import { launch } from '@cloudflare/playwright';
18+
import { expect } from '@cloudflare/playwright/test';
19+
20+
interface Env {
21+
MYBROWSER: Fetcher;
22+
}
23+
24+
export default {
25+
async fetch(request: Request, env: Env) {
26+
27+
const browser = await launch(env.MYBROWSER);
28+
const page = await browser.newPage();
29+
30+
await page.goto('https://demo.playwright.dev/todomvc');
31+
32+
const TODO_ITEMS = todos.length > 0 ? todos : [
33+
'buy some cheese',
34+
'feed the cat',
35+
'book a doctors appointment'
36+
];
37+
38+
const newTodo = page.getByPlaceholder('What needs to be done?');
39+
for (const item of TODO_ITEMS) {
40+
await newTodo.fill(item);
41+
await newTodo.press('Enter');
42+
}
43+
44+
await expect(page.getByTestId('todo-title')).toHaveCount(TODO_ITEMS.length);
45+
46+
await Promise.all(TODO_ITEMS.map(
47+
(value, index) => expect(page.getByTestId('todo-title').nth(index)).toHaveText(value)
48+
));
49+
},
50+
};
51+
```
52+
53+
Playwright is available as an npm package at [`@cloudflare/playwright`](https://www.npmjs.com/package/@cloudflare/playwright) and the code is at [GitHub](https://github.com/cloudflare/playwright).
54+
55+
Learn more in our [documentation](/browser-rendering/platform/playwright/).
Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
---
2+
pcx_content_type: concept
3+
title: Playwright
4+
description: Learn how to use Playwright with Cloudflare Workers for browser automation. Access Playwright API, manage sessions, and optimize browser rendering.
5+
sidebar:
6+
order: 5
7+
badge: Beta
8+
---
9+
10+
import { Render, WranglerConfig, TabItem, Tabs } from "~/components";
11+
12+
[Playwright](https://playwright.dev/) is an open-source package developed by Microsoft that can do browser automation tasks; it's commonly used to write frontend tests, create screenshots, or crawl pages.
13+
14+
The Workers team forked a [version of Playwright](https://github.com/cloudflare/playwright) that was modified to be compatible with [Cloudflare Workers](https://developers.cloudflare.com/workers/) and [Browser Rendering](https://developers.cloudflare.com/browser-rendering/).
15+
16+
Our version is open sourced and can be found in [Cloudflare's fork of Playwright](https://github.com/cloudflare/playwright). The npm package can be installed from [npmjs](https://www.npmjs.com/) as [@cloudflare/playwright](https://www.npmjs.com/package/@cloudflare/playwright):
17+
18+
```bash
19+
npm install @cloudflare/playwright --save-dev
20+
```
21+
22+
## Use Playwright in a Worker
23+
24+
Make sure you have the [browser binding](/browser-rendering/platform/wrangler/#bindings) configured in your `wrangler.toml` file:
25+
26+
<WranglerConfig>
27+
28+
```toml
29+
name = "cloudflare-playwright-example"
30+
main = "src/index.ts"
31+
workers_dev = true
32+
compatibility_flags = ["nodejs_compat_v2"]
33+
compatibility_date = "2025-03-05"
34+
upload_source_maps = true
35+
36+
[dev]
37+
port = 9000
38+
39+
[browser]
40+
binding = "MYBROWSER"
41+
```
42+
43+
</WranglerConfig>
44+
45+
Install the npm package:
46+
47+
```bash
48+
npm install --save-dev @cloudflare/playwright
49+
```
50+
51+
Let's look at some examples of how to use Playwright:
52+
53+
### Take a screenshot
54+
55+
Using browser automation to take screenshots of web pages is a common use case. This script tells the browser to navigate to https://demo.playwright.dev/todomvc, create some items, take a screenshot of the page, and return the image in the response.
56+
57+
```ts
58+
import type { Fetcher } from '@cloudflare/workers-types';
59+
import { launch } from '@cloudflare/playwright';
60+
61+
interface Env {
62+
MYBROWSER: Fetcher;
63+
}
64+
65+
export default {
66+
async fetch(request: Request, env: Env) {
67+
68+
const todos = searchParams.getAll('todo');
69+
70+
const browser = await launch(env.MYBROWSER);
71+
const page = await browser.newPage();
72+
73+
await page.goto('https://demo.playwright.dev/todomvc');
74+
75+
const TODO_ITEMS = todos.length > 0 ? todos : [
76+
'buy some cheese',
77+
'feed the cat',
78+
'book a doctors appointment'
79+
];
80+
81+
const newTodo = page.getByPlaceholder('What needs to be done?');
82+
for (const item of TODO_ITEMS) {
83+
await newTodo.fill(item);
84+
await newTodo.press('Enter');
85+
}
86+
87+
const img = await page.screenshot();
88+
await browser.close();
89+
90+
return new Response(img, {
91+
headers: {
92+
'Content-Type': 'image/png',
93+
},
94+
});
95+
},
96+
}
97+
```
98+
99+
### Trace
100+
101+
A Playwright trace is a detailed log of your workflow execution that captures information like user clicks and navigation actions, screenshots of the page, and any console messages generated and used for debugging. Developers can take a `trace.zip` file and either open it [locally](https://playwright.dev/docs/trace-viewer#opening-the-trace) or upload it to the [Playwright Trace Viewer](https://trace.playwright.dev/), a GUI tool that helps you explore the data.
102+
103+
Here's an example of a worker generating a trace file:
104+
105+
```ts
106+
import type { Fetcher } from '@cloudflare/workers-types';
107+
import { launch, fs } from "@cloudflare/playwright";
108+
import fs from '@cloudflare/playwright/fs';
109+
110+
interface Env {
111+
MYBROWSER: Fetcher;
112+
}
113+
114+
export default {
115+
async fetch(request: Request, env: Env) {
116+
const { searchParams } = new URL(request.url);
117+
const todos = searchParams.getAll('todo');
118+
const trace = searchParams.has('trace');
119+
120+
const browser = await launch(env.MYBROWSER);
121+
const page = await browser.newPage();
122+
123+
if (trace) await page.context().tracing.start({ screenshots: true, snapshots: true });
124+
125+
await page.goto('https://demo.playwright.dev/todomvc');
126+
127+
const TODO_ITEMS = todos.length > 0 ? todos : [
128+
'buy some cheese',
129+
'feed the cat',
130+
'book a doctors appointment'
131+
];
132+
133+
const newTodo = page.getByPlaceholder('What needs to be done?');
134+
for (const item of TODO_ITEMS) {
135+
await newTodo.fill(item);
136+
await newTodo.press('Enter');
137+
}
138+
139+
await expect(page.getByTestId('todo-title')).toHaveCount(TODO_ITEMS.length);
140+
141+
await Promise.all(TODO_ITEMS.map(
142+
(value, index) => expect(page.getByTestId('todo-title').nth(index)).toHaveText(value)
143+
));
144+
145+
if (trace) {
146+
await page.context().tracing.stop({ path: 'trace.zip' });
147+
await browser.close();
148+
const file = await fs.promises.readFile('trace.zip');
149+
150+
return new Response(file, {
151+
status: 200,
152+
headers: {
153+
'Content-Type': 'application/zip',
154+
},
155+
});
156+
} else {
157+
const img = await page.screenshot();
158+
await browser.close();
159+
160+
return new Response(img, {
161+
headers: {
162+
'Content-Type': 'image/png',
163+
},
164+
});
165+
}
166+
},
167+
};
168+
```
169+
170+
### Assertions
171+
172+
One of the most common use cases for using Playwright is software testing. Playwright includes test assertion features in its APIs; refer to [Assertions](https://playwright.dev/docs/test-assertions) in the Playwright documentation for details. Here's an example of a Worker doing `expect()` test assertions of the [todomvc](https://demo.playwright.dev/todomvc) demo page:
173+
174+
```ts
175+
import type { Fetcher } from '@cloudflare/workers-types';
176+
import { launch } from '@cloudflare/playwright';
177+
import { expect } from '@cloudflare/playwright/test';
178+
179+
interface Env {
180+
MYBROWSER: Fetcher;
181+
}
182+
183+
export default {
184+
async fetch(request: Request, env: Env) {
185+
186+
const browser = await launch(env.MYBROWSER);
187+
const page = await browser.newPage();
188+
189+
await page.goto('https://demo.playwright.dev/todomvc');
190+
191+
const TODO_ITEMS = todos.length > 0 ? todos : [
192+
'buy some cheese',
193+
'feed the cat',
194+
'book a doctors appointment'
195+
];
196+
197+
const newTodo = page.getByPlaceholder('What needs to be done?');
198+
for (const item of TODO_ITEMS) {
199+
await newTodo.fill(item);
200+
await newTodo.press('Enter');
201+
}
202+
203+
await expect(page.getByTestId('todo-title')).toHaveCount(TODO_ITEMS.length);
204+
205+
await Promise.all(TODO_ITEMS.map(
206+
(value, index) => expect(page.getByTestId('todo-title').nth(index)).toHaveText(value)
207+
));
208+
},
209+
};
210+
```
211+
212+
### Keep Alive
213+
214+
If users omit the `browser.close()` statement, the browser instance will stay open, ready to be connected to again and [re-used](/browser-rendering/workers-binding-api/reuse-sessions/) but it will, by default, close automatically after 1 minute of inactivity. Users can optionally extend this idle time up to 10 minutes, by using the `keep_alive` option, set in milliseconds:
215+
216+
```js
217+
const browser = await playwright.launch(env.MYBROWSER, { keep_alive: 600000 });
218+
```
219+
220+
Using the above, the browser will stay open for up to 10 minutes, even if inactive.
221+
222+
## Session management
223+
224+
In order to facilitate browser session management, we've extended the Playwright API with new methods:
225+
226+
### List open sessions
227+
228+
`playwright.sessions()` lists the current running sessions. It will return an output similar to this:
229+
230+
```json
231+
[
232+
{
233+
"connectionId": "2a2246fa-e234-4dc1-8433-87e6cee80145",
234+
"connectionStartTime": 1711621704607,
235+
"sessionId": "478f4d7d-e943-40f6-a414-837d3736a1dc",
236+
"startTime": 1711621703708
237+
},
238+
{
239+
"sessionId": "565e05fb-4d2a-402b-869b-5b65b1381db7",
240+
"startTime": 1711621703808
241+
}
242+
]
243+
```
244+
245+
Notice that the session `478f4d7d-e943-40f6-a414-837d3736a1dc` has an active worker connection (`connectionId=2a2246fa-e234-4dc1-8433-87e6cee80145`), while session `565e05fb-4d2a-402b-869b-5b65b1381db7` is free. While a connection is active, no other workers may connect to that session.
246+
247+
### List recent sessions
248+
249+
`playwright.history()` lists recent sessions, both open and closed. It's useful to get a sense of your current usage.
250+
251+
```json
252+
[
253+
{
254+
"closeReason": 2,
255+
"closeReasonText": "BrowserIdle",
256+
"endTime": 1711621769485,
257+
"sessionId": "478f4d7d-e943-40f6-a414-837d3736a1dc",
258+
"startTime": 1711621703708
259+
},
260+
{
261+
"closeReason": 1,
262+
"closeReasonText": "NormalClosure",
263+
"endTime": 1711123501771,
264+
"sessionId": "2be00a21-9fb6-4bb2-9861-8cd48e40e771",
265+
"startTime": 1711123430918
266+
}
267+
]
268+
```
269+
270+
Session `2be00a21-9fb6-4bb2-9861-8cd48e40e771` was closed explicitly with `browser.close()` by the client, while session `478f4d7d-e943-40f6-a414-837d3736a1dc` was closed due to reaching the maximum idle time (check [limits](/browser-rendering/platform/limits/)).
271+
272+
You should also be able to access this information in the dashboard, albeit with a slight delay.
273+
274+
### Active limits
275+
276+
`playwright.limits()` lists your active limits:
277+
278+
```json
279+
{
280+
"activeSessions": [
281+
{ "id": "478f4d7d-e943-40f6-a414-837d3736a1dc" },
282+
{ "id": "565e05fb-4d2a-402b-869b-5b65b1381db7" }
283+
],
284+
"allowedBrowserAcquisitions": 1,
285+
"maxConcurrentSessions": 2,
286+
"timeUntilNextAllowedBrowserAcquisition": 0
287+
}
288+
```
289+
290+
- `activeSessions` lists the IDs of the current open sessions
291+
- `maxConcurrentSessions` defines how many browsers can be open at the same time
292+
- `allowedBrowserAcquisitions` specifies if a new browser session can be opened according to the rate [limits](/browser-rendering/platform/limits/) in place
293+
- `timeUntilNextAllowedBrowserAcquisition` defines the waiting period before a new browser can be launched.
294+
295+
## Playwright API
296+
297+
The full Playwright API can be found [here](https://playwright.dev/docs/api/class-playwright).
298+
299+
Note that `@cloudflare/playwright` is in beta. The following capabilities are not yet fully supported, but we’re actively working on them:
300+
301+
- [API Testing](https://playwright.dev/docs/api-testing)
302+
- [Playwright Test](https://playwright.dev/docs/test-configuration) except [Assertions](https://playwright.dev/docs/test-assertions)
303+
- [Components](https://playwright.dev/docs/test-components)
304+
- [Firefox](https://playwright.dev/docs/api/class-playwright#playwright-firefox), [Android](https://playwright.dev/docs/api/class-android) and [Electron](https://playwright.dev/docs/api/class-electron), as well as different versions of Chrome
305+
- [Network](https://playwright.dev/docs/next/network#network)
306+
- [Videos](https://playwright.dev/docs/next/videos)
307+
308+
This is **not an exhaustive list** — expect rapid changes as we work toward broader parity with the original feature set. You can also check [latest test results](https://playwright-full-test-report.pages.dev/) for a granular up to date list of the features that are fully supported.

0 commit comments

Comments
 (0)