fix(evaluate): throw error when page reloads during page.evaluate. (#2073)

Fixes #2021.
This commit is contained in:
Andrey Lushnikov 2018-02-22 19:10:17 -08:00 committed by GitHub
parent 66887743ea
commit fc2a10440f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -44,7 +44,11 @@ class ExecutionContext {
*/ */
async evaluate(pageFunction, ...args) { async evaluate(pageFunction, ...args) {
const handle = await this.evaluateHandle(pageFunction, ...args); const handle = await this.evaluateHandle(pageFunction, ...args);
const result = await handle.jsonValue().catch(error => undefined); const result = await handle.jsonValue().catch(error => {
if (error.message.includes('Object reference chain is too long'))
return;
throw error;
});
await handle.dispose(); await handle.dispose();
return result; return result;
} }

View File

@ -425,6 +425,16 @@ describe('Page', function() {
const result = await page.evaluate(() => 7 * 3); const result = await page.evaluate(() => 7 * 3);
expect(result).toBe(21); expect(result).toBe(21);
}); });
it('should throw when evaluation triggers reload', async({page, server}) => {
let error = null;
await page.evaluate(() => {
location.reload();
return new Promise(resolve => {
setTimeout(() => resolve(1), 0);
});
}).catch(e => error = e);
expect(error.message).toContain('Protocol error');
});
it('should await promise', async({page, server}) => { it('should await promise', async({page, server}) => {
const result = await page.evaluate(() => Promise.resolve(8 * 7)); const result = await page.evaluate(() => Promise.resolve(8 * 7));
expect(result).toBe(56); expect(result).toBe(56);
@ -486,6 +496,15 @@ describe('Page', function() {
const result = await page.evaluate(() => window); const result = await page.evaluate(() => window);
expect(result).toBe(undefined); expect(result).toBe(undefined);
}); });
it('should fail for circular object', async({page, server}) => {
const result = await page.evaluate(() => {
const a = {};
const b = {a};
a.b = b;
return a;
});
expect(result).toBe(undefined);
});
it('should accept a string', async({page, server}) => { it('should accept a string', async({page, server}) => {
const result = await page.evaluate('1 + 2'); const result = await page.evaluate('1 + 2');
expect(result).toBe(3); expect(result).toBe(3);