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.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const {waitForEvents} = require('./utils');
|
const {waitEvent} = require('./utils');
|
||||||
|
|
||||||
module.exports.addTests = function({testRunner, expect}) {
|
module.exports.addTests = function({testRunner, expect}) {
|
||||||
const {describe, xdescribe, fdescribe} = testRunner;
|
const {describe, xdescribe, fdescribe} = testRunner;
|
||||||
@ -49,7 +49,7 @@ module.exports.addTests = function({testRunner, expect}) {
|
|||||||
await page.coverage.stopJSCoverage();
|
await page.coverage.stopJSCoverage();
|
||||||
// generate a script in page and wait for the event.
|
// generate a script in page and wait for the event.
|
||||||
const [event] = await Promise.all([
|
const [event] = await Promise.all([
|
||||||
waitForEvents(client, 'Debugger.scriptParsed'),
|
waitEvent(client, 'Debugger.scriptParsed'),
|
||||||
page.evaluate('//# sourceURL=foo.js')
|
page.evaluate('//# sourceURL=foo.js')
|
||||||
]);
|
]);
|
||||||
// expect events to be dispatched.
|
// expect events to be dispatched.
|
||||||
@ -70,4 +70,4 @@ module.exports.addTests = function({testRunner, expect}) {
|
|||||||
expect(error.message).toContain('Session closed.');
|
expect(error.message).toContain('Session closed.');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -64,7 +64,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
|
|||||||
remoteBrowser2.on('disconnected', () => ++disconnectedRemote2);
|
remoteBrowser2.on('disconnected', () => ++disconnectedRemote2);
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
utils.waitForEvents(remoteBrowser2, 'disconnected'),
|
utils.waitEvent(remoteBrowser2, 'disconnected'),
|
||||||
remoteBrowser2.disconnect(),
|
remoteBrowser2.disconnect(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@ -73,8 +73,8 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
|
|||||||
expect(disconnectedRemote2).toBe(1);
|
expect(disconnectedRemote2).toBe(1);
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
utils.waitForEvents(remoteBrowser1, 'disconnected'),
|
utils.waitEvent(remoteBrowser1, 'disconnected'),
|
||||||
utils.waitForEvents(originalBrowser, 'disconnected'),
|
utils.waitEvent(originalBrowser, 'disconnected'),
|
||||||
originalBrowser.close(),
|
originalBrowser.close(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const {waitForEvents} = require('./utils');
|
const {waitEvent} = require('./utils');
|
||||||
|
|
||||||
module.exports.addTests = function({testRunner, expect}) {
|
module.exports.addTests = function({testRunner, expect}) {
|
||||||
const {describe, xdescribe, fdescribe} = testRunner;
|
const {describe, xdescribe, fdescribe} = testRunner;
|
||||||
@ -110,7 +110,7 @@ module.exports.addTests = function({testRunner, expect}) {
|
|||||||
// send request and wait for server response
|
// send request and wait for server response
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
page.evaluate(() => fetch('./get', { method: 'GET'})),
|
page.evaluate(() => fetch('./get', { method: 'GET'})),
|
||||||
waitForEvents(page, 'response')
|
waitEvent(page, 'response')
|
||||||
]);
|
]);
|
||||||
|
|
||||||
expect(serverResponse).toBeTruthy();
|
expect(serverResponse).toBeTruthy();
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const utils = require('./utils');
|
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}) {
|
module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, puppeteer, PROJECT_ROOT}) {
|
||||||
const {describe, xdescribe, fdescribe} = testRunner;
|
const {describe, xdescribe, fdescribe} = testRunner;
|
||||||
@ -88,7 +88,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
|
|||||||
let error = null;
|
let error = null;
|
||||||
page.on('error', err => error = err);
|
page.on('error', err => error = err);
|
||||||
page.goto('chrome://crash').catch(e => {});
|
page.goto('chrome://crash').catch(e => {});
|
||||||
await waitForEvents(page, 'error');
|
await waitEvent(page, 'error');
|
||||||
expect(error.message).toBe('Page crashed!');
|
expect(error.message).toBe('Page crashed!');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -329,7 +329,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
|
|||||||
page.once('console', m => message = m);
|
page.once('console', m => message = m);
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
page.evaluate(() => console.log('hello', 5, {foo: 'bar'})),
|
page.evaluate(() => console.log('hello', 5, {foo: 'bar'})),
|
||||||
waitForEvents(page, 'console')
|
waitEvent(page, 'console')
|
||||||
]);
|
]);
|
||||||
expect(message.text()).toEqual('hello 5 JSHandle@object');
|
expect(message.text()).toEqual('hello 5 JSHandle@object');
|
||||||
expect(message.type()).toEqual('log');
|
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}) => {
|
it('should work for different console API calls', async({page, server}) => {
|
||||||
const messages = [];
|
const messages = [];
|
||||||
page.on('console', msg => messages.push(msg));
|
page.on('console', msg => messages.push(msg));
|
||||||
await Promise.all([
|
// All console events will be reported before `page.evaluate` is finished.
|
||||||
page.evaluate(() => {
|
await page.evaluate(() => {
|
||||||
// A pair of time/timeEnd generates only one Console API call.
|
// A pair of time/timeEnd generates only one Console API call.
|
||||||
console.time('calling console.time');
|
console.time('calling console.time');
|
||||||
console.timeEnd('calling console.time');
|
console.timeEnd('calling console.time');
|
||||||
console.trace('calling console.trace');
|
console.trace('calling console.trace');
|
||||||
console.dir('calling console.dir');
|
console.dir('calling console.dir');
|
||||||
console.warn('calling console.warn');
|
console.warn('calling console.warn');
|
||||||
console.error('calling console.error');
|
console.error('calling console.error');
|
||||||
console.log(Promise.resolve('should not wait until resolved!'));
|
console.log(Promise.resolve('should not wait until resolved!'));
|
||||||
}),
|
});
|
||||||
// Wait for 5 events to hit - console.time is not reported
|
|
||||||
waitForEvents(page, 'console', 5)
|
|
||||||
]);
|
|
||||||
expect(messages.map(msg => msg.type())).toEqual([
|
expect(messages.map(msg => msg.type())).toEqual([
|
||||||
'timeEnd', 'trace', 'dir', 'warning', 'error', 'log'
|
'timeEnd', 'trace', 'dir', 'warning', 'error', 'log'
|
||||||
]);
|
]);
|
||||||
@ -371,7 +368,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
|
|||||||
page.once('console', msg => message = msg);
|
page.once('console', msg => message = msg);
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
page.evaluate(() => console.error(window)),
|
page.evaluate(() => console.error(window)),
|
||||||
waitForEvents(page, 'console')
|
waitEvent(page, 'console')
|
||||||
]);
|
]);
|
||||||
expect(message.text()).toBe('JSHandle@object');
|
expect(message.text()).toBe('JSHandle@object');
|
||||||
});
|
});
|
||||||
@ -380,7 +377,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
|
|||||||
describe('Page.Events.DOMContentLoaded', function() {
|
describe('Page.Events.DOMContentLoaded', function() {
|
||||||
it('should fire when expected', async({page, server}) => {
|
it('should fire when expected', async({page, server}) => {
|
||||||
page.goto('about:blank');
|
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.on('request', async r => request = r);
|
||||||
page.$eval('iframe', (frame, url) => frame.src = url, server.EMPTY_PAGE),
|
page.$eval('iframe', (frame, url) => frame.src = url, server.EMPTY_PAGE),
|
||||||
// Wait for request interception.
|
// Wait for request interception.
|
||||||
await waitForEvents(page, 'request');
|
await waitEvent(page, 'request');
|
||||||
// Delete frame to cause request to be canceled.
|
// Delete frame to cause request to be canceled.
|
||||||
await page.$eval('iframe', frame => frame.remove());
|
await page.$eval('iframe', frame => frame.remove());
|
||||||
let error = null;
|
let error = null;
|
||||||
@ -1068,7 +1065,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
|
|||||||
page.once('pageerror', e => error = e);
|
page.once('pageerror', e => error = e);
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
page.goto(server.PREFIX + '/error.html'),
|
page.goto(server.PREFIX + '/error.html'),
|
||||||
waitForEvents(page, 'pageerror')
|
waitEvent(page, 'pageerror')
|
||||||
]);
|
]);
|
||||||
expect(error.message).toContain('Fancy');
|
expect(error.message).toContain('Fancy');
|
||||||
});
|
});
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const {waitForEvents} = require('./utils');
|
const {waitEvent} = require('./utils');
|
||||||
|
|
||||||
module.exports.addTests = function({testRunner, expect}) {
|
module.exports.addTests = function({testRunner, expect}) {
|
||||||
const {describe, xdescribe, fdescribe} = testRunner;
|
const {describe, xdescribe, fdescribe} = testRunner;
|
||||||
@ -126,7 +126,7 @@ module.exports.addTests = function({testRunner, expect}) {
|
|||||||
serverResponse.writeHead(302, { location: '/injectedstyle.css' });
|
serverResponse.writeHead(302, { location: '/injectedstyle.css' });
|
||||||
serverResponse.end();
|
serverResponse.end();
|
||||||
// Wait for the new page to load.
|
// Wait for the new page to load.
|
||||||
await waitForEvents(newPage, 'load');
|
await waitEvent(newPage, 'load');
|
||||||
// Cleanup.
|
// Cleanup.
|
||||||
await newPage.close();
|
await newPage.close();
|
||||||
});
|
});
|
||||||
|
@ -76,22 +76,10 @@ const utils = module.exports = {
|
|||||||
/**
|
/**
|
||||||
* @param {!EventEmitter} emitter
|
* @param {!EventEmitter} emitter
|
||||||
* @param {string} eventName
|
* @param {string} eventName
|
||||||
* @param {number=} eventCount
|
|
||||||
* @return {!Promise<!Object>}
|
* @return {!Promise<!Object>}
|
||||||
*/
|
*/
|
||||||
waitForEvents: function(emitter, eventName, eventCount = 1) {
|
waitEvent: function(emitter, eventName) {
|
||||||
let fulfill;
|
return new Promise(fulfill => emitter.once(eventName, 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);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user