fix: return undefined when Page.evaluate encounters circular JSON (#983)

This commit is contained in:
JoelEinbinder 2017-10-09 12:30:05 -07:00 committed by GitHub
parent 3ecd98d634
commit a7672acb85
3 changed files with 7 additions and 4 deletions

View File

@ -500,6 +500,8 @@ 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 [Promise], then `page.evaluate` would wait for the promise to resolve and return its value.
If the function passed into `page.evaluate` returns a non-[Serializable] value, then `page.evaluate` resolves to `undefined`.
```js ```js
const result = await page.evaluate(() => { const result = await page.evaluate(() => {
return Promise.resolve(8 * 7); return Promise.resolve(8 * 7);
@ -1270,6 +1272,8 @@ Adds a `<link rel="stylesheet">` tag to the frame with the desired url.
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 [Promise], then `frame.evaluate` would wait for the promise to resolve and return its value.
If the function passed into `frame.evaluate` returns a non-[Serializable] value, then `frame.evaluate` resolves to `undefined`.
```js ```js
const result = await frame.evaluate(() => { const result = await frame.evaluate(() => {
return Promise.resolve(8 * 7); return Promise.resolve(8 * 7);

View File

@ -35,7 +35,7 @@ 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(); const result = await handle.jsonValue().catch(error => undefined);
await handle.dispose(); await handle.dispose();
return result; return result;
} }

View File

@ -266,9 +266,8 @@ describe('Page', function() {
expect(result).toBe(true); expect(result).toBe(true);
})); }));
it('should fail for window object', SX(async function() { it('should fail for window object', SX(async function() {
let error = null; const result = await page.evaluate(() => window);
await page.evaluate(() => window).catch(e => error = e); expect(result).toBe(undefined);
expect(error.message).toContain('Converting circular structure to JSON');
})); }));
it('should accept a string', SX(async function() { it('should accept a string', SX(async function() {
const result = await page.evaluate('1 + 2'); const result = await page.evaluate('1 + 2');