diff --git a/src/webkit/Browser.ts b/src/webkit/Browser.ts index 316232b995..e5e63eded9 100644 --- a/src/webkit/Browser.ts +++ b/src/webkit/Browser.ts @@ -75,7 +75,7 @@ export class Browser extends EventEmitter { async userAgent(): Promise { const context = await this.createIncognitoBrowserContext(); const page = await context.newPage(); - const userAgent = await page.evaluate('navigator.userAgent'); + const userAgent = await page.evaluate(() => navigator.userAgent); context.close(); return userAgent; } diff --git a/src/webkit/ExecutionContext.ts b/src/webkit/ExecutionContext.ts index 6274543437..08d94a894d 100644 --- a/src/webkit/ExecutionContext.ts +++ b/src/webkit/ExecutionContext.ts @@ -24,11 +24,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 { _globalObjectId?: string; _session: TargetSession; _frame: Frame; @@ -55,11 +56,11 @@ export class ExecutionContext { return this._frame; } - async evaluate(pageFunction: Function | string, ...args: any[]): Promise { - return await this._evaluateInternal(true /* returnByValue */, pageFunction, ...args); + evaluate: types.Evaluate = (pageFunction, ...args) => { + return this._evaluateInternal(true /* returnByValue */, pageFunction, ...args); } - async evaluateHandle(pageFunction: Function | string, ...args: any[]): Promise { + evaluateHandle: types.EvaluateHandle = (pageFunction, ...args) => { return this._evaluateInternal(false /* returnByValue */, pageFunction, ...args); } diff --git a/src/webkit/FrameManager.ts b/src/webkit/FrameManager.ts index d78f513fb2..2781ae866f 100644 --- a/src/webkit/FrameManager.ts +++ b/src/webkit/FrameManager.ts @@ -14,8 +14,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + import * as EventEmitter from 'events'; import * as fs from 'fs'; +import * as types from '../types'; import { TimeoutError } from '../Errors'; import { Events } from './events'; import { assert, debugError, helper, RegisteredListener } from '../helper'; @@ -318,14 +320,14 @@ export class Frame { return this._contextPromise; } - async evaluateHandle(pageFunction: Function | string, ...args: Array): Promise { + evaluateHandle: types.EvaluateHandle = 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: Array): Promise { + evaluate: types.Evaluate = async (pageFunction, ...args) => { const context = await this.executionContext(); - return context.evaluate(pageFunction, ...args); + return context.evaluate(pageFunction, ...args as any); } async $(selector: string): Promise { @@ -350,14 +352,14 @@ export class Frame { return value; } - async $eval(selector: string, pageFunction: Function | string, ...args: Array): Promise<(any)> { + $eval: types.$Eval = 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: Array): Promise<(any)> { + $$eval: types.$$Eval = 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; } diff --git a/src/webkit/JSHandle.ts b/src/webkit/JSHandle.ts index f4af598f82..c88df4d6f0 100644 --- a/src/webkit/JSHandle.ts +++ b/src/webkit/JSHandle.ts @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + import * as fs from 'fs'; import { assert, debugError, helper } from '../helper'; import { ClickOptions, MultiClickOptions, selectFunction, SelectOption, fillFunction } from '../input'; @@ -24,6 +25,7 @@ import { Page } from './Page'; import { Protocol } from './protocol'; import { releaseObject, valueFromRemoteObject } from './protocolHelper'; import Injected from '../injected/injected'; +import * as types from '../types'; type SelectorRoot = Element | ShadowRoot | Document; @@ -54,12 +56,12 @@ export class JSHandle { return this._context; } - async evaluate(pageFunction: Function | string, ...args: any[]): Promise { - return await this.executionContext().evaluate(pageFunction, this, ...args); + evaluate: types.EvaluateOn = (pageFunction, ...args) => { + return this.executionContext().evaluate(pageFunction, this, ...args); } - async evaluateHandle(pageFunction: Function | string, ...args: any[]): Promise { - return await this.executionContext().evaluateHandle(pageFunction, this, ...args); + evaluateHandle: types.EvaluateHandleOn = (pageFunction, ...args) => { + return this.executionContext().evaluateHandle(pageFunction, this, ...args); } async getProperty(propertyName: string): Promise { @@ -311,22 +313,22 @@ export class ElementHandle extends JSHandle { return result; } - async $eval(selector: string, pageFunction: Function | string, ...args: any[]): Promise { + $eval: types.$Eval = 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 { + $$eval: types.$$Eval = 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/webkit/Page.ts b/src/webkit/Page.ts index 0070dba091..80839d8440 100644 --- a/src/webkit/Page.ts +++ b/src/webkit/Page.ts @@ -33,6 +33,7 @@ import { valueFromRemoteObject } from './protocolHelper'; import { Target } from './Target'; import { TaskQueue } from './TaskQueue'; import * as input from '../input'; +import * as types from '../types'; const writeFileAsync = helper.promisify(fs.writeFile); @@ -200,17 +201,17 @@ export class Page extends EventEmitter { return this.mainFrame().$(selector); } - async evaluateHandle(pageFunction: Function | string, ...args: any[]): Promise { + evaluateHandle: types.EvaluateHandle = 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 = (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 = (selector, pageFunction, ...args) => { + return this.mainFrame().$$eval(selector, pageFunction, ...args as any); } async $$(selector: string): Promise { @@ -349,8 +350,8 @@ export class Page extends EventEmitter { return this._viewport; } - async evaluate(pageFunction: Function | string, ...args: any[]): Promise { - return this._frameManager.mainFrame().evaluate(pageFunction, ...args); + evaluate: types.Evaluate = (pageFunction, ...args) => { + return this._frameManager.mainFrame().evaluate(pageFunction, ...args as any); } async evaluateOnNewDocument(pageFunction: Function | string, ...args: Array) {