| 
									
										
										
										
											2020-08-04 15:57:25 -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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-25 15:05:50 -08:00
										 |  |  | import { playwrightTest as it, expect } from '../config/browserTest'; | 
					
						
							| 
									
										
										
										
											2021-04-02 21:07:45 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | it.describe('launch server', () => { | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   it.skip(({ mode }) => mode !== 'default'); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   it('should work', async ({ browserType }) => { | 
					
						
							|  |  |  |     const browserServer = await browserType.launchServer(); | 
					
						
							| 
									
										
										
										
											2020-08-28 13:53:47 -07:00
										 |  |  |     expect(browserServer.wsEndpoint()).not.toBe(null); | 
					
						
							|  |  |  |     await browserServer.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-27 17:24:23 +08:00
										 |  |  |   it('should work with host', async ({ browserType }) => { | 
					
						
							|  |  |  |     const host = '0.0.0.0'; | 
					
						
							|  |  |  |     const browserServer = await browserType.launchServer({ host }); | 
					
						
							|  |  |  |     expect(browserServer.wsEndpoint()).toContain(String(host)); | 
					
						
							|  |  |  |     await browserServer.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   it('should work with port', async ({ browserType }, testInfo) => { | 
					
						
							| 
									
										
										
										
											2021-04-02 21:07:45 -07:00
										 |  |  |     const port = 8800 + testInfo.workerIndex; | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |     const browserServer = await browserType.launchServer({ port }); | 
					
						
							| 
									
										
										
										
											2021-04-02 21:07:45 -07:00
										 |  |  |     expect(browserServer.wsEndpoint()).toContain(String(port)); | 
					
						
							| 
									
										
										
										
											2020-09-14 14:43:39 -07:00
										 |  |  |     await browserServer.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   it('should work with wsPath', async ({ browserType }) => { | 
					
						
							| 
									
										
										
										
											2021-08-22 09:04:47 -07:00
										 |  |  |     const wsPath = '/unguessable-token'; | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |     const browserServer = await browserType.launchServer({ wsPath }); | 
					
						
							| 
									
										
										
										
											2021-08-22 09:04:47 -07:00
										 |  |  |     expect(browserServer.wsEndpoint()).toMatch(/:\d+\/unguessable-token$/); | 
					
						
							|  |  |  |     await browserServer.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   it('should work when wsPath is missing leading slash', async ({ browserType }) => { | 
					
						
							| 
									
										
										
										
											2021-08-22 09:04:47 -07:00
										 |  |  |     const wsPath = 'unguessable-token'; | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |     const browserServer = await browserType.launchServer({ wsPath }); | 
					
						
							| 
									
										
										
										
											2021-08-22 09:04:47 -07:00
										 |  |  |     expect(browserServer.wsEndpoint()).toMatch(/:\d+\/unguessable-token$/); | 
					
						
							|  |  |  |     await browserServer.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   it('should default to random wsPath', async ({ browserType }) => { | 
					
						
							|  |  |  |     const browserServer = await browserType.launchServer(); | 
					
						
							| 
									
										
										
										
											2021-08-22 09:04:47 -07:00
										 |  |  |     expect(browserServer.wsEndpoint()).toMatch(/:\d+\/[a-f\d]{32}$/); | 
					
						
							|  |  |  |     await browserServer.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   it('should provide an error when ws endpoint is incorrect', async ({ browserType }) => { | 
					
						
							|  |  |  |     const browserServer = await browserType.launchServer(); | 
					
						
							| 
									
										
										
										
											2021-01-11 15:53:45 -08:00
										 |  |  |     const error = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() + '-foo' }).catch(e => e); | 
					
						
							|  |  |  |     await browserServer.close(); | 
					
						
							| 
									
										
										
										
											2022-09-29 13:04:19 -08:00
										 |  |  |     expect(error.message).toContain('400 Bad Request'); | 
					
						
							| 
									
										
										
										
											2021-01-11 15:53:45 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   it('should fire "close" event during kill', async ({ browserType }) => { | 
					
						
							| 
									
										
										
										
											2020-08-28 13:53:47 -07:00
										 |  |  |     const order = []; | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |     const browserServer = await browserType.launchServer(); | 
					
						
							| 
									
										
										
										
											2021-04-29 11:11:32 -07:00
										 |  |  |     const closedPromise = new Promise<void>(f => browserServer.on('close', () => { | 
					
						
							| 
									
										
										
										
											2020-08-28 13:53:47 -07:00
										 |  |  |       order.push('closed'); | 
					
						
							|  |  |  |       f(); | 
					
						
							|  |  |  |     })); | 
					
						
							|  |  |  |     await Promise.all([ | 
					
						
							|  |  |  |       browserServer.kill().then(() => order.push('killed')), | 
					
						
							|  |  |  |       closedPromise, | 
					
						
							|  |  |  |     ]); | 
					
						
							|  |  |  |     expect(order).toEqual(['closed', 'killed']); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   it('should return child_process instance', async ({ browserType }) => { | 
					
						
							|  |  |  |     const browserServer = await browserType.launchServer(); | 
					
						
							| 
									
										
										
										
											2020-08-28 13:53:47 -07:00
										 |  |  |     expect(browserServer.process().pid).toBeGreaterThan(0); | 
					
						
							|  |  |  |     await browserServer.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-12 17:37:45 +01:00
										 |  |  |   it('should fire close event', async ({ browserType }) => { | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |     const browserServer = await browserType.launchServer(); | 
					
						
							| 
									
										
										
										
											2020-08-28 13:53:47 -07:00
										 |  |  |     const [result] = await Promise.all([ | 
					
						
							| 
									
										
										
										
											2020-09-09 03:06:52 -07:00
										 |  |  |       // @ts-expect-error The signal parameter is not documented.
 | 
					
						
							|  |  |  |       new Promise(f => browserServer.on('close', (exitCode, signal) => f({ exitCode, signal }))), | 
					
						
							| 
									
										
										
										
											2020-08-28 13:53:47 -07:00
										 |  |  |       browserServer.close(), | 
					
						
							|  |  |  |     ]); | 
					
						
							|  |  |  |     expect(result['exitCode']).toBe(0); | 
					
						
							|  |  |  |     expect(result['signal']).toBe(null); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-11-11 15:12:10 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   it('should log protocol', async ({ browserType }) => { | 
					
						
							| 
									
										
										
										
											2020-11-11 15:12:10 -08:00
										 |  |  |     const logs: string[] = []; | 
					
						
							|  |  |  |     const logger = { | 
					
						
							|  |  |  |       isEnabled(name: string) { | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       log(name: string, severity: string, message: string) { | 
					
						
							|  |  |  |         logs.push(`${name}:${severity}:${message}`); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |     const browserServer = await browserType.launchServer({ logger }); | 
					
						
							| 
									
										
										
										
											2020-11-11 15:12:10 -08:00
										 |  |  |     await browserServer.close(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(logs.some(log => log.startsWith('protocol:verbose:SEND ►'))).toBe(true); | 
					
						
							|  |  |  |     expect(logs.some(log => log.startsWith('protocol:verbose:◀ RECV'))).toBe(true); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:57:25 -07:00
										 |  |  | }); |