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

fix: throw from eval methods if >1 argument is passed #2043

Merged
merged 3 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { ConsoleMessage } from './console';
import * as dom from './dom';
import { TimeoutError, NotConnectedError } from './errors';
import { Events } from './events';
import { assert, helper, RegisteredListener } from './helper';
import { assert, helper, RegisteredListener, assertMaxArguments } from './helper';
import * as js from './javascript';
import * as network from './network';
import { Page } from './page';
Expand Down Expand Up @@ -415,13 +415,15 @@ export class Frame {
async evaluateHandle<R, Arg>(pageFunction: types.Func1<Arg, R>, arg: Arg): Promise<types.SmartHandle<R>>;
async evaluateHandle<R>(pageFunction: types.Func1<void, R>, arg?: any): Promise<types.SmartHandle<R>>;
async evaluateHandle<R, Arg>(pageFunction: types.Func1<Arg, R>, arg: Arg): Promise<types.SmartHandle<R>> {
assertMaxArguments(arguments.length, 2);
const context = await this._mainContext();
return context.evaluateHandleInternal(pageFunction, arg);
}

async evaluate<R, Arg>(pageFunction: types.Func1<Arg, R>, arg: Arg): Promise<R>;
async evaluate<R>(pageFunction: types.Func1<void, R>, arg?: any): Promise<R>;
async evaluate<R, Arg>(pageFunction: types.Func1<Arg, R>, arg: Arg): Promise<R> {
assertMaxArguments(arguments.length, 2);
const context = await this._mainContext();
return context.evaluateInternal(pageFunction, arg);
}
Expand Down Expand Up @@ -464,6 +466,7 @@ export class Frame {
async $eval<R, Arg>(selector: string, pageFunction: types.FuncOn<Element, Arg, R>, arg: Arg): Promise<R>;
async $eval<R>(selector: string, pageFunction: types.FuncOn<Element, void, R>, arg?: any): Promise<R>;
async $eval<R, Arg>(selector: string, pageFunction: types.FuncOn<Element, Arg, R>, arg: Arg): Promise<R> {
assertMaxArguments(arguments.length, 3);
const handle = await this.$(selector);
if (!handle)
throw new Error(`Error: failed to find element matching selector "${selector}"`);
Expand All @@ -475,6 +478,7 @@ export class Frame {
async $$eval<R, Arg>(selector: string, pageFunction: types.FuncOn<Element[], Arg, R>, arg: Arg): Promise<R>;
async $$eval<R>(selector: string, pageFunction: types.FuncOn<Element[], void, R>, arg?: any): Promise<R>;
async $$eval<R, Arg>(selector: string, pageFunction: types.FuncOn<Element[], Arg, R>, arg: Arg): Promise<R> {
assertMaxArguments(arguments.length, 3);
const arrayHandle = await selectors._queryArray(this, selector);
const result = await arrayHandle.evaluate(pageFunction, arg);
arrayHandle.dispose();
Expand Down
4 changes: 4 additions & 0 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ export function assert(value: any, message?: string): asserts value {
throw new Error(message);
}

export function assertMaxArguments(count: number, max: number): asserts count {
assert(count <= max, 'Too many arguments. If you need to pass more than 1 argument to the function wrap them in an object.');
}

export function getFromENV(name: string) {
let value = process.env[name];
value = value || process.env[`npm_config_${name.toLowerCase()}`];
Expand Down
8 changes: 7 additions & 1 deletion src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import * as dom from './dom';
import * as frames from './frames';
import { assert, helper, Listener } from './helper';
import { assert, helper, Listener, assertMaxArguments } from './helper';
import * as input from './input';
import * as js from './javascript';
import * as network from './network';
Expand Down Expand Up @@ -228,18 +228,21 @@ export class Page extends ExtendedEventEmitter implements InnerLogger {
async evaluateHandle<R, Arg>(pageFunction: types.Func1<Arg, R>, arg: Arg): Promise<types.SmartHandle<R>>;
async evaluateHandle<R>(pageFunction: types.Func1<void, R>, arg?: any): Promise<types.SmartHandle<R>>;
async evaluateHandle<R, Arg>(pageFunction: types.Func1<Arg, R>, arg: Arg): Promise<types.SmartHandle<R>> {
assertMaxArguments(arguments.length, 2);
return this.mainFrame().evaluateHandle(pageFunction, arg);
}

async $eval<R, Arg>(selector: string, pageFunction: types.FuncOn<Element, Arg, R>, arg: Arg): Promise<R>;
async $eval<R>(selector: string, pageFunction: types.FuncOn<Element, void, R>, arg?: any): Promise<R>;
async $eval<R, Arg>(selector: string, pageFunction: types.FuncOn<Element, Arg, R>, arg: Arg): Promise<R> {
assertMaxArguments(arguments.length, 3);
return this.mainFrame().$eval(selector, pageFunction, arg);
}

async $$eval<R, Arg>(selector: string, pageFunction: types.FuncOn<Element[], Arg, R>, arg: Arg): Promise<R>;
async $$eval<R>(selector: string, pageFunction: types.FuncOn<Element[], void, R>, arg?: any): Promise<R>;
async $$eval<R, Arg>(selector: string, pageFunction: types.FuncOn<Element[], Arg, R>, arg: Arg): Promise<R> {
assertMaxArguments(arguments.length, 3);
return this.mainFrame().$$eval(selector, pageFunction, arg);
}

Expand Down Expand Up @@ -373,6 +376,7 @@ export class Page extends ExtendedEventEmitter implements InnerLogger {
async evaluate<R, Arg>(pageFunction: types.Func1<Arg, R>, arg: Arg): Promise<R>;
async evaluate<R>(pageFunction: types.Func1<void, R>, arg?: any): Promise<R>;
async evaluate<R, Arg>(pageFunction: types.Func1<Arg, R>, arg: Arg): Promise<R> {
assertMaxArguments(arguments.length, 2);
return this.mainFrame().evaluate(pageFunction, arg);
}

Expand Down Expand Up @@ -568,12 +572,14 @@ export class Worker extends EventEmitter {
async evaluate<R, Arg>(pageFunction: types.Func1<Arg, R>, arg: Arg): Promise<R>;
async evaluate<R>(pageFunction: types.Func1<void, R>, arg?: any): Promise<R>;
async evaluate<R, Arg>(pageFunction: types.Func1<Arg, R>, arg: Arg): Promise<R> {
assertMaxArguments(arguments.length, 2);
return (await this._executionContextPromise).evaluateInternal(pageFunction, arg);
}

async evaluateHandle<R, Arg>(pageFunction: types.Func1<Arg, R>, arg: Arg): Promise<types.SmartHandle<R>>;
async evaluateHandle<R>(pageFunction: types.Func1<void, R>, arg?: any): Promise<types.SmartHandle<R>>;
async evaluateHandle<R, Arg>(pageFunction: types.Func1<Arg, R>, arg: Arg): Promise<types.SmartHandle<R>> {
assertMaxArguments(arguments.length, 2);
return (await this._executionContextPromise).evaluateHandleInternal(pageFunction, arg);
}
}
Expand Down
18 changes: 18 additions & 0 deletions test/evaluation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,24 @@ describe('Page.evaluate', function() {
const result = await page.evaluate(() => -Infinity);
expect(Object.is(result, -Infinity)).toBe(true);
});
it('should throw when passed more than one parameter', async({page, server}) => {
const expectThrow = async f => {
let error;
await f().catch(e => error = e);
expect('' + error).toContain('Too many arguments');
}
await expectThrow(() => page.evaluate((a, b) => false, 1, 2));
await expectThrow(() => page.evaluateHandle((a, b) => false, 1, 2));
await expectThrow(() => page.$eval('sel', (a, b) => false, 1, 2));
await expectThrow(() => page.$$eval('sel', (a, b) => false, 1, 2));
await expectThrow(() => page.evaluate((a, b) => false, 1, 2));
const frame = page.mainFrame();
await expectThrow(() => frame.evaluate((a, b) => false, 1, 2));
await expectThrow(() => frame.evaluateHandle((a, b) => false, 1, 2));
await expectThrow(() => frame.$eval('sel', (a, b) => false, 1, 2));
await expectThrow(() => frame.$$eval('sel', (a, b) => false, 1, 2));
await expectThrow(() => frame.evaluate((a, b) => false, 1, 2));
});
it('should accept "undefined" as one of multiple parameters', async({page, server}) => {
const result = await page.evaluate(({ a, b }) => Object.is(a, undefined) && Object.is(b, 'foo'), { a: undefined, b: 'foo' });
expect(result).toBe(true);
Expand Down