chore: use eval types in firefox (#77)
This commit is contained in:
parent
d756f616f5
commit
371394ff2f
|
|
@ -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 {helper, assert} from '../helper';
|
||||||
import {TimeoutError} from '../Errors';
|
import {TimeoutError} from '../Errors';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as util from 'util';
|
import * as util from 'util';
|
||||||
|
import * as types from '../types';
|
||||||
import {ElementHandle, JSHandle} from './JSHandle';
|
import {ElementHandle, JSHandle} from './JSHandle';
|
||||||
import { ExecutionContext } from './ExecutionContext';
|
import { ExecutionContext } from './ExecutionContext';
|
||||||
|
|
||||||
|
|
@ -61,14 +79,14 @@ export class DOMWorld {
|
||||||
throw new Error('Method not implemented.');
|
throw new Error('Method not implemented.');
|
||||||
}
|
}
|
||||||
|
|
||||||
async evaluateHandle(pageFunction, ...args): 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, ...args): 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> {
|
||||||
|
|
@ -87,12 +105,12 @@ export class DOMWorld {
|
||||||
return document.$x(expression);
|
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();
|
const document = await this._document();
|
||||||
return document.$eval(selector, pageFunction, ...args);
|
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();
|
const document = await this._document();
|
||||||
return document.$$eval(selector, pageFunction, ...args);
|
return document.$$eval(selector, pageFunction, ...args);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,9 @@ import { Frame } from './FrameManager';
|
||||||
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 class ExecutionContext {
|
export class ExecutionContext implements types.EvaluationContext<JSHandle> {
|
||||||
_session: any;
|
_session: any;
|
||||||
_frame: Frame;
|
_frame: Frame;
|
||||||
_executionContextId: string;
|
_executionContextId: string;
|
||||||
|
|
@ -34,7 +35,7 @@ export class ExecutionContext {
|
||||||
this._executionContextId = executionContextId;
|
this._executionContextId = executionContextId;
|
||||||
}
|
}
|
||||||
|
|
||||||
async evaluateHandle(pageFunction, ...args): Promise<JSHandle> {
|
evaluateHandle: types.EvaluateHandle<JSHandle> = async (pageFunction, ...args) => {
|
||||||
if (helper.isString(pageFunction)) {
|
if (helper.isString(pageFunction)) {
|
||||||
const payload = await this._session.send('Runtime.evaluate', {
|
const payload = await this._session.send('Runtime.evaluate', {
|
||||||
expression: pageFunction.trim(),
|
expression: pageFunction.trim(),
|
||||||
|
|
@ -62,7 +63,7 @@ export class ExecutionContext {
|
||||||
throw new Error('Passed function is not well-serializable!');
|
throw new Error('Passed function is not well-serializable!');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
args = args.map(arg => {
|
const protocolArgs = args.map(arg => {
|
||||||
if (arg instanceof JSHandle) {
|
if (arg instanceof JSHandle) {
|
||||||
if (arg._context !== this)
|
if (arg._context !== this)
|
||||||
throw new Error('JSHandles can be evaluated only in the context they were created!');
|
throw new Error('JSHandles can be evaluated only in the context they were created!');
|
||||||
|
|
@ -84,7 +85,7 @@ export class ExecutionContext {
|
||||||
try {
|
try {
|
||||||
callFunctionPromise = this._session.send('Runtime.callFunction', {
|
callFunctionPromise = this._session.send('Runtime.callFunction', {
|
||||||
functionDeclaration: functionText,
|
functionDeclaration: functionText,
|
||||||
args,
|
args: protocolArgs,
|
||||||
executionContextId: this._executionContextId
|
executionContextId: this._executionContextId
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
@ -106,9 +107,9 @@ export class ExecutionContext {
|
||||||
return this._frame;
|
return this._frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
async evaluate(pageFunction, ...args): Promise<any> {
|
evaluate: types.Evaluate<JSHandle> = async (pageFunction, ...args) => {
|
||||||
try {
|
try {
|
||||||
const handle = await this.evaluateHandle(pageFunction, ...args);
|
const handle = await this.evaluateHandle(pageFunction, ...args as any);
|
||||||
const result = await handle.jsonValue();
|
const result = await handle.jsonValue();
|
||||||
await handle.dispose();
|
await handle.dispose();
|
||||||
return result;
|
return result;
|
||||||
|
|
|
||||||
|
|
@ -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 { JugglerSession } from './Connection';
|
||||||
import { Page } from './Page';
|
import { Page } from './Page';
|
||||||
|
|
||||||
|
|
@ -11,6 +28,7 @@ import { JSHandle, ElementHandle } from './JSHandle';
|
||||||
import { TimeoutSettings } from '../TimeoutSettings';
|
import { TimeoutSettings } from '../TimeoutSettings';
|
||||||
import { NetworkManager } from './NetworkManager';
|
import { NetworkManager } from './NetworkManager';
|
||||||
import { MultiClickOptions, ClickOptions, SelectOption } from '../input';
|
import { MultiClickOptions, ClickOptions, SelectOption } from '../input';
|
||||||
|
import * as types from '../types';
|
||||||
|
|
||||||
export const FrameManagerEvents = {
|
export const FrameManagerEvents = {
|
||||||
FrameNavigated: Symbol('FrameManagerEvents.FrameNavigated'),
|
FrameNavigated: Symbol('FrameManagerEvents.FrameNavigated'),
|
||||||
|
|
@ -359,8 +377,8 @@ export class Frame {
|
||||||
return this._mainWorld.setContent(html);
|
return this._mainWorld.setContent(html);
|
||||||
}
|
}
|
||||||
|
|
||||||
async evaluate(pageFunction, ...args): 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> {
|
||||||
|
|
@ -371,20 +389,20 @@ export class Frame {
|
||||||
return this._mainWorld.$$(selector);
|
return this._mainWorld.$$(selector);
|
||||||
}
|
}
|
||||||
|
|
||||||
async $eval(selector: string, pageFunction: Function | string, ...args: Array<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: Array<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 $x(expression: string): Promise<Array<ElementHandle>> {
|
async $x(expression: string): Promise<Array<ElementHandle>> {
|
||||||
return this._mainWorld.$x(expression);
|
return this._mainWorld.$x(expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
async evaluateHandle(pageFunction, ...args): Promise<JSHandle> {
|
evaluateHandle: types.EvaluateHandle<JSHandle> = (pageFunction, ...args) => {
|
||||||
return this._mainWorld.evaluateHandle(pageFunction, ...args);
|
return this._mainWorld.evaluateHandle(pageFunction, ...args as any);
|
||||||
}
|
}
|
||||||
|
|
||||||
async addScriptTag(options: { content?: string; path?: string; type?: string; url?: string; }): Promise<ElementHandle> {
|
async addScriptTag(options: { content?: string; path?: string; type?: string; url?: string; }): Promise<ElementHandle> {
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
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, fillFunction, MultiClickOptions, selectFunction, SelectOption } from '../input';
|
import { ClickOptions, fillFunction, MultiClickOptions, selectFunction, SelectOption } from '../input';
|
||||||
import { JugglerSession } from './Connection';
|
import { JugglerSession } from './Connection';
|
||||||
|
|
@ -56,12 +57,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);
|
||||||
}
|
}
|
||||||
|
|
||||||
toString(): string {
|
toString(): string {
|
||||||
|
|
@ -190,7 +191,7 @@ export class ElementHandle extends JSHandle {
|
||||||
}
|
}
|
||||||
|
|
||||||
isIntersectingViewport(): Promise<boolean> {
|
isIntersectingViewport(): Promise<boolean> {
|
||||||
return this._frame.evaluate(async element => {
|
return this._frame.evaluate(async (element: Element) => {
|
||||||
const visibleRatio = await new Promise(resolve => {
|
const visibleRatio = await new Promise(resolve => {
|
||||||
const observer = new IntersectionObserver(entries => {
|
const observer = new IntersectionObserver(entries => {
|
||||||
resolve(entries[0].intersectionRatio);
|
resolve(entries[0].intersectionRatio);
|
||||||
|
|
@ -233,7 +234,7 @@ export class ElementHandle extends JSHandle {
|
||||||
return result;
|
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);
|
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}"`);
|
||||||
|
|
@ -242,7 +243,7 @@ export class ElementHandle extends JSHandle {
|
||||||
return result;
|
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(
|
const arrayHandle = await this._frame.evaluateHandle(
|
||||||
(root: SelectorRoot, selector: string, injected: Injected) => injected.querySelectorAll('css=' + selector, root),
|
(root: SelectorRoot, selector: string, injected: Injected) => injected.querySelectorAll('css=' + selector, root),
|
||||||
this, selector, await this._context._injected()
|
this, selector, await this._context._injected()
|
||||||
|
|
@ -270,7 +271,7 @@ export class ElementHandle extends JSHandle {
|
||||||
}
|
}
|
||||||
|
|
||||||
async _scrollIntoViewIfNeeded() {
|
async _scrollIntoViewIfNeeded() {
|
||||||
const error = await this._frame.evaluate(async element => {
|
const error = await this._frame.evaluate(async (element: Element) => {
|
||||||
if (!element.isConnected)
|
if (!element.isConnected)
|
||||||
return 'Node is detached from document';
|
return 'Node is detached from document';
|
||||||
if (element.nodeType !== Node.ELEMENT_NODE)
|
if (element.nodeType !== Node.ELEMENT_NODE)
|
||||||
|
|
@ -286,7 +287,7 @@ export class ElementHandle extends JSHandle {
|
||||||
requestAnimationFrame(() => {});
|
requestAnimationFrame(() => {});
|
||||||
});
|
});
|
||||||
if (visibleRatio !== 1.0)
|
if (visibleRatio !== 1.0)
|
||||||
element.scrollIntoView({block: 'center', inline: 'center', behavior: 'instant'});
|
element.scrollIntoView({block: 'center', inline: 'center', behavior: ('instant' as ScrollBehavior)});
|
||||||
return false;
|
return false;
|
||||||
}, this);
|
}, this);
|
||||||
if (error)
|
if (error)
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ import { createHandle, ElementHandle, JSHandle } from './JSHandle';
|
||||||
import { NavigationWatchdog } from './NavigationWatchdog';
|
import { NavigationWatchdog } from './NavigationWatchdog';
|
||||||
import { NetworkManager, NetworkManagerEvents, Request, Response } from './NetworkManager';
|
import { NetworkManager, NetworkManagerEvents, Request, Response } from './NetworkManager';
|
||||||
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);
|
||||||
|
|
||||||
|
|
@ -471,8 +472,8 @@ export class Page extends EventEmitter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
evaluate(pageFunction, ...args) {
|
evaluate: types.Evaluate<JSHandle> = (pageFunction, ...args) => {
|
||||||
return this.mainFrame().evaluate(pageFunction, ...args);
|
return this.mainFrame().evaluate(pageFunction, ...args as any);
|
||||||
}
|
}
|
||||||
|
|
||||||
addScriptTag(options: { content?: string; path?: string; type?: string; url?: string; }): Promise<ElementHandle> {
|
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);
|
return this._frameManager.mainFrame().$$(selector);
|
||||||
}
|
}
|
||||||
|
|
||||||
$eval(selector: string, pageFunction: Function | string, ...args: Array<any>): Promise<(object | undefined)> {
|
$eval: types.$Eval<JSHandle> = (selector, pageFunction, ...args) => {
|
||||||
return this._frameManager.mainFrame().$eval(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)> {
|
$$eval: types.$$Eval<JSHandle> = (selector, pageFunction, ...args) => {
|
||||||
return this._frameManager.mainFrame().$$eval(selector, pageFunction, ...args);
|
return this._frameManager.mainFrame().$$eval(selector, pageFunction, ...args as any);
|
||||||
}
|
}
|
||||||
|
|
||||||
$x(expression: string): Promise<Array<ElementHandle>> {
|
$x(expression: string): Promise<Array<ElementHandle>> {
|
||||||
return this._frameManager.mainFrame().$x(expression);
|
return this._frameManager.mainFrame().$x(expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
evaluateHandle(pageFunction, ...args) {
|
evaluateHandle: types.EvaluateHandle<JSHandle> = async (pageFunction, ...args) => {
|
||||||
return this._frameManager.mainFrame().evaluateHandle(pageFunction, ...args);
|
return this._frameManager.mainFrame().evaluateHandle(pageFunction, ...args as any);
|
||||||
}
|
}
|
||||||
|
|
||||||
async close(options: any = {}) {
|
async close(options: any = {}) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue