Fix evaluation in case of first "undefind" argument (#732)

It turns out that [undefined, 1].join(',') results in ",1" instead
of "undefined,1". This causes a syntax error when trying to pass undefined
as a first argument to `page.evaluate` method.

Fixes #572.
This commit is contained in:
Andrey Lushnikov 2017-09-11 16:59:38 -07:00 committed by GitHub
parent e5c17eecb9
commit a71f287c6b
2 changed files with 15 additions and 1 deletions

View File

@ -28,7 +28,17 @@ class Helper {
console.assert(args.length === 0, 'Cannot evaluate a string with arguments');
return fun;
}
return `(${fun})(${args.map(x => JSON.stringify(x)).join(',')})`;
return `(${fun})(${args.map(serializeArgument).join(',')})`;
/**
* @param {*} arg
* @return {string}
*/
function serializeArgument(arg) {
if (Object.is(arg, undefined))
return 'undefined';
return JSON.stringify(arg);
}
}
/**

View File

@ -246,6 +246,10 @@ describe('Page', function() {
const result = await page.evaluate(() => -Infinity);
expect(Object.is(result, -Infinity)).toBe(true);
}));
it('should accept "undefined" as one of multiple parameters', SX(async function() {
const result = await page.evaluate((a, b) => Object.is(a, undefined) && Object.is(b, 'foo'), undefined, 'foo');
expect(result).toBe(true);
}));
it('should not fail for window object', SX(async function() {
const result = await page.evaluate(() => window);
expect(result).toBe('Window');