@@ -36,88 +36,100 @@ You can find a full running example here [Cloudflare Playwright running example]
36
36
``` js
37
37
import { launch } from ' @cloudflare/playwright' ;
38
38
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 ();
59
59
await browser .close ();
60
60
61
61
return new Response (img, {
62
62
headers: {
63
63
' Content-Type' : ' image/png' ,
64
64
},
65
65
});
66
+ }
67
+ } satisfies ExportedHandler< Env> ;
66
68
```
67
69
68
70
### Trace
69
71
70
72
``` js
71
- import { launch } from ' @cloudflare/playwright' ;
73
+ import { launch } from " @cloudflare/playwright" ;
72
74
import fs from " @cloudflare/playwright/fs" ;
73
75
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 ();
76
80
77
- await page .context ().tracing .start ({ screenshots: true , snapshots: true });
81
+ await page .context ().tracing .start ({ screenshots: true , snapshots: true });
78
82
79
- // ... do something, screenshot for example
83
+ // ... do something, screenshot for example
80
84
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" );
84
88
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> ;
91
97
```
92
98
93
99
### Assertions
94
100
95
101
``` 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> ;
121
133
```
122
134
123
135
# Contribute
@@ -158,4 +170,3 @@ The following capabilities are not yet fully supported, but we’re actively wor
158
170
- [ Videos] ( https://playwright.dev/docs/next/videos )
159
171
160
172
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