Skip to content

Commit

Permalink
Add support for unserializable values in Page.evaluate
Browse files Browse the repository at this point in the history
This patch adds support for unserializable return values in
Page.evaluate.

Currently, these values are:
- NaN
- Infinity
- -Infinity
- -0

Fixes #51.
  • Loading branch information
aslushnikov committed Jul 6, 2017
1 parent 62111fb commit 773a09a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
25 changes: 23 additions & 2 deletions lib/FrameManager.js
Expand Up @@ -169,12 +169,33 @@ class FrameManager extends EventEmitter {
}
let syncExpression = helper.evaluationString(fun, ...args);
let expression = `Promise.resolve(${syncExpression})`;
let { exceptionDetails, result: remoteObject } = await this._client.send('Runtime.evaluate', { expression, contextId, returnByValue: true, awaitPromise: true });
let { exceptionDetails, result: remoteObject } = await this._client.send('Runtime.evaluate', { expression, contextId, returnByValue: false, awaitPromise: true });
if (exceptionDetails) {
let message = await helper.getExceptionMessage(this._client, exceptionDetails);
throw new Error('Evaluation failed: ' + message);
}
return remoteObject.value;
if (remoteObject.unserializableValue) {
switch (remoteObject.unserializableValue) {
case '-0':
return -0;
case 'NaN':
return NaN;
case 'Infinity':
return Infinity;
case '-Infinity':
return -Infinity;
default:
throw new Error('Unsupported unserializable value: ' + remoteObject.unserializableValue);
}
}
if (!remoteObject.objectId)
return remoteObject.value;
let response = await this._client.send('Runtime.callFunctionOn', {
objectId: remoteObject.objectId,
functionDeclaration: 'function() { return this; }',
returnByValue: true,
});
return response.result.value;
}
}

Expand Down
22 changes: 22 additions & 0 deletions test/test.js
Expand Up @@ -80,6 +80,28 @@ describe('Puppeteer', function() {
expect(error).toBeTruthy();
expect(error.message).toContain('not is not defined');
}));
it('should return complex objects', SX(async function() {
const object = {foo: 'bar!'};
let result = await page.evaluate(a => a, object);
expect(result).not.toBe(object);
expect(result).toEqual(object);
}));
it('should return NaN', SX(async function() {
let result = await page.evaluate(() => NaN);
expect(Object.is(result, NaN)).toBe(true);
}));
it('should return -0', SX(async function() {
let result = await page.evaluate(() => -0);
expect(Object.is(result, -0)).toBe(true);
}));
it('should return Infinity', SX(async function() {
let result = await page.evaluate(() => Infinity);
expect(Object.is(result, Infinity)).toBe(true);
}));
it('should return -Infinity', SX(async function() {
let result = await page.evaluate(() => -Infinity);
expect(Object.is(result, -Infinity)).toBe(true);
}));
});

describe('Frame.evaluate', function() {
Expand Down

0 comments on commit 773a09a

Please sign in to comment.