From 06d61919efcb3a169e101503ffbc603e4e8b915f Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Mon, 9 Apr 2018 15:46:05 -0700 Subject: [PATCH] test: refactor utils.waitForEvents into utils.waitEvent (#2336) This simplifies waiting for events in tests. --- test/CDPSession.spec.js | 6 +++--- test/browser.spec.js | 6 +++--- test/network.spec.js | 4 ++-- test/page.spec.js | 39 ++++++++++++++++++--------------------- test/target.spec.js | 4 ++-- test/utils.js | 16 ++-------------- 6 files changed, 30 insertions(+), 45 deletions(-) diff --git a/test/CDPSession.spec.js b/test/CDPSession.spec.js index d5a78f06511..4e1d0f54d53 100644 --- a/test/CDPSession.spec.js +++ b/test/CDPSession.spec.js @@ -14,7 +14,7 @@ * limitations under the License. */ -const {waitForEvents} = require('./utils'); +const {waitEvent} = require('./utils'); module.exports.addTests = function({testRunner, expect}) { const {describe, xdescribe, fdescribe} = testRunner; @@ -49,7 +49,7 @@ module.exports.addTests = function({testRunner, expect}) { await page.coverage.stopJSCoverage(); // generate a script in page and wait for the event. const [event] = await Promise.all([ - waitForEvents(client, 'Debugger.scriptParsed'), + waitEvent(client, 'Debugger.scriptParsed'), page.evaluate('//# sourceURL=foo.js') ]); // expect events to be dispatched. @@ -70,4 +70,4 @@ module.exports.addTests = function({testRunner, expect}) { expect(error.message).toContain('Session closed.'); }); }); -}; \ No newline at end of file +}; diff --git a/test/browser.spec.js b/test/browser.spec.js index c78af732e95..6fe52b1e864 100644 --- a/test/browser.spec.js +++ b/test/browser.spec.js @@ -64,7 +64,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p remoteBrowser2.on('disconnected', () => ++disconnectedRemote2); await Promise.all([ - utils.waitForEvents(remoteBrowser2, 'disconnected'), + utils.waitEvent(remoteBrowser2, 'disconnected'), remoteBrowser2.disconnect(), ]); @@ -73,8 +73,8 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p expect(disconnectedRemote2).toBe(1); await Promise.all([ - utils.waitForEvents(remoteBrowser1, 'disconnected'), - utils.waitForEvents(originalBrowser, 'disconnected'), + utils.waitEvent(remoteBrowser1, 'disconnected'), + utils.waitEvent(originalBrowser, 'disconnected'), originalBrowser.close(), ]); diff --git a/test/network.spec.js b/test/network.spec.js index ad48ff6f741..d442e667d1d 100644 --- a/test/network.spec.js +++ b/test/network.spec.js @@ -14,7 +14,7 @@ * limitations under the License. */ -const {waitForEvents} = require('./utils'); +const {waitEvent} = require('./utils'); module.exports.addTests = function({testRunner, expect}) { const {describe, xdescribe, fdescribe} = testRunner; @@ -110,7 +110,7 @@ module.exports.addTests = function({testRunner, expect}) { // send request and wait for server response await Promise.all([ page.evaluate(() => fetch('./get', { method: 'GET'})), - waitForEvents(page, 'response') + waitEvent(page, 'response') ]); expect(serverResponse).toBeTruthy(); diff --git a/test/page.spec.js b/test/page.spec.js index 8193107ac1f..313370c77a2 100644 --- a/test/page.spec.js +++ b/test/page.spec.js @@ -16,7 +16,7 @@ const fs = require('fs'); const path = require('path'); const utils = require('./utils'); -const {waitForEvents, getPDFPages, cssPixelsToInches} = require('./utils'); +const {waitEvent, getPDFPages, cssPixelsToInches} = require('./utils'); module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, puppeteer, PROJECT_ROOT}) { const {describe, xdescribe, fdescribe} = testRunner; @@ -88,7 +88,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p let error = null; page.on('error', err => error = err); page.goto('chrome://crash').catch(e => {}); - await waitForEvents(page, 'error'); + await waitEvent(page, 'error'); expect(error.message).toBe('Page crashed!'); }); }); @@ -329,7 +329,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p page.once('console', m => message = m); await Promise.all([ page.evaluate(() => console.log('hello', 5, {foo: 'bar'})), - waitForEvents(page, 'console') + waitEvent(page, 'console') ]); expect(message.text()).toEqual('hello 5 JSHandle@object'); expect(message.type()).toEqual('log'); @@ -340,20 +340,17 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p it('should work for different console API calls', async({page, server}) => { const messages = []; page.on('console', msg => messages.push(msg)); - await Promise.all([ - page.evaluate(() => { - // A pair of time/timeEnd generates only one Console API call. - console.time('calling console.time'); - console.timeEnd('calling console.time'); - console.trace('calling console.trace'); - console.dir('calling console.dir'); - console.warn('calling console.warn'); - console.error('calling console.error'); - console.log(Promise.resolve('should not wait until resolved!')); - }), - // Wait for 5 events to hit - console.time is not reported - waitForEvents(page, 'console', 5) - ]); + // All console events will be reported before `page.evaluate` is finished. + await page.evaluate(() => { + // A pair of time/timeEnd generates only one Console API call. + console.time('calling console.time'); + console.timeEnd('calling console.time'); + console.trace('calling console.trace'); + console.dir('calling console.dir'); + console.warn('calling console.warn'); + console.error('calling console.error'); + console.log(Promise.resolve('should not wait until resolved!')); + }); expect(messages.map(msg => msg.type())).toEqual([ 'timeEnd', 'trace', 'dir', 'warning', 'error', 'log' ]); @@ -371,7 +368,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p page.once('console', msg => message = msg); await Promise.all([ page.evaluate(() => console.error(window)), - waitForEvents(page, 'console') + waitEvent(page, 'console') ]); expect(message.text()).toBe('JSHandle@object'); }); @@ -380,7 +377,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p describe('Page.Events.DOMContentLoaded', function() { it('should fire when expected', async({page, server}) => { page.goto('about:blank'); - await waitForEvents(page, 'domcontentloaded', 1); + await waitEvent(page, 'domcontentloaded'); }); }); @@ -975,7 +972,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p page.on('request', async r => request = r); page.$eval('iframe', (frame, url) => frame.src = url, server.EMPTY_PAGE), // Wait for request interception. - await waitForEvents(page, 'request'); + await waitEvent(page, 'request'); // Delete frame to cause request to be canceled. await page.$eval('iframe', frame => frame.remove()); let error = null; @@ -1068,7 +1065,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p page.once('pageerror', e => error = e); await Promise.all([ page.goto(server.PREFIX + '/error.html'), - waitForEvents(page, 'pageerror') + waitEvent(page, 'pageerror') ]); expect(error.message).toContain('Fancy'); }); diff --git a/test/target.spec.js b/test/target.spec.js index fc975c6aa66..fe2f0403779 100644 --- a/test/target.spec.js +++ b/test/target.spec.js @@ -14,7 +14,7 @@ * limitations under the License. */ -const {waitForEvents} = require('./utils'); +const {waitEvent} = require('./utils'); module.exports.addTests = function({testRunner, expect}) { const {describe, xdescribe, fdescribe} = testRunner; @@ -126,7 +126,7 @@ module.exports.addTests = function({testRunner, expect}) { serverResponse.writeHead(302, { location: '/injectedstyle.css' }); serverResponse.end(); // Wait for the new page to load. - await waitForEvents(newPage, 'load'); + await waitEvent(newPage, 'load'); // Cleanup. await newPage.close(); }); diff --git a/test/utils.js b/test/utils.js index cfc980582d9..55068cf5bed 100644 --- a/test/utils.js +++ b/test/utils.js @@ -76,22 +76,10 @@ const utils = module.exports = { /** * @param {!EventEmitter} emitter * @param {string} eventName - * @param {number=} eventCount * @return {!Promise} */ - waitForEvents: function(emitter, eventName, eventCount = 1) { - let fulfill; - const promise = new Promise(x => fulfill = x); - emitter.on(eventName, onEvent); - return promise; - - function onEvent(event) { - --eventCount; - if (eventCount) - return; - emitter.removeListener(eventName, onEvent); - fulfill(event); - } + waitEvent: function(emitter, eventName) { + return new Promise(fulfill => emitter.once(eventName, fulfill)); }, /**