| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2018 Google Inc. All rights reserved. | 
					
						
							| 
									
										
										
										
											2021-02-10 14:00:02 -08:00
										 |  |  |  * Modifications copyright (c) Microsoft Corporation. | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-04-05 09:18:56 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { test as pageTest, expect } from '../config/pageTest'; | 
					
						
							| 
									
										
										
										
											2021-04-29 11:11:32 -07:00
										 |  |  | import { playwrightTest } from '../config/browserTest'; | 
					
						
							| 
									
										
										
										
											2021-02-10 14:00:02 -08:00
										 |  |  | import http from 'http'; | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-05 09:18:56 -07:00
										 |  |  | pageTest.describe('chromium', () => { | 
					
						
							| 
									
										
										
										
											2021-04-30 13:26:13 -07:00
										 |  |  |   pageTest.skip(({ browserName }) => browserName !== 'chromium'); | 
					
						
							|  |  |  |   pageTest.skip(({ isAndroid }) => isAndroid); | 
					
						
							|  |  |  |   pageTest.skip(({ isElectron }) => isElectron); | 
					
						
							| 
									
										
										
										
											2021-04-05 09:18:56 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   pageTest('should create a worker from a service worker', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-28 13:53:47 -07:00
										 |  |  |     const [worker] = await Promise.all([ | 
					
						
							| 
									
										
										
										
											2021-04-05 09:18:56 -07:00
										 |  |  |       page.context().waitForEvent('serviceworker'), | 
					
						
							| 
									
										
										
										
											2020-08-28 13:53:47 -07:00
										 |  |  |       page.goto(server.PREFIX + '/serviceworkers/empty/sw.html') | 
					
						
							|  |  |  |     ]); | 
					
						
							|  |  |  |     expect(await worker.evaluate(() => self.toString())).toBe('[object ServiceWorkerGlobalScope]'); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-05 09:18:56 -07:00
										 |  |  |   pageTest('serviceWorkers() should return current workers', async ({page, server}) => { | 
					
						
							|  |  |  |     const context = page.context(); | 
					
						
							| 
									
										
										
										
											2020-08-28 13:53:47 -07:00
										 |  |  |     const [worker1] = await Promise.all([ | 
					
						
							| 
									
										
										
										
											2021-04-02 09:47:14 +08:00
										 |  |  |       context.waitForEvent('serviceworker'), | 
					
						
							| 
									
										
										
										
											2020-08-28 13:53:47 -07:00
										 |  |  |       page.goto(server.PREFIX + '/serviceworkers/empty/sw.html') | 
					
						
							|  |  |  |     ]); | 
					
						
							| 
									
										
										
										
											2021-04-02 09:47:14 +08:00
										 |  |  |     let workers = context.serviceWorkers(); | 
					
						
							| 
									
										
										
										
											2020-08-28 13:53:47 -07:00
										 |  |  |     expect(workers.length).toBe(1); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 13:53:47 -07:00
										 |  |  |     const [worker2] = await Promise.all([ | 
					
						
							| 
									
										
										
										
											2021-04-02 09:47:14 +08:00
										 |  |  |       context.waitForEvent('serviceworker'), | 
					
						
							| 
									
										
										
										
											2020-08-28 13:53:47 -07:00
										 |  |  |       page.goto(server.CROSS_PROCESS_PREFIX + '/serviceworkers/empty/sw.html') | 
					
						
							|  |  |  |     ]); | 
					
						
							| 
									
										
										
										
											2021-04-02 09:47:14 +08:00
										 |  |  |     workers = context.serviceWorkers(); | 
					
						
							| 
									
										
										
										
											2020-08-28 13:53:47 -07:00
										 |  |  |     expect(workers.length).toBe(2); | 
					
						
							|  |  |  |     expect(workers).toContain(worker1); | 
					
						
							|  |  |  |     expect(workers).toContain(worker2); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-05 09:18:56 -07:00
										 |  |  |   pageTest('should not create a worker from a shared worker', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-28 13:53:47 -07:00
										 |  |  |     await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |     let serviceWorkerCreated; | 
					
						
							| 
									
										
										
										
											2021-04-05 09:18:56 -07:00
										 |  |  |     page.context().once('serviceworker', () => serviceWorkerCreated = true); | 
					
						
							| 
									
										
										
										
											2020-08-28 13:53:47 -07:00
										 |  |  |     await page.evaluate(() => { | 
					
						
							|  |  |  |       new SharedWorker('data:text/javascript,console.log("hi")'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     expect(serviceWorkerCreated).not.toBeTruthy(); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-05 09:18:56 -07:00
										 |  |  |   pageTest('Page.route should work with intervention headers', async ({server, page}) => { | 
					
						
							| 
									
										
										
										
											2020-08-28 13:53:47 -07:00
										 |  |  |     server.setRoute('/intervention', (req, res) => res.end(`
 | 
					
						
							|  |  |  |       <script> | 
					
						
							|  |  |  |         document.write('<script src="${server.CROSS_PROCESS_PREFIX}/intervention.js">' + '</scr' + 'ipt>'); | 
					
						
							|  |  |  |       </script> | 
					
						
							|  |  |  |     `));
 | 
					
						
							|  |  |  |     server.setRedirect('/intervention.js', '/redirect.js'); | 
					
						
							|  |  |  |     let serverRequest = null; | 
					
						
							|  |  |  |     server.setRoute('/redirect.js', (req, res) => { | 
					
						
							|  |  |  |       serverRequest = req; | 
					
						
							|  |  |  |       res.end('console.log(1);'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await page.route('*', route => route.continue()); | 
					
						
							|  |  |  |     await page.goto(server.PREFIX + '/intervention'); | 
					
						
							|  |  |  |     // Check for feature URL substring rather than https://www.chromestatus.com to
 | 
					
						
							|  |  |  |     // make it work with Edgium.
 | 
					
						
							|  |  |  |     expect(serverRequest.headers.intervention).toContain('feature/5718547946799104'); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-04-05 09:18:56 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | playwrightTest.describe('chromium', () => { | 
					
						
							| 
									
										
										
										
											2021-04-30 13:26:13 -07:00
										 |  |  |   playwrightTest.skip(({ browserName }) => browserName !== 'chromium'); | 
					
						
							| 
									
										
										
										
											2021-04-05 09:18:56 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   playwrightTest('should close service worker together with the context', async ({browserType, browserOptions, server}) => { | 
					
						
							|  |  |  |     const browser = await browserType.launch(browserOptions); | 
					
						
							|  |  |  |     const context = await browser.newContext(); | 
					
						
							|  |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     const [worker] = await Promise.all([ | 
					
						
							|  |  |  |       context.waitForEvent('serviceworker'), | 
					
						
							|  |  |  |       page.goto(server.PREFIX + '/serviceworkers/empty/sw.html') | 
					
						
							|  |  |  |     ]); | 
					
						
							|  |  |  |     const messages = []; | 
					
						
							|  |  |  |     context.on('close', () => messages.push('context')); | 
					
						
							|  |  |  |     worker.on('close', () => messages.push('worker')); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |     expect(messages.join('|')).toBe('worker|context'); | 
					
						
							|  |  |  |     await browser.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-02-10 14:00:02 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-05 09:18:56 -07:00
										 |  |  |   playwrightTest('should connect to an existing cdp session', async ({ browserType, browserOptions }, testInfo) => { | 
					
						
							|  |  |  |     const port = 9339 + testInfo.workerIndex; | 
					
						
							| 
									
										
										
										
											2021-02-10 14:00:02 -08:00
										 |  |  |     const browserServer = await browserType.launch({ | 
					
						
							|  |  |  |       ...browserOptions, | 
					
						
							|  |  |  |       args: ['--remote-debugging-port=' + port] | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       const cdpBrowser = await browserType.connectOverCDP({ | 
					
						
							| 
									
										
										
										
											2021-04-08 14:55:28 -07:00
										 |  |  |         endpointURL: `http://localhost:${port}/`, | 
					
						
							| 
									
										
										
										
											2021-02-10 14:00:02 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  |       const contexts = cdpBrowser.contexts(); | 
					
						
							|  |  |  |       expect(contexts.length).toBe(1); | 
					
						
							|  |  |  |       await cdpBrowser.close(); | 
					
						
							|  |  |  |     } finally { | 
					
						
							|  |  |  |       await browserServer.close(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-03-01 11:05:02 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-05 09:18:56 -07:00
										 |  |  |   playwrightTest('should connect to an existing cdp session twice', async ({ browserType, browserOptions, server }, testInfo) => { | 
					
						
							|  |  |  |     const port = 9339 + testInfo.workerIndex; | 
					
						
							| 
									
										
										
										
											2021-03-01 11:05:02 -08:00
										 |  |  |     const browserServer = await browserType.launch({ | 
					
						
							|  |  |  |       ...browserOptions, | 
					
						
							|  |  |  |       args: ['--remote-debugging-port=' + port] | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       const cdpBrowser1 = await browserType.connectOverCDP({ | 
					
						
							| 
									
										
										
										
											2021-04-08 14:55:28 -07:00
										 |  |  |         endpointURL: `http://localhost:${port}/`, | 
					
						
							| 
									
										
										
										
											2021-03-01 11:05:02 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  |       const cdpBrowser2 = await browserType.connectOverCDP({ | 
					
						
							| 
									
										
										
										
											2021-04-08 14:55:28 -07:00
										 |  |  |         endpointURL: `http://localhost:${port}/`, | 
					
						
							| 
									
										
										
										
											2021-03-01 11:05:02 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  |       const contexts1 = cdpBrowser1.contexts(); | 
					
						
							|  |  |  |       expect(contexts1.length).toBe(1); | 
					
						
							|  |  |  |       const [context1] = contexts1; | 
					
						
							|  |  |  |       const page1 = await context1.newPage(); | 
					
						
							|  |  |  |       await page1.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const contexts2 = cdpBrowser2.contexts(); | 
					
						
							|  |  |  |       expect(contexts2.length).toBe(1); | 
					
						
							|  |  |  |       const [context2] = contexts2; | 
					
						
							|  |  |  |       const page2 = await context2.newPage(); | 
					
						
							|  |  |  |       await page2.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(context1.pages().length).toBe(2); | 
					
						
							|  |  |  |       expect(context2.pages().length).toBe(2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       await cdpBrowser1.close(); | 
					
						
							|  |  |  |       await cdpBrowser2.close(); | 
					
						
							|  |  |  |     } finally { | 
					
						
							|  |  |  |       await browserServer.close(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-03-15 09:50:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-05 09:18:56 -07:00
										 |  |  |   playwrightTest('should connect to existing service workers', async ({browserType, browserOptions, server}, testInfo) => { | 
					
						
							|  |  |  |     const port = 9339 + testInfo.workerIndex; | 
					
						
							| 
									
										
										
										
											2021-03-15 09:50:17 -07:00
										 |  |  |     const browserServer = await browserType.launch({ | 
					
						
							|  |  |  |       ...browserOptions, | 
					
						
							|  |  |  |       args: ['--remote-debugging-port=' + port] | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       const cdpBrowser1 = await browserType.connectOverCDP({ | 
					
						
							| 
									
										
										
										
											2021-04-08 14:55:28 -07:00
										 |  |  |         endpointURL: `http://localhost:${port}`, | 
					
						
							| 
									
										
										
										
											2021-03-15 09:50:17 -07:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2021-04-02 09:47:14 +08:00
										 |  |  |       const context = cdpBrowser1.contexts()[0]; | 
					
						
							| 
									
										
										
										
											2021-03-15 09:50:17 -07:00
										 |  |  |       const page = await cdpBrowser1.contexts()[0].newPage(); | 
					
						
							|  |  |  |       const [worker] = await Promise.all([ | 
					
						
							|  |  |  |         context.waitForEvent('serviceworker'), | 
					
						
							|  |  |  |         page.goto(server.PREFIX + '/serviceworkers/empty/sw.html') | 
					
						
							|  |  |  |       ]); | 
					
						
							|  |  |  |       expect(await worker.evaluate(() => self.toString())).toBe('[object ServiceWorkerGlobalScope]'); | 
					
						
							|  |  |  |       await cdpBrowser1.close(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const cdpBrowser2 = await browserType.connectOverCDP({ | 
					
						
							| 
									
										
										
										
											2021-04-08 14:55:28 -07:00
										 |  |  |         endpointURL: `http://localhost:${port}`, | 
					
						
							| 
									
										
										
										
											2021-03-15 09:50:17 -07:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2021-04-02 09:47:14 +08:00
										 |  |  |       const context2 = cdpBrowser2.contexts()[0]; | 
					
						
							| 
									
										
										
										
											2021-03-15 09:50:17 -07:00
										 |  |  |       expect(context2.serviceWorkers().length).toBe(1); | 
					
						
							|  |  |  |       await cdpBrowser2.close(); | 
					
						
							|  |  |  |     } finally { | 
					
						
							|  |  |  |       await browserServer.close(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-04-08 14:55:28 -07:00
										 |  |  |   playwrightTest('should connect over a ws endpoint', async ({browserType, browserOptions, server}, testInfo) => { | 
					
						
							|  |  |  |     const port = 9339 + testInfo.workerIndex; | 
					
						
							|  |  |  |     const browserServer = await browserType.launch({ | 
					
						
							|  |  |  |       ...browserOptions, | 
					
						
							|  |  |  |       args: ['--remote-debugging-port=' + port] | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       const json = await new Promise<string>((resolve, reject) => { | 
					
						
							|  |  |  |         http.get(`http://localhost:${port}/json/version/`, resp => { | 
					
						
							|  |  |  |           let data = ''; | 
					
						
							|  |  |  |           resp.on('data', chunk => data += chunk); | 
					
						
							|  |  |  |           resp.on('end', () => resolve(data)); | 
					
						
							|  |  |  |         }).on('error', reject); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       const cdpBrowser = await browserType.connectOverCDP({ | 
					
						
							|  |  |  |         endpointURL: JSON.parse(json).webSocketDebuggerUrl, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       const contexts = cdpBrowser.contexts(); | 
					
						
							|  |  |  |       expect(contexts.length).toBe(1); | 
					
						
							|  |  |  |       await cdpBrowser.close(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // also connect with the depercreated wsEndpoint option
 | 
					
						
							|  |  |  |       const cdpBrowser2 = await browserType.connectOverCDP({ | 
					
						
							|  |  |  |         wsEndpoint: JSON.parse(json).webSocketDebuggerUrl, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       const contexts2 = cdpBrowser.contexts(); | 
					
						
							|  |  |  |       expect(contexts2.length).toBe(1); | 
					
						
							|  |  |  |       await cdpBrowser2.close(); | 
					
						
							|  |  |  |     } finally { | 
					
						
							|  |  |  |       await browserServer.close(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-04-23 14:52:27 -07:00
										 |  |  |   playwrightTest('should send extra headers with connect request', async ({browserType, browserOptions, server}, testInfo) => { | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       const [request] = await Promise.all([ | 
					
						
							|  |  |  |         server.waitForWebSocketConnectionRequest(), | 
					
						
							|  |  |  |         browserType.connectOverCDP({ | 
					
						
							|  |  |  |           wsEndpoint: `ws://localhost:${server.PORT}/ws`, | 
					
						
							|  |  |  |           headers: { | 
					
						
							|  |  |  |             'User-Agent': 'Playwright', | 
					
						
							|  |  |  |             'foo': 'bar', | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }).catch(() => {}) | 
					
						
							|  |  |  |       ]); | 
					
						
							|  |  |  |       expect(request.headers['user-agent']).toBe('Playwright'); | 
					
						
							|  |  |  |       expect(request.headers['foo']).toBe('bar'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       const [request] = await Promise.all([ | 
					
						
							|  |  |  |         server.waitForWebSocketConnectionRequest(), | 
					
						
							|  |  |  |         browserType.connectOverCDP({ | 
					
						
							|  |  |  |           endpointURL: `ws://localhost:${server.PORT}/ws`, | 
					
						
							|  |  |  |           headers: { | 
					
						
							|  |  |  |             'User-Agent': 'Playwright', | 
					
						
							|  |  |  |             'foo': 'bar', | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }).catch(() => {}) | 
					
						
							|  |  |  |       ]); | 
					
						
							|  |  |  |       expect(request.headers['user-agent']).toBe('Playwright'); | 
					
						
							|  |  |  |       expect(request.headers['foo']).toBe('bar'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  | }); |