Implement page.setJavaScriptEnabled method

Fixes #462.
This commit is contained in:
JoelEinbinder 2017-08-23 14:08:56 -07:00 committed by Andrey Lushnikov
parent 63e2b36d29
commit a1414de42f
3 changed files with 31 additions and 0 deletions

View File

@ -56,6 +56,7 @@
+ [page.screenshot([options])](#pagescreenshotoptions)
+ [page.setContent(html)](#pagesetcontenthtml)
+ [page.setExtraHTTPHeaders(headers)](#pagesetextrahttpheadersheaders)
+ [page.setJavaScriptEnabled(enabled)](#pagesetjavascriptenabledenabled)
+ [page.setRequestInterceptionEnabled(value)](#pagesetrequestinterceptionenabledvalue)
+ [page.setUserAgent(userAgent)](#pagesetuseragentuseragent)
+ [page.setViewport(viewport)](#pagesetviewportviewport)
@ -659,6 +660,11 @@ The extra HTTP headers will be sent with every request the page initiates.
> **NOTE** page.setExtraHTTPHeaders does not guarantee the order of headers in the outgoing requests.
#### page.setJavaScriptEnabled(enabled)
- `enabled` <[boolean]> Whether or not to enabled JavaScript on the page.
- returns: <[Promise]>
> **NOTE** changing this value won't affect scripts that have already been run. It will take full effect on the next [navigation](#pagegotourl-options).
#### page.setRequestInterceptionEnabled(value)
- `value` <[boolean]> Whether to enable request interception.

View File

@ -385,6 +385,13 @@ class Page extends EventEmitter {
]);
}
/**
* @param {boolean} enabled
*/
async setJavaScriptEnabled(enabled) {
await this._client.send('Emulation.setScriptExecutionDisabled', { value: !enabled });
}
/**
* @param {?string} mediaType
* @return {!Promise}

View File

@ -1696,6 +1696,24 @@ describe('Page', function() {
}));
});
describe('Page.setJavaScriptEnabled', function() {
it('should work', SX(async function() {
await page.setJavaScriptEnabled(false);
await page.goto('data:text/html, <script>var something = "forbidden"</script>');
let error = null;
try {
await page.evaluate('something');
} catch (e) {
error = e;
}
expect(error.message).toContain('something is not defined');
await page.setJavaScriptEnabled(true);
await page.goto('data:text/html, <script>var something = "forbidden"</script>');
expect(await page.evaluate('something')).toBe('forbidden');
}));
});
describe('Page.evaluateOnNewDocument', function() {
it('should evaluate before anything else on the page', SX(async function() {
await page.evaluateOnNewDocument(function(){