test: refactor utils.waitForEvents into utils.waitEvent (#2336)
This simplifies waiting for events in tests.
This commit is contained in:
parent
c86c12e605
commit
06d61919ef
@ -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.');
|
||||
});
|
||||
});
|
||||
};
|
||||
};
|
||||
|
@ -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(),
|
||||
]);
|
||||
|
||||
|
@ -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();
|
||||
|
@ -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');
|
||||
});
|
||||
|
@ -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();
|
||||
});
|
||||
|
@ -76,22 +76,10 @@ const utils = module.exports = {
|
||||
/**
|
||||
* @param {!EventEmitter} emitter
|
||||
* @param {string} eventName
|
||||
* @param {number=} eventCount
|
||||
* @return {!Promise<!Object>}
|
||||
*/
|
||||
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));
|
||||
},
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user