| 
									
										
										
										
											2021-03-19 10:31:54 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2018 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 { contextTest as it, expect } from '../config/browserTest'; | 
					
						
							| 
									
										
										
										
											2021-03-19 10:31:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 19:42:52 +01:00
										 |  |  | it('should work with browser context scripts @smoke', async ({ context, server }) => { | 
					
						
							| 
									
										
										
										
											2023-10-23 09:31:30 -07:00
										 |  |  |   await context.addInitScript(() => (window as any)['temp'] = 123); | 
					
						
							| 
									
										
										
										
											2021-03-19 10:31:54 +08:00
										 |  |  |   const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2023-10-23 09:31:30 -07:00
										 |  |  |   await page.addInitScript(() => (window as any)['injected'] = (window as any)['temp']); | 
					
						
							| 
									
										
										
										
											2021-03-19 10:31:54 +08:00
										 |  |  |   await page.goto(server.PREFIX + '/tamperable.html'); | 
					
						
							| 
									
										
										
										
											2023-10-23 09:31:30 -07:00
										 |  |  |   expect(await page.evaluate(() => (window as any)['result'])).toBe(123); | 
					
						
							| 
									
										
										
										
											2021-03-19 10:31:54 +08:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | it('should work without navigation, after all bindings', async ({ context }) => { | 
					
						
							| 
									
										
										
										
											2023-10-23 09:31:30 -07:00
										 |  |  |   let callback: (arg: unknown) => void; | 
					
						
							| 
									
										
										
										
											2021-03-19 10:31:54 +08:00
										 |  |  |   const promise = new Promise(f => callback = f); | 
					
						
							| 
									
										
										
										
											2023-10-23 09:31:30 -07:00
										 |  |  |   await context.exposeFunction('woof', function(arg: any) { | 
					
						
							| 
									
										
										
										
											2021-03-19 10:31:54 +08:00
										 |  |  |     callback(arg); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await context.addInitScript(() => { | 
					
						
							| 
									
										
										
										
											2023-10-23 09:31:30 -07:00
										 |  |  |     (window as any)['woof']('hey'); | 
					
						
							|  |  |  |     (window as any)['temp'] = 123; | 
					
						
							| 
									
										
										
										
											2021-03-19 10:31:54 +08:00
										 |  |  |   }); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-23 09:31:30 -07:00
										 |  |  |   expect(await page.evaluate(() => (window as any)['temp'])).toBe(123); | 
					
						
							| 
									
										
										
										
											2021-03-19 10:31:54 +08:00
										 |  |  |   expect(await promise).toBe('hey'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | it('should work without navigation in popup', async ({ context }) => { | 
					
						
							| 
									
										
										
										
											2023-10-23 09:31:30 -07:00
										 |  |  |   await context.addInitScript(() => (window as any)['temp'] = 123); | 
					
						
							| 
									
										
										
										
											2021-03-19 10:31:54 +08:00
										 |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   const [popup] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('popup'), | 
					
						
							| 
									
										
										
										
											2023-10-23 09:31:30 -07:00
										 |  |  |     page.evaluate(() => (window as any)['win'] = window.open()), | 
					
						
							| 
									
										
										
										
											2021-03-19 10:31:54 +08:00
										 |  |  |   ]); | 
					
						
							| 
									
										
										
										
											2023-10-23 09:31:30 -07:00
										 |  |  |   expect(await popup.evaluate(() => (window as any)['temp'])).toBe(123); | 
					
						
							| 
									
										
										
										
											2021-03-19 10:31:54 +08:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-05 15:51:45 -07:00
										 |  |  | it('should work with browser context scripts with a path', async ({ context, server, asset }) => { | 
					
						
							|  |  |  |   await context.addInitScript({ path: asset('injectedfile.js') }); | 
					
						
							| 
									
										
										
										
											2021-03-19 10:31:54 +08:00
										 |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/tamperable.html'); | 
					
						
							| 
									
										
										
										
											2023-10-23 09:31:30 -07:00
										 |  |  |   expect(await page.evaluate(() => (window as any)['result'])).toBe(123); | 
					
						
							| 
									
										
										
										
											2021-03-19 10:31:54 +08:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | it('should work with browser context scripts for already created pages', async ({ context, server }) => { | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2023-10-23 09:31:30 -07:00
										 |  |  |   await context.addInitScript(() => (window as any)['temp'] = 123); | 
					
						
							|  |  |  |   await page.addInitScript(() => (window as any)['injected'] = (window as any)['temp']); | 
					
						
							| 
									
										
										
										
											2021-03-19 10:31:54 +08:00
										 |  |  |   await page.goto(server.PREFIX + '/tamperable.html'); | 
					
						
							| 
									
										
										
										
											2023-10-23 09:31:30 -07:00
										 |  |  |   expect(await page.evaluate(() => (window as any)['result'])).toBe(123); | 
					
						
							| 
									
										
										
										
											2021-03-19 10:31:54 +08:00
										 |  |  | }); |