Skip to content

Commit 00274fc

Browse files
authored
fix code example and indentation (#32)
* fix code example and indentation * wrap examples with worker fetch to make it copy pasteable
1 parent 77f6b29 commit 00274fc

File tree

1 file changed

+71
-60
lines changed

1 file changed

+71
-60
lines changed

README.md

Lines changed: 71 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -36,88 +36,100 @@ You can find a full running example here [Cloudflare Playwright running example]
3636
```js
3737
import { launch } from '@cloudflare/playwright';
3838

39-
const todos = searchParams.getAll('todo');
40-
41-
const browser = await launch(env.MYBROWSER);
42-
const page = await browser.newPage();
43-
44-
await page.goto('https://demo.playwright.dev/todomvc');
45-
46-
const TODO_ITEMS = todos.length > 0 ? todos : [
47-
'buy some cheese',
48-
'feed the cat',
49-
'book a doctors appointment'
50-
];
51-
52-
const newTodo = page.getByPlaceholder('What needs to be done?');
53-
for (const item of TODO_ITEMS) {
54-
await newTodo.fill(item);
55-
await newTodo.press('Enter');
56-
}
57-
58-
const img = await page.screenshot();
39+
export default {
40+
async fetch(request, env): Promise<Response> {
41+
const browser = await launch(env.MYBROWSER);
42+
const page = await browser.newPage();
43+
44+
await page.goto('https://demo.playwright.dev/todomvc');
45+
46+
const TODO_ITEMS = [
47+
'buy some cheese',
48+
'feed the cat',
49+
'book a doctors appointment'
50+
];
51+
52+
const newTodo = page.getByPlaceholder('What needs to be done?');
53+
for (const item of TODO_ITEMS) {
54+
await newTodo.fill(item);
55+
await newTodo.press('Enter');
56+
}
57+
58+
const img = await page.screenshot();
5959
await browser.close();
6060

6161
return new Response(img, {
6262
headers: {
6363
'Content-Type': 'image/png',
6464
},
6565
});
66+
}
67+
} satisfies ExportedHandler<Env>;
6668
```
6769

6870
### Trace
6971

7072
```js
71-
import { launch } from '@cloudflare/playwright';
73+
import { launch } from "@cloudflare/playwright";
7274
import fs from "@cloudflare/playwright/fs";
7375

74-
const browser = await launch(env.MYBROWSER);
75-
const page = await browser.newPage();
76+
export default {
77+
async fetch(request, env): Promise<Response> {
78+
const browser = await launch(env.MYBROWSER);
79+
const page = await browser.newPage();
7680

77-
await page.context().tracing.start({ screenshots: true, snapshots: true });
81+
await page.context().tracing.start({ screenshots: true, snapshots: true });
7882

79-
// ... do something, screenshot for example
83+
// ... do something, screenshot for example
8084

81-
await page.context().tracing.stop({ path: 'trace.zip' });
82-
await browser.close();
83-
const file = await fs.promises.readFile('trace.zip');
85+
await page.context().tracing.stop({ path: "trace.zip" });
86+
await browser.close();
87+
const file = await fs.promises.readFile("trace.zip");
8488

85-
return new Response(file, {
86-
status: 200,
87-
headers: {
88-
'Content-Type': 'application/zip',
89-
},
90-
});
89+
return new Response(file, {
90+
status: 200,
91+
headers: {
92+
"Content-Type": "application/zip",
93+
},
94+
});
95+
}
96+
} satisfies ExportedHandler<Env>;
9197
```
9298

9399
### Assertions
94100

95101
```js
96-
import { launch } from '@cloudflare/playwright';
97-
import { expect } from '@cloudflare/playwright/test';
98-
99-
const browser = await launch(env.MYBROWSER);
100-
const page = await browser.newPage();
101-
102-
await page.goto('https://demo.playwright.dev/todomvc');
103-
104-
const TODO_ITEMS = todos.length > 0 ? todos : [
105-
'buy some cheese',
106-
'feed the cat',
107-
'book a doctors appointment'
108-
];
109-
110-
const newTodo = page.getByPlaceholder('What needs to be done?');
111-
for (const item of TODO_ITEMS) {
112-
await newTodo.fill(item);
113-
await newTodo.press('Enter');
114-
}
115-
116-
await expect(page.getByTestId('todo-title')).toHaveCount(TODO_ITEMS.length);
117-
118-
await Promise.all(TODO_ITEMS.map(
119-
(value, index) => expect(page.getByTestId('todo-title').nth(index)).toHaveText(value)
120-
));
102+
import { launch } from "@cloudflare/playwright";
103+
import { expect } from "@cloudflare/playwright/test";
104+
105+
export default {
106+
async fetch(request, env): Promise<Response> {
107+
const browser = await launch(env.MYBROWSER);
108+
const page = await browser.newPage();
109+
110+
await page.goto("https://demo.playwright.dev/todomvc");
111+
112+
const TODO_ITEMS = [
113+
"buy some cheese",
114+
"feed the cat",
115+
"book a doctors appointment",
116+
];
117+
118+
const newTodo = page.getByPlaceholder("What needs to be done?");
119+
for (const item of TODO_ITEMS) {
120+
await newTodo.fill(item);
121+
await newTodo.press("Enter");
122+
}
123+
124+
await expect(page.getByTestId("todo-title")).toHaveCount(TODO_ITEMS.length);
125+
126+
await Promise.all(
127+
TODO_ITEMS.map((value, index) =>
128+
expect(page.getByTestId("todo-title").nth(index)).toHaveText(value)
129+
)
130+
);
131+
}
132+
} satisfies ExportedHandler<Env>;
121133
```
122134

123135
# Contribute
@@ -158,4 +170,3 @@ The following capabilities are not yet fully supported, but we’re actively wor
158170
- [Videos](https://playwright.dev/docs/next/videos)
159171

160172
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
161-

0 commit comments

Comments
 (0)