Skip to content

Commit

Permalink
Implement page.setJavaScriptEnabled method
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelEinbinder authored and aslushnikov committed Aug 23, 2017
1 parent 63e2b36 commit a1414de
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions lib/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down

0 comments on commit a1414de

Please sign in to comment.