chore: use types for eval functions in webkit (#79)

This commit is contained in:
Dmitry Gozman 2019-11-26 07:53:48 -08:00 committed by Pavel Feldman
parent 432116ba80
commit cefebb3938
5 changed files with 35 additions and 29 deletions

View file

@ -75,7 +75,7 @@ export class Browser extends EventEmitter {
async userAgent(): Promise<string> {
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;
}

View file

@ -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<JSHandle> {
_globalObjectId?: string;
_session: TargetSession;
_frame: Frame;
@ -55,11 +56,11 @@ export class ExecutionContext {
return this._frame;
}
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);
}

View file

@ -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<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: Array<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> {
@ -350,14 +352,14 @@ export class Frame {
return value;
}
async $eval(selector: string, pageFunction: Function | string, ...args: Array<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: Array<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;
}

View file

@ -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<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> {
@ -311,22 +313,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;
}

View file

@ -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<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[]> {
@ -349,8 +350,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: Array<any>) {