chore: common types for evaluate functions (#72)

This commit is contained in:
Dmitry Gozman 2019-11-25 15:06:52 -08:00 committed by GitHub
parent 2e581f1625
commit 038e24c089
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 69 additions and 41 deletions

View file

@ -16,6 +16,7 @@
*/ */
import * as fs from 'fs'; import * as fs from 'fs';
import * as types from '../types';
import { TimeoutError } from '../Errors'; import { TimeoutError } from '../Errors';
import { ExecutionContext } from './ExecutionContext'; import { ExecutionContext } from './ExecutionContext';
import { Frame } from './Frame'; import { Frame } from './Frame';
@ -79,14 +80,14 @@ export class DOMWorld {
return this._contextPromise; return this._contextPromise;
} }
async evaluateHandle(pageFunction: Function | string, ...args: any[]): Promise<JSHandle> { evaluateHandle: types.EvaluateHandle<JSHandle> = async (pageFunction, ...args) => {
const context = await this.executionContext(); const context = await this.executionContext();
return context.evaluateHandle(pageFunction, ...args); return context.evaluateHandle(pageFunction, ...args as any);
} }
async evaluate(pageFunction: Function | string, ...args: any[]): Promise<any> { evaluate: types.Evaluate<JSHandle> = async (pageFunction, ...args) => {
const context = await this.executionContext(); const context = await this.executionContext();
return context.evaluate(pageFunction, ...args); return context.evaluate(pageFunction, ...args as any);
} }
async $(selector: string): Promise<ElementHandle | null> { async $(selector: string): Promise<ElementHandle | null> {
@ -111,14 +112,14 @@ export class DOMWorld {
return value; return value;
} }
async $eval(selector: string, pageFunction: Function | string, ...args: any[]): Promise<any> { $eval: types.$Eval<JSHandle> = async (selector, pageFunction, ...args) => {
const document = await this._document(); const document = await this._document();
return document.$eval(selector, pageFunction, ...args); return document.$eval(selector, pageFunction, ...args as any);
} }
async $$eval(selector: string, pageFunction: Function | string, ...args: any[]): Promise<any> { $$eval: types.$$Eval<JSHandle> = async (selector, pageFunction, ...args) => {
const document = await this._document(); const document = await this._document();
const value = await document.$$eval(selector, pageFunction, ...args); const value = await document.$$eval(selector, pageFunction, ...args as any);
return value; return value;
} }
@ -439,7 +440,7 @@ class WaitTask {
} }
} }
async function waitForPredicatePageFunction(predicateBody: string, polling: string, timeout: number, ...args): Promise<any> { async function waitForPredicatePageFunction(predicateBody: string, polling: string | number, timeout: number, ...args): Promise<any> {
const predicate = new Function('...args', predicateBody); const predicate = new Function('...args', predicateBody);
let timedOut = false; let timedOut = false;
if (timeout) if (timeout)

View file

@ -25,11 +25,12 @@ import { Protocol } from './protocol';
import * as injectedSource from '../generated/injectedSource'; import * as injectedSource from '../generated/injectedSource';
import * as cssSelectorEngineSource from '../generated/cssSelectorEngineSource'; import * as cssSelectorEngineSource from '../generated/cssSelectorEngineSource';
import * as xpathSelectorEngineSource from '../generated/xpathSelectorEngineSource'; import * as xpathSelectorEngineSource from '../generated/xpathSelectorEngineSource';
import * as types from '../types';
export const EVALUATION_SCRIPT_URL = '__playwright_evaluation_script__'; export const EVALUATION_SCRIPT_URL = '__playwright_evaluation_script__';
const SOURCE_URL_REGEX = /^[\040\t]*\/\/[@#] sourceURL=\s*(\S*?)\s*$/m; const SOURCE_URL_REGEX = /^[\040\t]*\/\/[@#] sourceURL=\s*(\S*?)\s*$/m;
export class ExecutionContext { export class ExecutionContext implements types.EvaluationContext<JSHandle> {
_client: CDPSession; _client: CDPSession;
_world: DOMWorld; _world: DOMWorld;
private _injectedPromise: Promise<JSHandle> | null = null; private _injectedPromise: Promise<JSHandle> | null = null;
@ -45,11 +46,11 @@ export class ExecutionContext {
return this._world ? this._world.frame() : null; return this._world ? this._world.frame() : null;
} }
async evaluate(pageFunction: Function | string, ...args: any[]): Promise<any> { evaluate: types.Evaluate<JSHandle> = (pageFunction, ...args) => {
return await this._evaluateInternal(true /* returnByValue */, pageFunction, ...args); return this._evaluateInternal(true /* returnByValue */, pageFunction, ...args);
} }
async evaluateHandle(pageFunction: Function | string, ...args: any[]): Promise<JSHandle> { evaluateHandle: types.EvaluateHandle<JSHandle> = (pageFunction, ...args) => {
return this._evaluateInternal(false /* returnByValue */, pageFunction, ...args); return this._evaluateInternal(false /* returnByValue */, pageFunction, ...args);
} }

View file

@ -15,6 +15,7 @@
* limitations under the License. * limitations under the License.
*/ */
import * as types from '../types';
import { helper, assert } from '../helper'; import { helper, assert } from '../helper';
import { ClickOptions, MultiClickOptions, PointerActionOptions, SelectOption } from '../input'; import { ClickOptions, MultiClickOptions, PointerActionOptions, SelectOption } from '../input';
import { CDPSession } from './Connection'; import { CDPSession } from './Connection';
@ -68,12 +69,12 @@ export class Frame {
return this._mainWorld.executionContext(); return this._mainWorld.executionContext();
} }
async evaluateHandle(pageFunction: Function | string, ...args: any[]): Promise<JSHandle> { evaluateHandle: types.EvaluateHandle<JSHandle> = (pageFunction, ...args) => {
return this._mainWorld.evaluateHandle(pageFunction, ...args); return this._mainWorld.evaluateHandle(pageFunction, ...args as any);
} }
async evaluate(pageFunction: Function | string, ...args: any[]): Promise<any> { evaluate: types.Evaluate<JSHandle> = (pageFunction, ...args) => {
return this._mainWorld.evaluate(pageFunction, ...args); return this._mainWorld.evaluate(pageFunction, ...args as any);
} }
async $(selector: string): Promise<ElementHandle | null> { async $(selector: string): Promise<ElementHandle | null> {
@ -84,12 +85,12 @@ export class Frame {
return this._mainWorld.$x(expression); return this._mainWorld.$x(expression);
} }
async $eval(selector: string, pageFunction: Function | string, ...args: any[]): Promise<(object | undefined)> { $eval: types.$Eval<JSHandle> = (selector, pageFunction, ...args) => {
return this._mainWorld.$eval(selector, pageFunction, ...args); return this._mainWorld.$eval(selector, pageFunction, ...args as any);
} }
async $$eval(selector: string, pageFunction: Function | string, ...args: any[]): Promise<(object | undefined)> { $$eval: types.$$Eval<JSHandle> = (selector, pageFunction, ...args) => {
return this._mainWorld.$$eval(selector, pageFunction, ...args); return this._mainWorld.$$eval(selector, pageFunction, ...args as any);
} }
async $$(selector: string): Promise<ElementHandle[]> { async $$(selector: string): Promise<ElementHandle[]> {

View file

@ -16,6 +16,7 @@
*/ */
import * as path from 'path'; import * as path from 'path';
import * as types from '../types';
import { assert, debugError, helper } from '../helper'; import { assert, debugError, helper } from '../helper';
import { ClickOptions, Modifier, MultiClickOptions, PointerActionOptions, SelectOption, selectFunction, fillFunction } from '../input'; import { ClickOptions, Modifier, MultiClickOptions, PointerActionOptions, SelectOption, selectFunction, fillFunction } from '../input';
import { CDPSession } from './Connection'; import { CDPSession } from './Connection';
@ -59,12 +60,12 @@ export class JSHandle {
return this._context; return this._context;
} }
async evaluate(pageFunction: Function | string, ...args: any[]): Promise<any> { evaluate: types.EvaluateOn<JSHandle> = (pageFunction, ...args) => {
return await this.executionContext().evaluate(pageFunction, this, ...args); return this.executionContext().evaluate(pageFunction, this, ...args);
} }
async evaluateHandle(pageFunction: Function | string, ...args: any[]): Promise<JSHandle> { evaluateHandle: types.EvaluateHandleOn<JSHandle> = (pageFunction, ...args) => {
return await this.executionContext().evaluateHandle(pageFunction, this, ...args); return this.executionContext().evaluateHandle(pageFunction, this, ...args);
} }
async getProperty(propertyName: string): Promise<JSHandle | null> { async getProperty(propertyName: string): Promise<JSHandle | null> {
@ -432,22 +433,22 @@ export class ElementHandle extends JSHandle {
return result; return result;
} }
async $eval(selector: string, pageFunction: Function | string, ...args: any[]): Promise<object | undefined> { $eval: types.$Eval<JSHandle> = async (selector, pageFunction, ...args) => {
const elementHandle = await this.$(selector); const elementHandle = await this.$(selector);
if (!elementHandle) if (!elementHandle)
throw new Error(`Error: failed to find element matching selector "${selector}"`); throw new Error(`Error: failed to find element matching selector "${selector}"`);
const result = await elementHandle.evaluate(pageFunction, ...args); const result = await elementHandle.evaluate(pageFunction, ...args as any);
await elementHandle.dispose(); await elementHandle.dispose();
return result; return result;
} }
async $$eval(selector: string, pageFunction: Function | string, ...args: any[]): Promise<object | undefined> { $$eval: types.$$Eval<JSHandle> = async (selector, pageFunction, ...args) => {
const arrayHandle = await this.evaluateHandle( const arrayHandle = await this.evaluateHandle(
(root: SelectorRoot, selector: string, injected: Injected) => injected.querySelectorAll('css=' + selector, root), (root: SelectorRoot, selector: string, injected: Injected) => injected.querySelectorAll('css=' + selector, root),
selector, await this._context._injected() selector, await this._context._injected()
); );
const result = await arrayHandle.evaluate(pageFunction, ...args); const result = await arrayHandle.evaluate(pageFunction, ...args as any);
await arrayHandle.dispose(); await arrayHandle.dispose();
return result; return result;
} }

View file

@ -44,6 +44,7 @@ import { getExceptionMessage, releaseObject, valueFromRemoteObject } from './pro
import { Target } from './Target'; import { Target } from './Target';
import { TaskQueue } from './TaskQueue'; import { TaskQueue } from './TaskQueue';
import * as input from '../input'; import * as input from '../input';
import * as types from '../types';
const writeFileAsync = helper.promisify(fs.writeFile); const writeFileAsync = helper.promisify(fs.writeFile);
@ -232,17 +233,17 @@ export class Page extends EventEmitter {
return this.mainFrame().$(selector); return this.mainFrame().$(selector);
} }
async evaluateHandle(pageFunction: Function | string, ...args: any[]): Promise<JSHandle> { evaluateHandle: types.EvaluateHandle<JSHandle> = async (pageFunction, ...args) => {
const context = await this.mainFrame().executionContext(); const context = await this.mainFrame().executionContext();
return context.evaluateHandle(pageFunction, ...args); return context.evaluateHandle(pageFunction, ...args as any);
} }
async $eval(selector: string, pageFunction: Function | string, ...args: any[]): Promise<(object | undefined)> { $eval: types.$Eval<JSHandle> = (selector, pageFunction, ...args) => {
return this.mainFrame().$eval(selector, pageFunction, ...args); return this.mainFrame().$eval(selector, pageFunction, ...args as any);
} }
async $$eval(selector: string, pageFunction: Function | string, ...args: any[]): Promise<(object | undefined)> { $$eval: types.$$Eval<JSHandle> = (selector, pageFunction, ...args) => {
return this.mainFrame().$$eval(selector, pageFunction, ...args); return this.mainFrame().$$eval(selector, pageFunction, ...args as any);
} }
async $$(selector: string): Promise<ElementHandle[]> { async $$(selector: string): Promise<ElementHandle[]> {
@ -567,8 +568,8 @@ export class Page extends EventEmitter {
return this._viewport; return this._viewport;
} }
async evaluate(pageFunction: Function | string, ...args: any[]): Promise<any> { evaluate: types.Evaluate<JSHandle> = (pageFunction, ...args) => {
return this._frameManager.mainFrame().evaluate(pageFunction, ...args); return this._frameManager.mainFrame().evaluate(pageFunction, ...args as any);
} }
async evaluateOnNewDocument(pageFunction: Function | string, ...args: any[]) { async evaluateOnNewDocument(pageFunction: Function | string, ...args: any[]) {

View file

@ -21,6 +21,7 @@ import { debugError } from '../../helper';
import { JSHandle } from '../JSHandle'; import { JSHandle } from '../JSHandle';
import { Protocol } from '../protocol'; import { Protocol } from '../protocol';
import { Events } from '../events'; import { Events } from '../events';
import * as types from '../../types';
type AddToConsoleCallback = (type: string, args: JSHandle[], stackTrace: Protocol.Runtime.StackTrace | undefined) => void; type AddToConsoleCallback = (type: string, args: JSHandle[], stackTrace: Protocol.Runtime.StackTrace | undefined) => void;
type HandleExceptionCallback = (exceptionDetails: Protocol.Runtime.ExceptionDetails) => void; type HandleExceptionCallback = (exceptionDetails: Protocol.Runtime.ExceptionDetails) => void;
@ -85,11 +86,11 @@ export class Worker extends EventEmitter {
return this._executionContextPromise; return this._executionContextPromise;
} }
async evaluate(pageFunction: Function | string, ...args: any[]): Promise<any> { evaluate: types.Evaluate<JSHandle> = async (pageFunction, ...args) => {
return (await this._executionContextPromise).evaluate(pageFunction, ...args); return (await this._executionContextPromise).evaluate(pageFunction, ...args as any);
} }
async evaluateHandle(pageFunction: Function | string, ...args: any[]): Promise<JSHandle> { evaluateHandle: types.EvaluateHandle<JSHandle> = async (pageFunction, ...args) => {
return (await this._executionContextPromise).evaluateHandle(pageFunction, ...args); return (await this._executionContextPromise).evaluateHandle(pageFunction, ...args as any);
} }
} }

22
src/types.ts Normal file
View file

@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
type Boxed<Args extends any[], Handle> = { [Index in keyof Args]: Args[Index] | Handle };
type PageFunction<Args extends any[], R = any> = string | ((...args: Args) => R | Promise<R>);
type PageFunctionOn<On, Args extends any[], R = any> = string | ((on: On, ...args: Args) => R | Promise<R>);
export type Evaluate<Handle> = <Args extends any[], R>(fn: PageFunction<Args, R>, ...args: Boxed<Args, Handle>) => Promise<R>;
export type EvaluateHandle<Handle> = <Args extends any[]>(fn: PageFunction<Args>, ...args: Boxed<Args, Handle>) => Promise<Handle>;
export type $Eval<Handle> = <Args extends any[], R>(selector: string, fn: PageFunctionOn<Element, Args, R>, ...args: Boxed<Args, Handle>) => Promise<R>;
export type $$Eval<Handle> = <Args extends any[], R>(selector: string, fn: PageFunctionOn<Element[], Args, R>, ...args: Boxed<Args, Handle>) => Promise<R>;
export type EvaluateOn<Handle> = <Args extends any[], R>(fn: PageFunctionOn<any, Args, R>, ...args: Boxed<Args, Handle>) => Promise<R>;
export type EvaluateHandleOn<Handle> = <Args extends any[]>(fn: PageFunctionOn<any, Args>, ...args: Boxed<Args, Handle>) => Promise<Handle>;
export interface EvaluationContext<Handle> {
evaluate: Evaluate<Handle>;
evaluateHandle: EvaluateHandle<Handle>;
}
export interface Handle {
dispose(): Promise<void>;
}