| 
									
										
										
										
											2019-12-10 13:21:51 -08: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. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-12-19 14:51:49 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-17 13:02:50 -07:00
										 |  |  | const {FFOX, CHROMIUM, WEBKIT} = testOptions; | 
					
						
							| 
									
										
										
										
											2019-12-10 13:21:51 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 15:19:09 -07:00
										 |  |  | describe('BrowserContext', function() { | 
					
						
							| 
									
										
										
										
											2020-04-12 22:19:26 -07:00
										 |  |  |   it('should work across sessions', async ({browserType, defaultBrowserOptions}) => { | 
					
						
							| 
									
										
										
										
											2020-04-08 15:19:09 -07:00
										 |  |  |     const browserServer = await browserType.launchServer(defaultBrowserOptions); | 
					
						
							|  |  |  |     const browser1 = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); | 
					
						
							|  |  |  |     expect(browser1.contexts().length).toBe(0); | 
					
						
							|  |  |  |     await browser1.newContext(); | 
					
						
							|  |  |  |     expect(browser1.contexts().length).toBe(1); | 
					
						
							| 
									
										
										
										
											2019-12-10 13:21:51 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 15:19:09 -07:00
										 |  |  |     const browser2 = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); | 
					
						
							|  |  |  |     expect(browser2.contexts().length).toBe(0); | 
					
						
							|  |  |  |     await browser2.newContext(); | 
					
						
							|  |  |  |     expect(browser2.contexts().length).toBe(1); | 
					
						
							| 
									
										
										
										
											2020-02-06 12:41:43 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 15:19:09 -07:00
										 |  |  |     expect(browser1.contexts().length).toBe(1); | 
					
						
							| 
									
										
										
										
											2020-02-06 12:41:43 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 15:19:09 -07:00
										 |  |  |     await browser1.close(); | 
					
						
							|  |  |  |     await browser2.close(); | 
					
						
							| 
									
										
										
										
											2020-03-30 13:49:52 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 15:19:09 -07:00
										 |  |  |     await browserServer._checkLeaks(); | 
					
						
							|  |  |  |     await browserServer.close(); | 
					
						
							| 
									
										
										
										
											2020-01-24 11:12:57 -08:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-04-08 15:19:09 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-01-24 11:12:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 15:19:09 -07:00
										 |  |  | describe('Browser.Events.disconnected', function() { | 
					
						
							| 
									
										
										
										
											2020-04-12 22:19:26 -07:00
										 |  |  |   it.slow()('should be emitted when: browser gets closed, disconnected or underlying websocket gets closed', async ({browserType, defaultBrowserOptions}) => { | 
					
						
							| 
									
										
										
										
											2020-04-08 15:19:09 -07:00
										 |  |  |     const browserServer = await browserType.launchServer(defaultBrowserOptions); | 
					
						
							|  |  |  |     const originalBrowser = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); | 
					
						
							|  |  |  |     const wsEndpoint = browserServer.wsEndpoint(); | 
					
						
							|  |  |  |     const remoteBrowser1 = await browserType.connect({ wsEndpoint }); | 
					
						
							|  |  |  |     const remoteBrowser2 = await browserType.connect({ wsEndpoint }); | 
					
						
							| 
									
										
										
										
											2020-01-24 11:12:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 15:19:09 -07:00
										 |  |  |     let disconnectedOriginal = 0; | 
					
						
							|  |  |  |     let disconnectedRemote1 = 0; | 
					
						
							|  |  |  |     let disconnectedRemote2 = 0; | 
					
						
							|  |  |  |     originalBrowser.on('disconnected', () => ++disconnectedOriginal); | 
					
						
							|  |  |  |     remoteBrowser1.on('disconnected', () => ++disconnectedRemote1); | 
					
						
							|  |  |  |     remoteBrowser2.on('disconnected', () => ++disconnectedRemote2); | 
					
						
							| 
									
										
										
										
											2020-01-24 11:12:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 15:19:09 -07:00
										 |  |  |     await Promise.all([ | 
					
						
							|  |  |  |       new Promise(f => remoteBrowser2.on('disconnected', f)), | 
					
						
							|  |  |  |       remoteBrowser2.close(), | 
					
						
							|  |  |  |     ]); | 
					
						
							| 
									
										
										
										
											2020-01-24 11:12:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 15:19:09 -07:00
										 |  |  |     expect(disconnectedOriginal).toBe(0); | 
					
						
							|  |  |  |     expect(disconnectedRemote1).toBe(0); | 
					
						
							|  |  |  |     expect(disconnectedRemote2).toBe(1); | 
					
						
							| 
									
										
										
										
											2020-01-24 11:12:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 15:19:09 -07:00
										 |  |  |     await Promise.all([ | 
					
						
							|  |  |  |       new Promise(f => remoteBrowser1.on('disconnected', f)), | 
					
						
							|  |  |  |       new Promise(f => originalBrowser.on('disconnected', f)), | 
					
						
							|  |  |  |       browserServer.close(), | 
					
						
							|  |  |  |     ]); | 
					
						
							| 
									
										
										
										
											2020-01-24 11:12:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 15:19:09 -07:00
										 |  |  |     expect(disconnectedOriginal).toBe(1); | 
					
						
							|  |  |  |     expect(disconnectedRemote1).toBe(1); | 
					
						
							|  |  |  |     expect(disconnectedRemote2).toBe(1); | 
					
						
							| 
									
										
										
										
											2020-01-24 11:12:57 -08:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-04-08 15:19:09 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-01-24 11:12:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 15:19:09 -07:00
										 |  |  | describe('browserType.connect', function() { | 
					
						
							| 
									
										
										
										
											2020-04-12 22:19:26 -07:00
										 |  |  |   it('should be able to connect multiple times to the same browser', async({browserType, defaultBrowserOptions}) => { | 
					
						
							| 
									
										
										
										
											2020-04-08 15:19:09 -07:00
										 |  |  |     const browserServer = await browserType.launchServer(defaultBrowserOptions); | 
					
						
							|  |  |  |     const browser1 = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); | 
					
						
							|  |  |  |     const browser2 = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); | 
					
						
							|  |  |  |     const page1 = await browser1.newPage(); | 
					
						
							|  |  |  |     expect(await page1.evaluate(() => 7 * 8)).toBe(56); | 
					
						
							|  |  |  |     browser1.close(); | 
					
						
							| 
									
										
										
										
											2020-02-06 12:41:43 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 15:19:09 -07:00
										 |  |  |     const page2 = await browser2.newPage(); | 
					
						
							|  |  |  |     expect(await page2.evaluate(() => 7 * 6)).toBe(42, 'original browser should still work'); | 
					
						
							|  |  |  |     await browser2.close(); | 
					
						
							|  |  |  |     await browserServer._checkLeaks(); | 
					
						
							|  |  |  |     await browserServer.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-04-12 22:19:26 -07:00
										 |  |  |   it('should not be able to close remote browser', async({browserType, defaultBrowserOptions}) => { | 
					
						
							| 
									
										
										
										
											2020-04-08 15:19:09 -07:00
										 |  |  |     const browserServer = await browserType.launchServer(defaultBrowserOptions); | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       const remote = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); | 
					
						
							|  |  |  |       await remote.newContext(); | 
					
						
							|  |  |  |       await remote.close(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       const remote = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); | 
					
						
							|  |  |  |       await remote.newContext(); | 
					
						
							|  |  |  |       await remote.close(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     await browserServer._checkLeaks(); | 
					
						
							|  |  |  |     await browserServer.close(); | 
					
						
							| 
									
										
										
										
											2019-12-10 13:21:51 -08:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-04-08 15:19:09 -07:00
										 |  |  | }); |