@@ -55,24 +55,20 @@ Let's look at some examples of how to use Playwright:
5555Using 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
6160interface Env {
62- MYBROWSER: Fetcher ;
61+ MYBROWSER: BrowserWorker ;
6362}
6463
6564export 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
10399Here'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" ;
108103import fs from ' @cloudflare/playwright/fs' ;
109104
110105interface Env {
111- MYBROWSER: Fetcher ;
106+ MYBROWSER: BrowserWorker ;
112107}
113108
114109export 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 {
172148One 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' ;
177152import { expect } from ' @cloudflare/playwright/test' ;
178153
179154interface Env {
180- MYBROWSER: Fetcher ;
155+ MYBROWSER: BrowserWorker ;
181156}
182157
183158export 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