| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2017 Google Inc. All rights reserved. | 
					
						
							|  |  |  |  * Modifications copyright (c) Microsoft Corporation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-08-19 21:32:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-06 07:08:22 -07:00
										 |  |  | import { test as it, expect } from './pageTest'; | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  | import vm from 'vm'; | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   const [request] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForRequest(server.PREFIX + '/digits/2.png'), | 
					
						
							|  |  |  |     page.evaluate(() => { | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |       void fetch('/digits/1.png'); | 
					
						
							|  |  |  |       void fetch('/digits/2.png'); | 
					
						
							|  |  |  |       void fetch('/digits/3.png'); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |     }) | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(request.url()).toBe(server.PREFIX + '/digits/2.png'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with predicate', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   const [request] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('request', request => request.url() === server.PREFIX + '/digits/2.png'), | 
					
						
							|  |  |  |     page.evaluate(() => { | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |       void fetch('/digits/1.png'); | 
					
						
							|  |  |  |       void fetch('/digits/2.png'); | 
					
						
							|  |  |  |       void fetch('/digits/3.png'); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |     }) | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(request.url()).toBe(server.PREFIX + '/digits/2.png'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should respect timeout', async ({ page, playwright }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   let error = null; | 
					
						
							|  |  |  |   await page.waitForEvent('request', { predicate: () => false, timeout: 1 }).catch(e => error = e); | 
					
						
							|  |  |  |   expect(error).toBeInstanceOf(playwright.errors.TimeoutError); | 
					
						
							| 
									
										
										
										
											2021-12-06 15:42:57 -08:00
										 |  |  |   expect(error.message).toContain('Timeout 1ms exceeded while waiting for event "request"'); | 
					
						
							| 
									
										
										
										
											2022-01-06 14:47:52 -08:00
										 |  |  |   // Error stack should point to the api call.
 | 
					
						
							|  |  |  |   const firstFrame = error.stack.split('\n').find(line => line.startsWith('    at ')); | 
					
						
							|  |  |  |   expect(firstFrame).toContain(__filename); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should respect default timeout', async ({ page, playwright }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   let error = null; | 
					
						
							|  |  |  |   page.setDefaultTimeout(1); | 
					
						
							|  |  |  |   await page.waitForEvent('request', () => false).catch(e => error = e); | 
					
						
							|  |  |  |   expect(error).toBeInstanceOf(playwright.errors.TimeoutError); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should log the url', async ({ page }) => { | 
					
						
							| 
									
										
										
										
											2022-06-30 09:05:39 -07:00
										 |  |  |   const error = await page.waitForRequest('long-long-long-long-long-long-long-long-long-long-long-long-long-long.css', { timeout: 1000 }).catch(e => e); | 
					
						
							| 
									
										
										
										
											2023-11-09 00:11:01 +01:00
										 |  |  |   expect(error.message).toContain('waiting for request "long-long-long-long-long-long-long-long-long-long…"'); | 
					
						
							| 
									
										
										
										
											2021-01-22 06:49:59 -08:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with no timeout', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   const [request] = await Promise.all([ | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |     page.waitForRequest(server.PREFIX + '/digits/2.png', { timeout: 0 }), | 
					
						
							| 
									
										
										
										
											2024-05-31 14:44:26 -07:00
										 |  |  |     page.evaluate(() => window.builtinSetTimeout(() => { | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |       void fetch('/digits/1.png'); | 
					
						
							|  |  |  |       void fetch('/digits/2.png'); | 
					
						
							|  |  |  |       void fetch('/digits/3.png'); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |     }, 50)) | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(request.url()).toBe(server.PREFIX + '/digits/2.png'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with url match', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   const [request] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForRequest(/digits\/\d\.png/), | 
					
						
							|  |  |  |     page.evaluate(() => { | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |       void fetch('/digits/1.png'); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |     }) | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(request.url()).toBe(server.PREFIX + '/digits/1.png'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with url match regular expression from a different context', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   const ctx = vm.createContext(); | 
					
						
							|  |  |  |   const regexp = vm.runInContext('new RegExp(/digits\\/\\d\\.png/)', ctx); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   const [request] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForRequest(regexp), | 
					
						
							|  |  |  |     page.evaluate(() => { | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |       void fetch('/digits/1.png'); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |     }) | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(request.url()).toBe(server.PREFIX + '/digits/1.png'); | 
					
						
							|  |  |  | }); |