chore: use eval types in firefox (#77)

This commit is contained in:
Dmitry Gozman 2019-11-25 16:42:37 -08:00 committed by GitHub
parent d756f616f5
commit 371394ff2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 37 deletions

View file

@ -1,7 +1,25 @@
/**
* Copyright 2019 Google Inc. All rights reserved.
* Modifications copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {helper, assert} from '../helper';
import {TimeoutError} from '../Errors';
import * as fs from 'fs';
import * as util from 'util';
import * as types from '../types';
import {ElementHandle, JSHandle} from './JSHandle';
import { ExecutionContext } from './ExecutionContext';
@ -61,14 +79,14 @@ export class DOMWorld {
throw new Error('Method not implemented.');
}
async evaluateHandle(pageFunction, ...args): 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, ...args): 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> {
@ -87,12 +105,12 @@ export class DOMWorld {
return document.$x(expression);
}
async $eval(selector: string, pageFunction: Function | string, ...args: Array<any>): Promise<(object | undefined)> {
$eval: types.$Eval<JSHandle> = async (selector, pageFunction, ...args) => {
const document = await this._document();
return document.$eval(selector, pageFunction, ...args);
}
async $$eval(selector: string, pageFunction: Function | string, ...args: Array<any>): Promise<(object | undefined)> {
$$eval: types.$$Eval<JSHandle> = async (selector, pageFunction, ...args) => {
const document = await this._document();
return document.$$eval(selector, pageFunction, ...args);
}

View file

@ -21,8 +21,9 @@ import { Frame } from './FrameManager';
import * as injectedSource from '../generated/injectedSource';
import * as cssSelectorEngineSource from '../generated/cssSelectorEngineSource';
import * as xpathSelectorEngineSource from '../generated/xpathSelectorEngineSource';
import * as types from '../types';
export class ExecutionContext {
export class ExecutionContext implements types.EvaluationContext<JSHandle> {
_session: any;
_frame: Frame;
_executionContextId: string;
@ -34,7 +35,7 @@ export class ExecutionContext {
this._executionContextId = executionContextId;
}
async evaluateHandle(pageFunction, ...args): Promise<JSHandle> {
evaluateHandle: types.EvaluateHandle<JSHandle> = async (pageFunction, ...args) => {
if (helper.isString(pageFunction)) {
const payload = await this._session.send('Runtime.evaluate', {
expression: pageFunction.trim(),
@ -62,7 +63,7 @@ export class ExecutionContext {
throw new Error('Passed function is not well-serializable!');
}
}
args = args.map(arg => {
const protocolArgs = args.map(arg => {
if (arg instanceof JSHandle) {
if (arg._context !== this)
throw new Error('JSHandles can be evaluated only in the context they were created!');
@ -84,7 +85,7 @@ export class ExecutionContext {
try {
callFunctionPromise = this._session.send('Runtime.callFunction', {
functionDeclaration: functionText,
args,
args: protocolArgs,
executionContextId: this._executionContextId
});
} catch (err) {
@ -106,9 +107,9 @@ export class ExecutionContext {
return this._frame;
}
async evaluate(pageFunction, ...args): Promise<any> {
evaluate: types.Evaluate<JSHandle> = async (pageFunction, ...args) => {
try {
const handle = await this.evaluateHandle(pageFunction, ...args);
const handle = await this.evaluateHandle(pageFunction, ...args as any);
const result = await handle.jsonValue();
await handle.dispose();
return result;

View file

@ -1,3 +1,20 @@
/**
* Copyright 2019 Google Inc. All rights reserved.
* Modifications copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { JugglerSession } from './Connection';
import { Page } from './Page';
@ -11,6 +28,7 @@ import { JSHandle, ElementHandle } from './JSHandle';
import { TimeoutSettings } from '../TimeoutSettings';
import { NetworkManager } from './NetworkManager';
import { MultiClickOptions, ClickOptions, SelectOption } from '../input';
import * as types from '../types';
export const FrameManagerEvents = {
FrameNavigated: Symbol('FrameManagerEvents.FrameNavigated'),
@ -359,8 +377,8 @@ export class Frame {
return this._mainWorld.setContent(html);
}
async evaluate(pageFunction, ...args): 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> {
@ -371,20 +389,20 @@ export class Frame {
return this._mainWorld.$$(selector);
}
async $eval(selector: string, pageFunction: Function | string, ...args: Array<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: Array<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 $x(expression: string): Promise<Array<ElementHandle>> {
return this._mainWorld.$x(expression);
}
async evaluateHandle(pageFunction, ...args): Promise<JSHandle> {
return this._mainWorld.evaluateHandle(pageFunction, ...args);
evaluateHandle: types.EvaluateHandle<JSHandle> = (pageFunction, ...args) => {
return this._mainWorld.evaluateHandle(pageFunction, ...args as any);
}
async addScriptTag(options: { content?: string; path?: string; type?: string; url?: string; }): Promise<ElementHandle> {

View file

@ -17,6 +17,7 @@
import * as fs from 'fs';
import * as path from 'path';
import * as types from '../types';
import { assert, debugError, helper } from '../helper';
import { ClickOptions, fillFunction, MultiClickOptions, selectFunction, SelectOption } from '../input';
import { JugglerSession } from './Connection';
@ -56,12 +57,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);
}
toString(): string {
@ -190,7 +191,7 @@ export class ElementHandle extends JSHandle {
}
isIntersectingViewport(): Promise<boolean> {
return this._frame.evaluate(async element => {
return this._frame.evaluate(async (element: Element) => {
const visibleRatio = await new Promise(resolve => {
const observer = new IntersectionObserver(entries => {
resolve(entries[0].intersectionRatio);
@ -233,7 +234,7 @@ export class ElementHandle extends JSHandle {
return result;
}
async $eval(selector: string, pageFunction: Function | string, ...args: Array<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}"`);
@ -242,7 +243,7 @@ export class ElementHandle extends JSHandle {
return result;
}
async $$eval(selector: string, pageFunction: Function | string, ...args: Array<any>): Promise<object | undefined> {
$$eval: types.$$Eval<JSHandle> = async (selector, pageFunction, ...args) => {
const arrayHandle = await this._frame.evaluateHandle(
(root: SelectorRoot, selector: string, injected: Injected) => injected.querySelectorAll('css=' + selector, root),
this, selector, await this._context._injected()
@ -270,7 +271,7 @@ export class ElementHandle extends JSHandle {
}
async _scrollIntoViewIfNeeded() {
const error = await this._frame.evaluate(async element => {
const error = await this._frame.evaluate(async (element: Element) => {
if (!element.isConnected)
return 'Node is detached from document';
if (element.nodeType !== Node.ELEMENT_NODE)
@ -286,7 +287,7 @@ export class ElementHandle extends JSHandle {
requestAnimationFrame(() => {});
});
if (visibleRatio !== 1.0)
element.scrollIntoView({block: 'center', inline: 'center', behavior: 'instant'});
element.scrollIntoView({block: 'center', inline: 'center', behavior: ('instant' as ScrollBehavior)});
return false;
}, this);
if (error)

View file

@ -16,6 +16,7 @@ import { createHandle, ElementHandle, JSHandle } from './JSHandle';
import { NavigationWatchdog } from './NavigationWatchdog';
import { NetworkManager, NetworkManagerEvents, Request, Response } from './NetworkManager';
import * as input from '../input';
import * as types from '../types';
const writeFileAsync = helper.promisify(fs.writeFile);
@ -471,8 +472,8 @@ export class Page extends EventEmitter {
}
}
evaluate(pageFunction, ...args) {
return this.mainFrame().evaluate(pageFunction, ...args);
evaluate: types.Evaluate<JSHandle> = (pageFunction, ...args) => {
return this.mainFrame().evaluate(pageFunction, ...args as any);
}
addScriptTag(options: { content?: string; path?: string; type?: string; url?: string; }): Promise<ElementHandle> {
@ -543,20 +544,20 @@ export class Page extends EventEmitter {
return this._frameManager.mainFrame().$$(selector);
}
$eval(selector: string, pageFunction: Function | string, ...args: Array<any>): Promise<(object | undefined)> {
return this._frameManager.mainFrame().$eval(selector, pageFunction, ...args);
$eval: types.$Eval<JSHandle> = (selector, pageFunction, ...args) => {
return this._frameManager.mainFrame().$eval(selector, pageFunction, ...args as any);
}
$$eval(selector: string, pageFunction: Function | string, ...args: Array<any>): Promise<(object | undefined)> {
return this._frameManager.mainFrame().$$eval(selector, pageFunction, ...args);
$$eval: types.$$Eval<JSHandle> = (selector, pageFunction, ...args) => {
return this._frameManager.mainFrame().$$eval(selector, pageFunction, ...args as any);
}
$x(expression: string): Promise<Array<ElementHandle>> {
return this._frameManager.mainFrame().$x(expression);
}
evaluateHandle(pageFunction, ...args) {
return this._frameManager.mainFrame().evaluateHandle(pageFunction, ...args);
evaluateHandle: types.EvaluateHandle<JSHandle> = async (pageFunction, ...args) => {
return this._frameManager.mainFrame().evaluateHandle(pageFunction, ...args as any);
}
async close(options: any = {}) {