| 
									
										
										
										
											2020-08-03 13:41:48 -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-10 16:48:34 -07:00
										 |  |  | import './base.fixture'; | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  | import { ChromiumBrowser } from '..'; | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-13 16:00:23 -07:00
										 |  |  | it('should work', async({browser}) => { | 
					
						
							| 
									
										
										
										
											2020-08-06 14:12:14 -07:00
										 |  |  |   expect(!!browser['_connection']).toBeTruthy(); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-13 16:00:23 -07:00
										 |  |  | it('should scope context handles', async({browserType, browser, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   const GOLDEN_PRECONDITION = { | 
					
						
							|  |  |  |     _guid: '', | 
					
						
							|  |  |  |     objects: [ | 
					
						
							|  |  |  |       { _guid: 'BrowserType', objects: [ | 
					
						
							|  |  |  |         { _guid: 'Browser', objects: [] } | 
					
						
							|  |  |  |       ] }, | 
					
						
							|  |  |  |       { _guid: 'BrowserType', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'BrowserType', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'Playwright', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'Selectors', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'Electron', objects: [] }, | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   await expectScopeState(browser, GOLDEN_PRECONDITION); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const context = await browser.newContext(); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await expectScopeState(browser, { | 
					
						
							|  |  |  |     _guid: '', | 
					
						
							|  |  |  |     objects: [ | 
					
						
							|  |  |  |       { _guid: 'BrowserType', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'BrowserType', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'BrowserType', objects: [ | 
					
						
							|  |  |  |         { _guid: 'Browser', objects: [ | 
					
						
							|  |  |  |           { _guid: 'BrowserContext', objects: [ | 
					
						
							|  |  |  |             { _guid: 'Frame', objects: [] }, | 
					
						
							|  |  |  |             { _guid: 'Page', objects: [] }, | 
					
						
							|  |  |  |             { _guid: 'Request', objects: [] }, | 
					
						
							|  |  |  |             { _guid: 'Response', objects: [] }, | 
					
						
							|  |  |  |           ]}, | 
					
						
							|  |  |  |         ] }, | 
					
						
							|  |  |  |       ] }, | 
					
						
							|  |  |  |       { _guid: 'Playwright', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'Selectors', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'Electron', objects: [] }, | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await context.close(); | 
					
						
							|  |  |  |   await expectScopeState(browser, GOLDEN_PRECONDITION); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-13 16:00:23 -07:00
										 |  |  | it.skip(!CHROMIUM)('should scope CDPSession handles', async({browserType, browser, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   const GOLDEN_PRECONDITION = { | 
					
						
							|  |  |  |     _guid: '', | 
					
						
							|  |  |  |     objects: [ | 
					
						
							|  |  |  |       { _guid: 'BrowserType', objects: [ | 
					
						
							|  |  |  |         { _guid: 'Browser', objects: [] } | 
					
						
							|  |  |  |       ] }, | 
					
						
							|  |  |  |       { _guid: 'BrowserType', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'BrowserType', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'Playwright', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'Selectors', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'Electron', objects: [] }, | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   await expectScopeState(browserType, GOLDEN_PRECONDITION); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-06 14:12:14 -07:00
										 |  |  |   const session = await (browser as ChromiumBrowser).newBrowserCDPSession(); | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   await expectScopeState(browserType, { | 
					
						
							|  |  |  |     _guid: '', | 
					
						
							|  |  |  |     objects: [ | 
					
						
							|  |  |  |       { _guid: 'BrowserType', objects: [ | 
					
						
							|  |  |  |         { _guid: 'Browser', objects: [ | 
					
						
							|  |  |  |           { _guid: 'CDPSession', objects: [] }, | 
					
						
							|  |  |  |         ] }, | 
					
						
							|  |  |  |       ] }, | 
					
						
							|  |  |  |       { _guid: 'BrowserType', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'BrowserType', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'Playwright', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'Selectors', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'Electron', objects: [] }, | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await session.detach(); | 
					
						
							|  |  |  |   await expectScopeState(browserType, GOLDEN_PRECONDITION); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-13 16:00:23 -07:00
										 |  |  | it('should scope browser handles', async({browserType, defaultBrowserOptions}) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 13:41:48 -07:00
										 |  |  |   const GOLDEN_PRECONDITION = { | 
					
						
							|  |  |  |     _guid: '', | 
					
						
							|  |  |  |     objects: [ | 
					
						
							|  |  |  |       { _guid: 'BrowserType', objects: [ | 
					
						
							|  |  |  |         { _guid: 'Browser', objects: [] } | 
					
						
							|  |  |  |       ] }, | 
					
						
							|  |  |  |       { _guid: 'BrowserType', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'BrowserType', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'Playwright', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'Selectors', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'Electron', objects: [] }, | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   await expectScopeState(browserType, GOLDEN_PRECONDITION); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const browser = await browserType.launch(defaultBrowserOptions); | 
					
						
							|  |  |  |   await browser.newContext(); | 
					
						
							|  |  |  |   await expectScopeState(browserType, { | 
					
						
							|  |  |  |     _guid: '', | 
					
						
							|  |  |  |     objects: [ | 
					
						
							|  |  |  |       { _guid: 'BrowserType', objects: [ | 
					
						
							|  |  |  |         { _guid: 'Browser', objects: [ | 
					
						
							|  |  |  |           { _guid: 'BrowserContext', objects: [] } | 
					
						
							|  |  |  |         ] }, | 
					
						
							|  |  |  |         { _guid: 'Browser', objects: [] }, | 
					
						
							|  |  |  |       ] }, | 
					
						
							|  |  |  |       { _guid: 'BrowserType', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'BrowserType', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'Playwright', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'Selectors', objects: [] }, | 
					
						
							|  |  |  |       { _guid: 'Electron', objects: [] }, | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await browser.close(); | 
					
						
							|  |  |  |   await expectScopeState(browserType, GOLDEN_PRECONDITION); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function expectScopeState(object, golden) { | 
					
						
							|  |  |  |   golden = trimGuids(golden); | 
					
						
							|  |  |  |   const remoteState = trimGuids(await object._channel.debugScopeState()); | 
					
						
							|  |  |  |   const localState = trimGuids(object._connection._debugScopeState()); | 
					
						
							|  |  |  |   expect(localState).toEqual(golden); | 
					
						
							|  |  |  |   expect(remoteState).toEqual(golden); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function compareObjects(a, b) { | 
					
						
							|  |  |  |   if (a._guid !== b._guid) | 
					
						
							|  |  |  |     return a._guid.localeCompare(b._guid); | 
					
						
							|  |  |  |   return a.objects.length - b.objects.length; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function trimGuids(object) { | 
					
						
							|  |  |  |   if (Array.isArray(object)) | 
					
						
							|  |  |  |     return object.map(trimGuids).sort(compareObjects); | 
					
						
							|  |  |  |   if (typeof object === 'object') { | 
					
						
							|  |  |  |     const result = {}; | 
					
						
							|  |  |  |     for (const key in object) | 
					
						
							|  |  |  |       result[key] = trimGuids(object[key]); | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (typeof object === 'string') | 
					
						
							|  |  |  |     return object ? object.match(/[^@]+/)[0] : ''; | 
					
						
							|  |  |  |   return object; | 
					
						
							|  |  |  | } |