Skip to content

Commit 62b9e5c

Browse files
authored
Improve Playwright examples (#21833)
* Improve Plawright examples * Replace tabs with spaces
1 parent c8d9cbe commit 62b9e5c

File tree

2 files changed

+51
-77
lines changed

2 files changed

+51
-77
lines changed

src/content/changelog/browser-rendering/2025-04-04-playwright-beta.mdx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ We're excited to share that you can now use Playwright's browser automation [cap
1313
Below is an example of how to use Playwright with Browser Rendering to test a TODO application using assertions:
1414

1515
```ts title="Assertion example"
16-
import type { Fetcher } from '@cloudflare/workers-types';
17-
import { launch } from '@cloudflare/playwright';
16+
import { launch, type BrowserWorker } from '@cloudflare/playwright';
1817
import { expect } from '@cloudflare/playwright/test';
1918

2019
interface Env {
21-
MYBROWSER: Fetcher;
20+
MYBROWSER: BrowserWorker;
2221
}
2322

2423
export default {
@@ -29,7 +28,7 @@ export default {
2928

3029
await page.goto('https://demo.playwright.dev/todomvc');
3130

32-
const TODO_ITEMS = todos.length > 0 ? todos : [
31+
const TODO_ITEMS = [
3332
'buy some cheese',
3433
'feed the cat',
3534
'book a doctors appointment'
@@ -46,7 +45,7 @@ export default {
4645
await Promise.all(TODO_ITEMS.map(
4746
(value, index) => expect(page.getByTestId('todo-title').nth(index)).toHaveText(value)
4847
));
49-
},
48+
},
5049
};
5150
```
5251

src/content/docs/browser-rendering/platform/playwright.mdx

Lines changed: 47 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,20 @@ Let's look at some examples of how to use Playwright:
5555
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.
5656

5757
```ts
58-
import type { Fetcher } from '@cloudflare/workers-types';
59-
import { launch } from '@cloudflare/playwright';
58+
import { launch, type BrowserWorker } from '@cloudflare/playwright';
6059

6160
interface Env {
62-
MYBROWSER: Fetcher;
61+
MYBROWSER: BrowserWorker;
6362
}
6463

6564
export default {
6665
async fetch(request: Request, env: Env) {
67-
const { searchParams } = new URL(request.url);
68-
const todos = searchParams.getAll('todo');
69-
7066
const browser = await launch(env.MYBROWSER);
7167
const page = await browser.newPage();
7268

7369
await page.goto('https://demo.playwright.dev/todomvc');
7470

75-
const TODO_ITEMS = todos.length > 0 ? todos : [
71+
const TODO_ITEMS = [
7672
'buy some cheese',
7773
'feed the cat',
7874
'book a doctors appointment'
@@ -92,7 +88,7 @@ export default {
9288
'Content-Type': 'image/png',
9389
},
9490
});
95-
},
91+
},
9692
}
9793
```
9894

@@ -103,28 +99,24 @@ A Playwright trace is a detailed log of your workflow execution that captures in
10399
Here's an example of a worker generating a trace file:
104100

105101
```ts
106-
import type { Fetcher } from '@cloudflare/workers-types';
107-
import { launch, fs } from "@cloudflare/playwright";
102+
import { launch, type BrowserWorker } from "@cloudflare/playwright";
108103
import fs from '@cloudflare/playwright/fs';
109104

110105
interface Env {
111-
MYBROWSER: Fetcher;
106+
MYBROWSER: BrowserWorker;
112107
}
113108

114109
export default {
115110
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-
120111
const browser = await launch(env.MYBROWSER);
121112
const page = await browser.newPage();
122113

123-
if (trace) await page.context().tracing.start({ screenshots: true, snapshots: true });
114+
// Start tracing before navigating to the page
115+
await page.context().tracing.start({ screenshots: true, snapshots: true });
124116

125117
await page.goto('https://demo.playwright.dev/todomvc');
126118

127-
const TODO_ITEMS = todos.length > 0 ? todos : [
119+
const TODO_ITEMS = [
128120
'buy some cheese',
129121
'feed the cat',
130122
'book a doctors appointment'
@@ -136,33 +128,17 @@ export default {
136128
await newTodo.press('Enter');
137129
}
138130

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-
));
131+
// Stop tracing and save the trace to a zip file
132+
await page.context().tracing.stop({ path: 'trace.zip' });
133+
await browser.close();
134+
const file = await fs.promises.readFile('trace.zip');
144135

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-
}
136+
return new Response(file, {
137+
status: 200,
138+
headers: {
139+
'Content-Type': 'application/zip',
140+
},
141+
});
166142
},
167143
};
168144
```
@@ -172,12 +148,11 @@ export default {
172148
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:
173149

174150
```ts
175-
import type { Fetcher } from '@cloudflare/workers-types';
176-
import { launch } from '@cloudflare/playwright';
151+
import { launch, type BrowserWorker } from '@cloudflare/playwright';
177152
import { expect } from '@cloudflare/playwright/test';
178153

179154
interface Env {
180-
MYBROWSER: Fetcher;
155+
MYBROWSER: BrowserWorker;
181156
}
182157

183158
export default {
@@ -187,7 +162,7 @@ export default {
187162

188163
await page.goto('https://demo.playwright.dev/todomvc');
189164

190-
const TODO_ITEMS = todos.length > 0 ? todos : [
165+
const TODO_ITEMS = [
191166
'buy some cheese',
192167
'feed the cat',
193168
'book a doctors appointment'
@@ -204,7 +179,7 @@ export default {
204179
await Promise.all(TODO_ITEMS.map(
205180
(value, index) => expect(page.getByTestId('todo-title').nth(index)).toHaveText(value)
206181
));
207-
},
182+
},
208183
};
209184
```
210185

@@ -228,16 +203,16 @@ In order to facilitate browser session management, we've extended the Playwright
228203

229204
```json
230205
[
231-
{
232-
"connectionId": "2a2246fa-e234-4dc1-8433-87e6cee80145",
233-
"connectionStartTime": 1711621704607,
234-
"sessionId": "478f4d7d-e943-40f6-a414-837d3736a1dc",
235-
"startTime": 1711621703708
236-
},
237-
{
238-
"sessionId": "565e05fb-4d2a-402b-869b-5b65b1381db7",
239-
"startTime": 1711621703808
240-
}
206+
{
207+
"connectionId": "2a2246fa-e234-4dc1-8433-87e6cee80145",
208+
"connectionStartTime": 1711621704607,
209+
"sessionId": "478f4d7d-e943-40f6-a414-837d3736a1dc",
210+
"startTime": 1711621703708
211+
},
212+
{
213+
"sessionId": "565e05fb-4d2a-402b-869b-5b65b1381db7",
214+
"startTime": 1711621703808
215+
}
241216
]
242217
```
243218

@@ -249,20 +224,20 @@ Notice that the session `478f4d7d-e943-40f6-a414-837d3736a1dc` has an active wor
249224

250225
```json
251226
[
252-
{
253-
"closeReason": 2,
254-
"closeReasonText": "BrowserIdle",
255-
"endTime": 1711621769485,
256-
"sessionId": "478f4d7d-e943-40f6-a414-837d3736a1dc",
257-
"startTime": 1711621703708
258-
},
259-
{
260-
"closeReason": 1,
261-
"closeReasonText": "NormalClosure",
262-
"endTime": 1711123501771,
263-
"sessionId": "2be00a21-9fb6-4bb2-9861-8cd48e40e771",
264-
"startTime": 1711123430918
265-
}
227+
{
228+
"closeReason": 2,
229+
"closeReasonText": "BrowserIdle",
230+
"endTime": 1711621769485,
231+
"sessionId": "478f4d7d-e943-40f6-a414-837d3736a1dc",
232+
"startTime": 1711621703708
233+
},
234+
{
235+
"closeReason": 1,
236+
"closeReasonText": "NormalClosure",
237+
"endTime": 1711123501771,
238+
"sessionId": "2be00a21-9fb6-4bb2-9861-8cd48e40e771",
239+
"startTime": 1711123430918
240+
}
266241
]
267242
```
268243

0 commit comments

Comments
 (0)