From 283af8718bb3ac07eab46d4c44eca7c5d8028ca4 Mon Sep 17 00:00:00 2001 From: cedricbellet Date: Mon, 1 May 2017 23:00:39 +0100 Subject: [PATCH] handleJavascriptDialogs (#1939) (#2106) --- lighthouse-core/gather/driver.js | 17 +++++++++++++++++ lighthouse-core/gather/gather-runner.js | 1 + lighthouse-core/test/gather/fake-driver.js | 3 +++ .../test/gather/gather-runner-test.js | 2 ++ 4 files changed, 23 insertions(+) diff --git a/lighthouse-core/gather/driver.js b/lighthouse-core/gather/driver.js index f495c91c35a7..66c963fb3f0e 100644 --- a/lighthouse-core/gather/driver.js +++ b/lighthouse-core/gather/driver.js @@ -833,6 +833,23 @@ class Driver { const promiseArr = urlPatterns.map(url => this.sendCommand('Network.addBlockedURL', {url})); return Promise.all(promiseArr); } + + /** + * Dismiss JavaScript dialogs (alert, confirm, prompt), providing a + * generic promptText in case the dialog is a prompt. + * @return {!Promise} + */ + dismissJavaScriptDialogs() { + return this.sendCommand('Page.enable').then(_ => { + this.once('Page.javascriptDialogOpening', _ => { + // rejection intentionally unhandled + this.sendCommand('Page.handleJavaScriptDialog', { + accept: true, + promptText: 'Lighthouse prompt response', + }); + }); + }); + } } /** diff --git a/lighthouse-core/gather/gather-runner.js b/lighthouse-core/gather/gather-runner.js index 3091ac8fdd4f..9f37b8944c6d 100644 --- a/lighthouse-core/gather/gather-runner.js +++ b/lighthouse-core/gather/gather-runner.js @@ -109,6 +109,7 @@ class GatherRunner { .then(_ => driver.beginEmulation(options.flags)) .then(_ => driver.enableRuntimeEvents()) .then(_ => driver.cacheNatives()) + .then(_ => driver.dismissJavaScriptDialogs()) .then(_ => resetStorage && driver.cleanAndDisableBrowserCaches()) .then(_ => resetStorage && driver.clearDataForOrigin(options.url)) .then(_ => driver.blockUrlPatterns(options.flags.blockedUrlPatterns || [])) diff --git a/lighthouse-core/test/gather/fake-driver.js b/lighthouse-core/test/gather/fake-driver.js index dddcfa6d8b99..2587528f37b8 100644 --- a/lighthouse-core/test/gather/fake-driver.js +++ b/lighthouse-core/test/gather/fake-driver.js @@ -37,6 +37,9 @@ module.exports = { setThrottling() { return Promise.resolve(); }, + dismissJavaScriptDialogs() { + return Promise.resolve(); + }, assertNoSameOriginServiceWorkerClients() { return Promise.resolve(); }, diff --git a/lighthouse-core/test/gather/gather-runner-test.js b/lighthouse-core/test/gather/gather-runner-test.js index 8f8dc374d0cc..b35da6a26e39 100644 --- a/lighthouse-core/test/gather/gather-runner-test.js +++ b/lighthouse-core/test/gather/gather-runner-test.js @@ -255,6 +255,7 @@ describe('GatherRunner', function() { assertNoSameOriginServiceWorkerClients: asyncFunc, beginEmulation: asyncFunc, setThrottling: asyncFunc, + dismissJavaScriptDialogs: asyncFunc, enableRuntimeEvents: asyncFunc, cacheNatives: asyncFunc, cleanAndDisableBrowserCaches: createCheck('calledDisableNetworkCache'), @@ -283,6 +284,7 @@ describe('GatherRunner', function() { assertNoSameOriginServiceWorkerClients: asyncFunc, beginEmulation: asyncFunc, setThrottling: asyncFunc, + dismissJavaScriptDialogs: asyncFunc, enableRuntimeEvents: asyncFunc, cacheNatives: asyncFunc, cleanAndDisableBrowserCaches: createCheck('calledDisableNetworkCache'),