Skip to content

Commit 93a2231

Browse files
committed
Improve Plawright examples
1 parent 2ba2c71 commit 93a2231

File tree

2 files changed

+24
-50
lines changed

2 files changed

+24
-50
lines changed

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

Lines changed: 3 additions & 4 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'

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

Lines changed: 21 additions & 46 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'
@@ -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'

0 commit comments

Comments
 (0)