Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
chore: common types for evaluate functions (#72)
Dmitry Gozman038e24c08940
diff --git a/src/chromium/DOMWorld.ts b/src/chromium/DOMWorld.tsindex f065aa790c..93764ed67d 100644--- a/src/chromium/DOMWorld.ts+++ b/src/chromium/DOMWorld.ts@@ -16,6 +16,7 @@*/import * as fs from 'fs';+import * as types from '../types';import { TimeoutError } from '../Errors';import { ExecutionContext } from './ExecutionContext';import { Frame } from './Frame';@@ -79,14 +80,14 @@ export class DOMWorld {return this._contextPromise;}- async evaluateHandle(pageFunction: Function | string, ...args: any[]): Promise<JSHandle> {+ evaluateHandle: types.EvaluateHandle<JSHandle> = async (pageFunction, ...args) => {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();- return context.evaluate(pageFunction, ...args);+ return context.evaluate(pageFunction, ...args as any);}async $(selector: string): Promise<ElementHandle | null> {@@ -111,14 +112,14 @@ export class DOMWorld {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();- 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 value = await document.$$eval(selector, pageFunction, ...args);+ const value = await document.$$eval(selector, pageFunction, ...args as any);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);let timedOut = false;if (timeout)diff --git a/src/chromium/ExecutionContext.ts b/src/chromium/ExecutionContext.tsindex 9f111fef03..f45a185bd6 100644--- a/src/chromium/ExecutionContext.ts+++ b/src/chromium/ExecutionContext.ts@@ -25,11 +25,12 @@ import { Protocol } from './protocol';import * as injectedSource from '../generated/injectedSource';import * as cssSelectorEngineSource from '../generated/cssSelectorEngineSource';import * as xpathSelectorEngineSource from '../generated/xpathSelectorEngineSource';+import * as types from '../types';export const EVALUATION_SCRIPT_URL = '__playwright_evaluation_script__';const SOURCE_URL_REGEX = /^[\040\t]*\/\/[@#] sourceURL=\s*(\S*?)\s*$/m;-export class ExecutionContext {+export class ExecutionContext implements types.EvaluationContext<JSHandle> {_client: CDPSession;_world: DOMWorld;private _injectedPromise: Promise<JSHandle> | null = null;@@ -45,11 +46,11 @@ export class ExecutionContext {return this._world ? this._world.frame() : null;}- async evaluate(pageFunction: Function | string, ...args: any[]): Promise<any> {- return await this._evaluateInternal(true /* returnByValue */, pageFunction, ...args);+ evaluate: types.Evaluate<JSHandle> = (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);}diff --git a/src/chromium/Frame.ts b/src/chromium/Frame.tsindex 719d0d7edd..94524202cc 100644--- a/src/chromium/Frame.ts+++ b/src/chromium/Frame.ts@@ -15,6 +15,7 @@* limitations under the License.*/+import * as types from '../types';import { helper, assert } from '../helper';import { ClickOptions, MultiClickOptions, PointerActionOptions, SelectOption } from '../input';import { CDPSession } from './Connection';@@ -68,12 +69,12 @@ export class Frame {return this._mainWorld.executionContext();}- async evaluateHandle(pageFunction: Function | string, ...args: any[]): Promise<JSHandle> {- return this._mainWorld.evaluateHandle(pageFunction, ...args);+ evaluateHandle: types.EvaluateHandle<JSHandle> = (pageFunction, ...args) => {+ return this._mainWorld.evaluateHandle(pageFunction, ...args as any);}- async evaluate(pageFunction: Function | string, ...args: any[]): Promise<any> {- return this._mainWorld.evaluate(pageFunction, ...args);+ evaluate: types.Evaluate<JSHandle> = (pageFunction, ...args) => {+ return this._mainWorld.evaluate(pageFunction, ...args as any);}async $(selector: string): Promise<ElementHandle | null> {@@ -84,12 +85,12 @@ export class Frame {return this._mainWorld.$x(expression);}- async $eval(selector: string, pageFunction: Function | string, ...args: any[]): Promise<(object | undefined)> {- return this._mainWorld.$eval(selector, pageFunction, ...args);+ $eval: types.$Eval<JSHandle> = (selector, pageFunction, ...args) => {+ return this._mainWorld.$eval(selector, pageFunction, ...args as any);}- async $$eval(selector: string, pageFunction: Function | string, ...args: any[]): Promise<(object | undefined)> {- return this._mainWorld.$$eval(selector, pageFunction, ...args);+ $$eval: types.$$Eval<JSHandle> = (selector, pageFunction, ...args) => {+ return this._mainWorld.$$eval(selector, pageFunction, ...args as any);}async $$(selector: string): Promise<ElementHandle[]> {diff --git a/src/chromium/JSHandle.ts b/src/chromium/JSHandle.tsindex 370e869117..da6d993259 100644--- a/src/chromium/JSHandle.ts+++ b/src/chromium/JSHandle.ts@@ -16,6 +16,7 @@*/import * as path from 'path';+import * as types from '../types';import { assert, debugError, helper } from '../helper';import { ClickOptions, Modifier, MultiClickOptions, PointerActionOptions, SelectOption, selectFunction, fillFunction } from '../input';import { CDPSession } from './Connection';@@ -59,12 +60,12 @@ export class JSHandle {return this._context;}- async evaluate(pageFunction: Function | string, ...args: any[]): Promise<any> {- return await this.executionContext().evaluate(pageFunction, this, ...args);+ evaluate: types.EvaluateOn<JSHandle> = (pageFunction, ...args) => {+ return this.executionContext().evaluate(pageFunction, this, ...args);}- async evaluateHandle(pageFunction: Function | string, ...args: any[]): Promise<JSHandle> {- return await this.executionContext().evaluateHandle(pageFunction, this, ...args);+ evaluateHandle: types.EvaluateHandleOn<JSHandle> = (pageFunction, ...args) => {+ return this.executionContext().evaluateHandle(pageFunction, this, ...args);}async getProperty(propertyName: string): Promise<JSHandle | null> {@@ -432,22 +433,22 @@ export class ElementHandle extends JSHandle {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);if (!elementHandle)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();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((root: SelectorRoot, selector: string, injected: Injected) => injected.querySelectorAll('css=' + selector, root),selector, await this._context._injected());- const result = await arrayHandle.evaluate(pageFunction, ...args);+ const result = await arrayHandle.evaluate(pageFunction, ...args as any);await arrayHandle.dispose();return result;}diff --git a/src/chromium/Page.ts b/src/chromium/Page.tsindex 357043266f..8f4745c560 100644--- a/src/chromium/Page.ts+++ b/src/chromium/Page.ts@@ -44,6 +44,7 @@ import { getExceptionMessage, releaseObject, valueFromRemoteObject } from './proimport { Target } from './Target';import { TaskQueue } from './TaskQueue';import * as input from '../input';+import * as types from '../types';const writeFileAsync = helper.promisify(fs.writeFile);@@ -232,17 +233,17 @@ export class Page extends EventEmitter {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();- return context.evaluateHandle(pageFunction, ...args);+ return context.evaluateHandle(pageFunction, ...args as any);}- async $eval(selector: string, pageFunction: Function | string, ...args: any[]): Promise<(object | undefined)> {- return this.mainFrame().$eval(selector, pageFunction, ...args);+ $eval: types.$Eval<JSHandle> = (selector, pageFunction, ...args) => {+ return this.mainFrame().$eval(selector, pageFunction, ...args as any);}- async $$eval(selector: string, pageFunction: Function | string, ...args: any[]): Promise<(object | undefined)> {- return this.mainFrame().$$eval(selector, pageFunction, ...args);+ $$eval: types.$$Eval<JSHandle> = (selector, pageFunction, ...args) => {+ return this.mainFrame().$$eval(selector, pageFunction, ...args as any);}async $$(selector: string): Promise<ElementHandle[]> {@@ -567,8 +568,8 @@ export class Page extends EventEmitter {return this._viewport;}- async evaluate(pageFunction: Function | string, ...args: any[]): Promise<any> {- return this._frameManager.mainFrame().evaluate(pageFunction, ...args);+ evaluate: types.Evaluate<JSHandle> = (pageFunction, ...args) => {+ return this._frameManager.mainFrame().evaluate(pageFunction, ...args as any);}async evaluateOnNewDocument(pageFunction: Function | string, ...args: any[]) {diff --git a/src/chromium/features/workers.ts b/src/chromium/features/workers.tsindex e5de3d7276..c67d64c686 100644--- a/src/chromium/features/workers.ts+++ b/src/chromium/features/workers.ts@@ -21,6 +21,7 @@ import { debugError } from '../../helper';import { JSHandle } from '../JSHandle';import { Protocol } from '../protocol';import { Events } from '../events';+import * as types from '../../types';type AddToConsoleCallback = (type: string, args: JSHandle[], stackTrace: Protocol.Runtime.StackTrace | undefined) => void;type HandleExceptionCallback = (exceptionDetails: Protocol.Runtime.ExceptionDetails) => void;@@ -85,11 +86,11 @@ export class Worker extends EventEmitter {return this._executionContextPromise;}- async evaluate(pageFunction: Function | string, ...args: any[]): Promise<any> {- return (await this._executionContextPromise).evaluate(pageFunction, ...args);+ evaluate: types.Evaluate<JSHandle> = async (pageFunction, ...args) => {+ return (await this._executionContextPromise).evaluate(pageFunction, ...args as any);}- async evaluateHandle(pageFunction: Function | string, ...args: any[]): Promise<JSHandle> {- return (await this._executionContextPromise).evaluateHandle(pageFunction, ...args);+ evaluateHandle: types.EvaluateHandle<JSHandle> = async (pageFunction, ...args) => {+ return (await this._executionContextPromise).evaluateHandle(pageFunction, ...args as any);}}diff --git a/src/types.ts b/src/types.tsnew file mode 100644index 0000000000..5268432c81--- /dev/null+++ b/src/types.ts@@ -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>;+}