| 
									
										
										
										
											2020-08-04 04:41:48 +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. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-04-03 12:07:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-26 07:05:50 +08:00
										 |  |  | import { contextTest as it, expect } from '../config/browserTest'; | 
					
						
							| 
									
										
										
										
											2023-10-24 00:31:30 +08:00
										 |  |  | import type { JSHandle } from '@playwright/test'; | 
					
						
							| 
									
										
										
										
											2020-08-04 04:41:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-28 00:58:08 +08:00
										 |  |  | it('expose binding should work', async ({ context }) => { | 
					
						
							| 
									
										
										
										
											2023-10-24 00:31:30 +08:00
										 |  |  |   let bindingSource: any; | 
					
						
							| 
									
										
										
										
											2020-08-04 04:41:48 +08:00
										 |  |  |   await context.exposeBinding('add', (source, a, b) => { | 
					
						
							|  |  |  |     bindingSource = source; | 
					
						
							|  |  |  |     return a + b; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-08-06 23:27:00 +08:00
										 |  |  |   const result = await page.evaluate('add(5, 6)'); | 
					
						
							| 
									
										
										
										
											2020-08-04 04:41:48 +08:00
										 |  |  |   expect(bindingSource.context).toBe(context); | 
					
						
							|  |  |  |   expect(bindingSource.page).toBe(page); | 
					
						
							|  |  |  |   expect(bindingSource.frame).toBe(page.mainFrame()); | 
					
						
							|  |  |  |   expect(result).toEqual(11); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-28 00:58:08 +08:00
										 |  |  | it('should work', async ({ context, server }) => { | 
					
						
							| 
									
										
										
										
											2023-10-24 00:31:30 +08:00
										 |  |  |   await context.exposeFunction('add', (a: number, b: number) => a + b); | 
					
						
							| 
									
										
										
										
											2020-08-04 04:41:48 +08:00
										 |  |  |   const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2023-10-24 00:31:30 +08:00
										 |  |  |   await page.exposeFunction('mul', (a: number, b: number) => a * b); | 
					
						
							|  |  |  |   await context.exposeFunction('sub', (a: number, b: number) => a - b); | 
					
						
							| 
									
										
										
										
											2020-08-04 04:41:48 +08:00
										 |  |  |   await context.exposeBinding('addHandle', async ({ frame }, a, b) => { | 
					
						
							|  |  |  |     const handle = await frame.evaluateHandle(([a, b]) => a + b, [a, b]); | 
					
						
							|  |  |  |     return handle; | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-08-06 23:27:00 +08:00
										 |  |  |   const result = await page.evaluate('(async () => ({ mul: await mul(9, 4), add: await add(9, 4), sub: await sub(9, 4), addHandle: await addHandle(5, 6) }))()'); | 
					
						
							| 
									
										
										
										
											2020-08-04 04:41:48 +08:00
										 |  |  |   expect(result).toEqual({ mul: 36, add: 13, sub: 5, addHandle: 11 }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-28 00:58:08 +08:00
										 |  |  | it('should throw for duplicate registrations', async ({ context, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 04:41:48 +08:00
										 |  |  |   await context.exposeFunction('foo', () => {}); | 
					
						
							|  |  |  |   await context.exposeFunction('bar', () => {}); | 
					
						
							|  |  |  |   let error = await context.exposeFunction('foo', () => {}).catch(e => e); | 
					
						
							|  |  |  |   expect(error.message).toContain('Function "foo" has been already registered'); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   error = await page.exposeFunction('foo', () => {}).catch(e => e); | 
					
						
							|  |  |  |   expect(error.message).toContain('Function "foo" has been already registered in the browser context'); | 
					
						
							|  |  |  |   await page.exposeFunction('baz', () => {}); | 
					
						
							|  |  |  |   error = await context.exposeFunction('baz', () => {}).catch(e => e); | 
					
						
							|  |  |  |   expect(error.message).toContain('Function "baz" has been already registered in one of the pages'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-28 00:58:08 +08:00
										 |  |  | it('should be callable from-inside addInitScript', async ({ context, server }) => { | 
					
						
							| 
									
										
										
										
											2023-10-24 00:31:30 +08:00
										 |  |  |   let args: string[] = []; | 
					
						
							|  |  |  |   await context.exposeFunction('woof', function(arg: string) { | 
					
						
							| 
									
										
										
										
											2020-08-04 04:41:48 +08:00
										 |  |  |     args.push(arg); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-08-12 06:50:53 +08:00
										 |  |  |   await context.addInitScript('window["woof"]("context")'); | 
					
						
							| 
									
										
										
										
											2020-08-04 04:41:48 +08:00
										 |  |  |   const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2021-03-19 06:18:25 +08:00
										 |  |  |   await page.evaluate('undefined'); | 
					
						
							|  |  |  |   expect(args).toEqual(['context']); | 
					
						
							| 
									
										
										
										
											2020-08-04 04:41:48 +08:00
										 |  |  |   args = []; | 
					
						
							| 
									
										
										
										
											2021-03-19 06:18:25 +08:00
										 |  |  |   await page.addInitScript('window["woof"]("page")'); | 
					
						
							| 
									
										
										
										
											2020-08-04 04:41:48 +08:00
										 |  |  |   await page.reload(); | 
					
						
							| 
									
										
										
										
											2021-03-19 06:18:25 +08:00
										 |  |  |   expect(args).toEqual(['context', 'page']); | 
					
						
							| 
									
										
										
										
											2020-08-04 04:41:48 +08:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-10-02 13:47:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-28 00:58:08 +08:00
										 |  |  | it('exposeBindingHandle should work', async ({ context }) => { | 
					
						
							| 
									
										
										
										
											2023-10-24 00:31:30 +08:00
										 |  |  |   let target!: JSHandle<any>; | 
					
						
							| 
									
										
										
										
											2020-10-02 13:47:31 +08:00
										 |  |  |   await context.exposeBinding('logme', (source, t) => { | 
					
						
							|  |  |  |     target = t; | 
					
						
							|  |  |  |     return 17; | 
					
						
							|  |  |  |   }, { handle: true }); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   const result = await page.evaluate(async function() { | 
					
						
							| 
									
										
										
										
											2023-10-24 00:31:30 +08:00
										 |  |  |     return (window as any)['logme']({ foo: 42 }); | 
					
						
							| 
									
										
										
										
											2020-10-02 13:47:31 +08:00
										 |  |  |   }); | 
					
						
							|  |  |  |   expect(await target.evaluate(x => x.foo)).toBe(42); | 
					
						
							|  |  |  |   expect(result).toEqual(17); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-05-10 23:41:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | it('should work with CSP', async ({ page, context, server }) => { | 
					
						
							|  |  |  |   server.setCSP('/empty.html', 'default-src "self"'); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   let called = false; | 
					
						
							|  |  |  |   await context.exposeBinding('hi', () => called = true); | 
					
						
							|  |  |  |   await page.evaluate(() => (window as any).hi()); | 
					
						
							|  |  |  |   expect(called).toBe(true); | 
					
						
							|  |  |  | }); |