From 6c1c3a0c45be1fdc0010fcc33be3185af90e4003 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Tue, 18 Jul 2017 00:03:52 -0700 Subject: [PATCH] Return remote object description for unserializable objects This patch starts handling unserializable objects by returning their description. Fixes #86. --- lib/helper.js | 22 +++++++++++++++------- test/test.js | 11 +++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/lib/helper.js b/lib/helper.js index 01eebd60..11a455d2 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -75,13 +75,21 @@ class Helper { } if (!remoteObject.objectId) return remoteObject.value; - let response = await client.send('Runtime.callFunctionOn', { - objectId: remoteObject.objectId, - functionDeclaration: 'function() { return this; }', - returnByValue: true, - }); - client.send('Runtime.releaseObject', {objectId: remoteObject.objectId}); - return response.result.value; + try { + let response = await client.send('Runtime.callFunctionOn', { + objectId: remoteObject.objectId, + functionDeclaration: 'function() { return this; }', + returnByValue: true, + }); + return response.result.value; + } catch (e) { + // Return description for unserializable object, e.g. 'window'. + return remoteObject.description; + } finally { + client.send('Runtime.releaseObject', {objectId: remoteObject.objectId}).catch(e => { + // While we were serializing object, the page might've navigated. + }); + } } } diff --git a/test/test.js b/test/test.js index 1d23274a..0cd9be95 100644 --- a/test/test.js +++ b/test/test.js @@ -128,6 +128,10 @@ describe('Puppeteer', function() { let result = await page.evaluate(() => -Infinity); expect(Object.is(result, -Infinity)).toBe(true); })); + it('should not fail for window object', SX(async function() { + let result = await page.evaluate(() => window); + expect(result).toBe('Window'); + })); }); describe('Page.injectFile', function() { @@ -256,6 +260,13 @@ describe('Puppeteer', function() { 'calling console.error', ]); })); + it('should not fail for window object', SX(async function() { + let windowObj = null; + page.once('console', arg => windowObj = arg); + page.evaluate(() => console.error(window)); + await waitForEvents(page, 'console'); + expect(windowObj).toBe('Window'); + })); }); describe('Page.navigate', function() {