diff --git a/lib/ExecutionContext.js b/lib/ExecutionContext.js index 88459720099..f326d315227 100644 --- a/lib/ExecutionContext.js +++ b/lib/ExecutionContext.js @@ -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)); diff --git a/test/page.spec.js b/test/page.spec.js index 515af17204c..29191232298 100644 --- a/test/page.spec.js +++ b/test/page.spec.js @@ -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() {