-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
028dd08
commit 2e65b0a
Showing
16 changed files
with
1,751 additions
and
1,359 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
const {FFOX, CHROMIUM, WEBKIT} = testOptions; | ||
|
||
it('should work with function', async({page, server}) => { | ||
const windowHandle = await page.evaluateHandle(() => { | ||
window.foo = [1, 2]; | ||
return window; | ||
}); | ||
expect(await windowHandle.evaluate(w => w.foo)).toEqual([1, 2]); | ||
}); | ||
|
||
it('should work with expression', async({page, server}) => { | ||
const windowHandle = await page.evaluateHandle(() => { | ||
window.foo = [1, 2]; | ||
return window; | ||
}); | ||
expect(await windowHandle.evaluate('window.foo')).toEqual([1, 2]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
const path = require('path'); | ||
const util = require('util'); | ||
const vm = require('vm'); | ||
const {FFOX, CHROMIUM, WEBKIT, WIN, USES_HOOKS, CHANNEL} = testOptions; | ||
|
||
it('should throw an error if no options are provided', async({page, server}) => { | ||
let error = null; | ||
try { | ||
await page.addScriptTag('/injectedfile.js'); | ||
} catch (e) { | ||
error = e; | ||
} | ||
expect(error.message).toContain('Provide an object with a `url`, `path` or `content` property'); | ||
}); | ||
|
||
it('should work with a url', async({page, server}) => { | ||
await page.goto(server.EMPTY_PAGE); | ||
const scriptHandle = await page.addScriptTag({ url: '/injectedfile.js' }); | ||
expect(scriptHandle.asElement()).not.toBeNull(); | ||
expect(await page.evaluate(() => __injected)).toBe(42); | ||
}); | ||
|
||
it('should work with a url and type=module', async({page, server}) => { | ||
await page.goto(server.EMPTY_PAGE); | ||
await page.addScriptTag({ url: '/es6/es6import.js', type: 'module' }); | ||
expect(await page.evaluate(() => __es6injected)).toBe(42); | ||
}); | ||
|
||
it('should work with a path and type=module', async({page, server}) => { | ||
await page.goto(server.EMPTY_PAGE); | ||
await page.addScriptTag({ path: path.join(__dirname, 'assets/es6/es6pathimport.js'), type: 'module' }); | ||
await page.waitForFunction('window.__es6injected'); | ||
expect(await page.evaluate(() => __es6injected)).toBe(42); | ||
}); | ||
|
||
it('should work with a content and type=module', async({page, server}) => { | ||
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'); | ||
expect(await page.evaluate(() => __es6injected)).toBe(42); | ||
}); | ||
|
||
it('should throw an error if loading from url fail', async({page, server}) => { | ||
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); | ||
}); | ||
|
||
it('should work with a path', async({page, server}) => { | ||
await page.goto(server.EMPTY_PAGE); | ||
const scriptHandle = await page.addScriptTag({ path: path.join(__dirname, 'assets/injectedfile.js') }); | ||
expect(scriptHandle.asElement()).not.toBeNull(); | ||
expect(await page.evaluate(() => __injected)).toBe(42); | ||
}); | ||
|
||
it.skip(WEBKIT)('should include sourceURL when path is provided', async({page, server}) => { | ||
await page.goto(server.EMPTY_PAGE); | ||
await page.addScriptTag({ path: path.join(__dirname, 'assets/injectedfile.js') }); | ||
const result = await page.evaluate(() => __injectedError.stack); | ||
expect(result).toContain(path.join('assets', 'injectedfile.js')); | ||
}); | ||
|
||
it('should work with content', async({page, server}) => { | ||
await page.goto(server.EMPTY_PAGE); | ||
const scriptHandle = await page.addScriptTag({ content: 'window.__injected = 35;' }); | ||
expect(scriptHandle.asElement()).not.toBeNull(); | ||
expect(await page.evaluate(() => __injected)).toBe(35); | ||
}); | ||
|
||
it.fail(FFOX)('should throw when added with content to the CSP page', async({page, server}) => { | ||
// Firefox fires onload for blocked script before it issues the CSP console error. | ||
await page.goto(server.PREFIX + '/csp.html'); | ||
let error = null; | ||
await page.addScriptTag({ content: 'window.__injected = 35;' }).catch(e => error = e); | ||
expect(error).toBeTruthy(); | ||
}); | ||
|
||
it('should throw when added with URL to the CSP page', async({page, server}) => { | ||
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(); | ||
}); | ||
|
||
it('should throw a nice error when the request fails', async({page, server}) => { | ||
await page.goto(server.EMPTY_PAGE); | ||
const url = server.PREFIX + '/this_does_not_exist.js'; | ||
const error = await page.addScriptTag({url}).catch(e => e); | ||
expect(error.message).toContain(url); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
const path = require('path'); | ||
const util = require('util'); | ||
const vm = require('vm'); | ||
const {FFOX, CHROMIUM, WEBKIT, WIN, USES_HOOKS, CHANNEL} = testOptions; | ||
|
||
it('should throw an error if no options are provided', async({page, server}) => { | ||
let error = null; | ||
try { | ||
await page.addStyleTag('/injectedstyle.css'); | ||
} catch (e) { | ||
error = e; | ||
} | ||
expect(error.message).toContain('Provide an object with a `url`, `path` or `content` property'); | ||
}); | ||
|
||
it('should work with a url', async({page, server}) => { | ||
await page.goto(server.EMPTY_PAGE); | ||
const styleHandle = await page.addStyleTag({ url: '/injectedstyle.css' }); | ||
expect(styleHandle.asElement()).not.toBeNull(); | ||
expect(await page.evaluate(`window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')`)).toBe('rgb(255, 0, 0)'); | ||
}); | ||
|
||
it('should throw an error if loading from url fail', async({page, server}) => { | ||
await page.goto(server.EMPTY_PAGE); | ||
let error = null; | ||
try { | ||
await page.addStyleTag({ url: '/nonexistfile.js' }); | ||
} catch (e) { | ||
error = e; | ||
} | ||
expect(error).not.toBe(null); | ||
}); | ||
|
||
it('should work with a path', async({page, server}) => { | ||
await page.goto(server.EMPTY_PAGE); | ||
const styleHandle = await page.addStyleTag({ path: path.join(__dirname, 'assets/injectedstyle.css') }); | ||
expect(styleHandle.asElement()).not.toBeNull(); | ||
expect(await page.evaluate(`window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')`)).toBe('rgb(255, 0, 0)'); | ||
}); | ||
|
||
it('should include sourceURL when path is provided', async({page, server}) => { | ||
await page.goto(server.EMPTY_PAGE); | ||
await page.addStyleTag({ path: path.join(__dirname, 'assets/injectedstyle.css') }); | ||
const styleHandle = await page.$('style'); | ||
const styleContent = await page.evaluate(style => style.innerHTML, styleHandle); | ||
expect(styleContent).toContain(path.join('assets', 'injectedstyle.css')); | ||
}); | ||
|
||
it('should work with content', async({page, server}) => { | ||
await page.goto(server.EMPTY_PAGE); | ||
const styleHandle = await page.addStyleTag({ content: 'body { background-color: green; }' }); | ||
expect(styleHandle.asElement()).not.toBeNull(); | ||
expect(await page.evaluate(`window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')`)).toBe('rgb(0, 128, 0)'); | ||
}); | ||
|
||
it('should throw when added with content to the CSP page', async({page, server}) => { | ||
await page.goto(server.PREFIX + '/csp.html'); | ||
let error = null; | ||
await page.addStyleTag({ content: 'body { background-color: green; }' }).catch(e => error = e); | ||
expect(error).toBeTruthy(); | ||
}); | ||
|
||
it('should throw when added with URL to the CSP page', async({page, server}) => { | ||
await page.goto(server.PREFIX + '/csp.html'); | ||
let error = null; | ||
await page.addStyleTag({ url: server.CROSS_PROCESS_PREFIX + '/injectedstyle.css' }).catch(e => error = e); | ||
expect(error).toBeTruthy(); | ||
}); |
Oops, something went wrong.