| 
									
										
										
										
											2020-08-04 15:09:24 -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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-06 07:08:22 -07:00
										 |  |  | import { test as it, expect } from './pageTest'; | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  | import path from 'path'; | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should throw an error if no options are provided', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   let error = null; | 
					
						
							|  |  |  |   try { | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |     // @ts-ignore
 | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |     await page.addScriptTag('/injectedfile.js'); | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							|  |  |  |     error = e; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   expect(error.message).toContain('Provide an object with a `url`, `path` or `content` property'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with a url', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   const scriptHandle = await page.addScriptTag({ url: '/injectedfile.js' }); | 
					
						
							|  |  |  |   expect(scriptHandle.asElement()).not.toBeNull(); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   expect(await page.evaluate(() => window['__injected'])).toBe(42); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with a url and type=module', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.addScriptTag({ url: '/es6/es6import.js', type: 'module' }); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   expect(await page.evaluate(() => window['__es6injected'])).toBe(42); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with a path and type=module', async ({ page, server, asset }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2021-04-05 15:51:45 -07:00
										 |  |  |   await page.addScriptTag({ path: asset('es6/es6pathimport.js'), type: 'module' }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.waitForFunction('window.__es6injected'); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   expect(await page.evaluate(() => window['__es6injected'])).toBe(42); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with a content and type=module', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.addScriptTag({ content: `import num from '/es6/es6module.js';window.__es6injected = num;`, type: 'module' }); | 
					
						
							|  |  |  |   await page.waitForFunction('window.__es6injected'); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   expect(await page.evaluate(() => window['__es6injected'])).toBe(42); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should throw an error if loading from url fail', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   let error = null; | 
					
						
							|  |  |  |   try { | 
					
						
							|  |  |  |     await page.addScriptTag({ url: '/nonexistfile.js' }); | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							|  |  |  |     error = e; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   expect(error).not.toBe(null); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with a path', async ({ page, server, asset }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2021-04-05 15:51:45 -07:00
										 |  |  |   const scriptHandle = await page.addScriptTag({ path: asset('injectedfile.js') }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   expect(scriptHandle.asElement()).not.toBeNull(); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   expect(await page.evaluate(() => window['__injected'])).toBe(42); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should include sourceURL when path is provided', async ({ page, server, browserName, asset }) => { | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  |   it.skip(browserName === 'webkit'); | 
					
						
							| 
									
										
										
										
											2021-04-02 14:23:42 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2021-04-05 15:51:45 -07:00
										 |  |  |   await page.addScriptTag({ path: asset('injectedfile.js') }); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   const result = await page.evaluate(() => window['__injectedError'].stack); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   expect(result).toContain(path.join('assets', 'injectedfile.js')); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with content', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   const scriptHandle = await page.addScriptTag({ content: 'window["__injected"] = 35;' }); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   expect(scriptHandle.asElement()).not.toBeNull(); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   expect(await page.evaluate(() => window['__injected'])).toBe(35); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should throw when added with content to the CSP page', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   // Firefox fires onload for blocked script before it issues the CSP console error.
 | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/csp.html'); | 
					
						
							|  |  |  |   let error = null; | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   await page.addScriptTag({ content: 'window["__injected"] = 35;' }).catch(e => error = e); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   expect(error).toBeTruthy(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should throw when added with URL to the CSP page', async ({ page, server, isAndroid }) => { | 
					
						
							| 
									
										
										
										
											2021-04-09 07:59:09 -07:00
										 |  |  |   it.skip(isAndroid, 'No cross-process on Android'); | 
					
						
							| 
									
										
										
										
											2021-04-02 14:23:42 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/csp.html'); | 
					
						
							|  |  |  |   let error = null; | 
					
						
							|  |  |  |   await page.addScriptTag({ url: server.CROSS_PROCESS_PREFIX + '/injectedfile.js' }).catch(e => error = e); | 
					
						
							|  |  |  |   expect(error).toBeTruthy(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should throw a nice error when the request fails', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   const url = server.PREFIX + '/this_does_not_exist.js'; | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   const error = await page.addScriptTag({ url }).catch(e => e); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:09:24 -07:00
										 |  |  |   expect(error.message).toContain(url); | 
					
						
							|  |  |  | }); |