mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
[tests] Await all page.evaluate() calls in all tests (#186)
If the evaluation is not awaited for, then the page might be closed before it's finished, and an exception will be thrown.
This commit is contained in:
parent
dbe8660082
commit
3f0306a4d0
57
test/test.js
57
test/test.js
@ -432,24 +432,28 @@ describe('Page', function() {
|
|||||||
it('should work', SX(async function() {
|
it('should work', SX(async function() {
|
||||||
let commandArgs = [];
|
let commandArgs = [];
|
||||||
page.once('console', (...args) => commandArgs = args);
|
page.once('console', (...args) => commandArgs = args);
|
||||||
page.evaluate(() => console.log(5, 'hello', {foo: 'bar'}));
|
await Promise.all([
|
||||||
await waitForEvents(page, 'console');
|
page.evaluate(() => console.log(5, 'hello', {foo: 'bar'})),
|
||||||
|
waitForEvents(page, 'console')
|
||||||
|
]);
|
||||||
expect(commandArgs).toEqual([5, 'hello', {foo: 'bar'}]);
|
expect(commandArgs).toEqual([5, 'hello', {foo: 'bar'}]);
|
||||||
}));
|
}));
|
||||||
it('should work for different console API calls', SX(async function() {
|
it('should work for different console API calls', SX(async function() {
|
||||||
let messages = [];
|
let messages = [];
|
||||||
page.on('console', msg => messages.push(msg));
|
page.on('console', msg => messages.push(msg));
|
||||||
page.evaluate(() => {
|
await Promise.all([
|
||||||
// A pair of time/timeEnd generates only one Console API call.
|
page.evaluate(() => {
|
||||||
console.time('calling console.time');
|
// A pair of time/timeEnd generates only one Console API call.
|
||||||
console.timeEnd('calling console.time');
|
console.time('calling console.time');
|
||||||
console.trace('calling console.trace');
|
console.timeEnd('calling console.time');
|
||||||
console.dir('calling console.dir');
|
console.trace('calling console.trace');
|
||||||
console.warn('calling console.warn');
|
console.dir('calling console.dir');
|
||||||
console.error('calling console.error');
|
console.warn('calling console.warn');
|
||||||
});
|
console.error('calling console.error');
|
||||||
// Wait for 5 events to hit.
|
}),
|
||||||
await waitForEvents(page, 'console', 5);
|
// Wait for 5 events to hit.
|
||||||
|
waitForEvents(page, 'console', 5)
|
||||||
|
]);
|
||||||
expect(messages[0]).toContain('calling console.time');
|
expect(messages[0]).toContain('calling console.time');
|
||||||
expect(messages.slice(1)).toEqual([
|
expect(messages.slice(1)).toEqual([
|
||||||
'calling console.trace',
|
'calling console.trace',
|
||||||
@ -461,8 +465,10 @@ describe('Page', function() {
|
|||||||
it('should not fail for window object', SX(async function() {
|
it('should not fail for window object', SX(async function() {
|
||||||
let windowObj = null;
|
let windowObj = null;
|
||||||
page.once('console', arg => windowObj = arg);
|
page.once('console', arg => windowObj = arg);
|
||||||
page.evaluate(() => console.error(window));
|
await Promise.all([
|
||||||
await waitForEvents(page, 'console');
|
page.evaluate(() => console.error(window)),
|
||||||
|
waitForEvents(page, 'console')
|
||||||
|
]);
|
||||||
expect(windowObj).toBe('Window');
|
expect(windowObj).toBe('Window');
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
@ -644,8 +650,10 @@ describe('Page', function() {
|
|||||||
describe('Page.waitForNavigation', function() {
|
describe('Page.waitForNavigation', function() {
|
||||||
it('should work', SX(async function() {
|
it('should work', SX(async function() {
|
||||||
await page.navigate(EMPTY_PAGE);
|
await page.navigate(EMPTY_PAGE);
|
||||||
const result = page.waitForNavigation();
|
const [result] = await Promise.all([
|
||||||
page.evaluate(url => window.location.href = url, PREFIX + '/grid.html');
|
page.waitForNavigation(),
|
||||||
|
page.evaluate(url => window.location.href = url, PREFIX + '/grid.html')
|
||||||
|
]);
|
||||||
const response = await result;
|
const response = await result;
|
||||||
expect(response.ok).toBe(true);
|
expect(response.ok).toBe(true);
|
||||||
expect(response.url).toContain('grid.html');
|
expect(response.url).toContain('grid.html');
|
||||||
@ -745,11 +753,10 @@ describe('Page', function() {
|
|||||||
request.headers.set('foo', 'bar');
|
request.headers.set('foo', 'bar');
|
||||||
request.continue();
|
request.continue();
|
||||||
});
|
});
|
||||||
let serverRequest = server.waitForRequest('/sleep.zzz');
|
const [request] = await Promise.all([
|
||||||
page.evaluate(() => {
|
server.waitForRequest('/sleep.zzz'),
|
||||||
fetch('/sleep.zzz');
|
page.evaluate(() => fetch('/sleep.zzz'))
|
||||||
});
|
]);
|
||||||
let request = await serverRequest;
|
|
||||||
expect(request.headers['foo']).toBe('bar');
|
expect(request.headers['foo']).toBe('bar');
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
@ -1228,8 +1235,10 @@ describe('Page', function() {
|
|||||||
page.on('response', r => pageResponse = r);
|
page.on('response', r => pageResponse = r);
|
||||||
page.on('requestfinished', () => requestFinished = true);
|
page.on('requestfinished', () => requestFinished = true);
|
||||||
// send request and wait for server response
|
// send request and wait for server response
|
||||||
page.evaluate(() => fetch('./get', { method: 'GET'}));
|
await Promise.all([
|
||||||
await waitForEvents(page, 'response');
|
page.evaluate(() => fetch('./get', { method: 'GET'})),
|
||||||
|
waitForEvents(page, 'response')
|
||||||
|
]);
|
||||||
|
|
||||||
expect(serverResponse).toBeTruthy();
|
expect(serverResponse).toBeTruthy();
|
||||||
expect(pageResponse).toBeTruthy();
|
expect(pageResponse).toBeTruthy();
|
||||||
|
Loading…
Reference in New Issue
Block a user