Skip to content

Commit

Permalink
fix: allow user gesture restricted code to be run in page.evaluate (#…
Browse files Browse the repository at this point in the history
…2503)

Fixes #2502
  • Loading branch information
JoelEinbinder authored and aslushnikov committed May 4, 2018
1 parent 1db4986 commit 1d225cf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/ExecutionContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ class ExecutionContext {
if (helper.isString(pageFunction)) {
const contextId = this._contextId;
const expression = /** @type {string} */ (pageFunction);
const { exceptionDetails, result: remoteObject } = await this._client.send('Runtime.evaluate', { expression, contextId, returnByValue: false, awaitPromise: true});
const { exceptionDetails, result: remoteObject } = await this._client.send('Runtime.evaluate', {
expression,
contextId,
returnByValue: false,
awaitPromise: true,
userGesture: true
});
if (exceptionDetails)
throw new Error('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails));
return this._objectHandleFactory(remoteObject);
Expand All @@ -75,7 +81,8 @@ class ExecutionContext {
executionContextId: this._contextId,
arguments: args.map(convertArgument.bind(this)),
returnByValue: false,
awaitPromise: true
awaitPromise: true,
userGesture: true
});
if (exceptionDetails)
throw new Error('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails));
Expand Down
12 changes: 12 additions & 0 deletions test/page.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,18 @@ module.exports.addTests = function({testRunner, expect, puppeteer, DeviceDescrip
const isFive = await page.evaluate(e => Object.is(e, 5), aHandle);
expect(isFive).toBeTruthy();
});
it('should simulate a user gesture', async({page, server}) => {
await page.evaluate(playAudio);
// also test evaluating strings
await page.evaluate(`(${playAudio})()`);

function playAudio() {
const audio = document.createElement('audio');
audio.src = 'data:audio/wav;base64,UklGRiQAAABXQVZFZm10IBAAAAABAAEARKwAAIhYAQACABAAZGF0YQAAAAA=';
// This returns a promise which throws if it was not triggered by a user gesture.
return audio.play();
}
});
});

describe('Page.setOfflineMode', function() {
Expand Down

0 comments on commit 1d225cf

Please sign in to comment.