Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(Page): evaluate should return a special message in case of non serialisable returned value #2425

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ List of all available devices is available in the source code: [DeviceDescriptor

If the function passed to the `page.evaluate` returns a [Promise], then `page.evaluate` would wait for the promise to resolve and return its value.

If the function passed to the `page.evaluate` returns a non-[Serializable] value, then `page.evaluate` resolves to `undefined`.
If the function passed to the `page.evaluate` returns a non-[Serializable] value, then `page.evaluate` resolves to `Non serializable object returned from evaluate` message.

Passing arguments to `pageFunction`:
```js
Expand Down Expand Up @@ -1802,7 +1802,7 @@ Gets the full HTML contents of the frame, including the doctype.

If the function passed to the `frame.evaluate` returns a [Promise], then `frame.evaluate` would wait for the promise to resolve and return its value.

If the function passed to the `frame.evaluate` returns a non-[Serializable] value, then `frame.evaluate` resolves to `undefined`.
If the function passed to the `frame.evaluate` returns a non-[Serializable] value, then `frame.evaluate` resolves to `Non serializable object returned from evaluate` message.

```js
const result = await frame.evaluate(() => {
Expand Down
4 changes: 2 additions & 2 deletions lib/ExecutionContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ class ExecutionContext {
const handle = await this.evaluateHandle(pageFunction, ...args);
const result = await handle.jsonValue().catch(error => {
if (error.message.includes('Object reference chain is too long'))
return;
return helper.getNonSerializableMessage();
if (error.message.includes('Object couldn\'t be returned by value'))
return;
return helper.getNonSerializableMessage();
throw error;
});
await handle.dispose();
Expand Down
4 changes: 4 additions & 0 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ class Helper {
apiCoverage = new Map();
}

static getNonSerializableMessage() {
return 'Non serializable object returned from evaluate';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of returning a string, this should be a Symbol exposed on the puppeteer namespace. This way you'll be able to do the following:

const result = await page.evaluate(() => window);
if (result === puppeteer.NON_SERIALIZABLE_OBJECT) {
  // ...
}

}

/**
* @param {!Object} obj
* @return {boolean}
Expand Down
9 changes: 5 additions & 4 deletions test/page.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
const fs = require('fs');
const path = require('path');
const utils = require('./utils');
const {helper} = require('../lib/helper');
const {waitEvent, getPDFPages, cssPixelsToInches} = require('./utils');

module.exports.addTests = function({testRunner, expect, puppeteer, DeviceDescriptors, headless}) {
Expand Down Expand Up @@ -124,9 +125,9 @@ module.exports.addTests = function({testRunner, expect, puppeteer, DeviceDescrip
it('should properly serialize null fields', async({page}) => {
expect(await page.evaluate(() => ({a: undefined}))).toEqual({});
});
it('should return undefined for non-serializable objects', async({page, server}) => {
expect(await page.evaluate(() => window)).toBe(undefined);
expect(await page.evaluate(() => [Symbol('foo4')])).toBe(undefined);
it('should return \'Non serializable object returned from evaluate\' for non-serializable objects', async({page, server}) => {
expect(await page.evaluate(() => window)).toBe(helper.getNonSerializableMessage());
expect(await page.evaluate(() => [Symbol('foo4')])).toBe(helper.getNonSerializableMessage());
});
it('should fail for circular object', async({page, server}) => {
const result = await page.evaluate(() => {
Expand All @@ -135,7 +136,7 @@ module.exports.addTests = function({testRunner, expect, puppeteer, DeviceDescrip
a.b = b;
return a;
});
expect(result).toBe(undefined);
expect(result).toBe(helper.getNonSerializableMessage());
});
it('should accept a string', async({page, server}) => {
const result = await page.evaluate('1 + 2');
Expand Down