fix: allow user gesture restricted code to be run in page.evaluate (#2503)

Fixes #2502
This commit is contained in:
Joel Einbinder 2018-05-04 11:45:16 -07:00 committed by Andrey Lushnikov
parent 1db4986d12
commit 1d225cfa17
2 changed files with 21 additions and 2 deletions

View File

@ -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);
@ -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));

View File

@ -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() {