2020-01-06 18:22:35 -08:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-11-27 12:39:53 -08:00
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
import * as dom from './dom';
|
2020-08-24 06:51:51 -07:00
|
|
|
import * as utilityScriptSource from '../generated/utilityScriptSource';
|
2020-06-11 18:18:33 -07:00
|
|
|
import { serializeAsCallArgument } from './common/utilityScriptSerializers';
|
2020-08-24 06:51:51 -07:00
|
|
|
import type UtilityScript from './injected/utilityScript';
|
2021-02-09 14:44:48 -08:00
|
|
|
import { SdkObject } from './instrumentation';
|
2020-05-27 22:19:05 -07:00
|
|
|
|
2021-04-07 07:01:38 +08:00
|
|
|
export type ObjectId = string;
|
2020-05-27 22:19:05 -07:00
|
|
|
export type RemoteObject = {
|
2020-06-03 17:50:16 -07:00
|
|
|
objectId?: ObjectId,
|
2020-05-27 22:19:05 -07:00
|
|
|
value?: any
|
|
|
|
};
|
2019-11-27 12:39:53 -08:00
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
type NoHandles<Arg> = Arg extends JSHandle ? never : (Arg extends object ? { [Key in keyof Arg]: NoHandles<Arg[Key]> } : Arg);
|
|
|
|
type Unboxed<Arg> =
|
|
|
|
Arg extends dom.ElementHandle<infer T> ? T :
|
|
|
|
Arg extends JSHandle<infer T> ? T :
|
|
|
|
Arg extends NoHandles<Arg> ? Arg :
|
|
|
|
Arg extends [infer A0] ? [Unboxed<A0>] :
|
|
|
|
Arg extends [infer A0, infer A1] ? [Unboxed<A0>, Unboxed<A1>] :
|
|
|
|
Arg extends [infer A0, infer A1, infer A2] ? [Unboxed<A0>, Unboxed<A1>, Unboxed<A2>] :
|
|
|
|
Arg extends Array<infer T> ? Array<Unboxed<T>> :
|
|
|
|
Arg extends object ? { [Key in keyof Arg]: Unboxed<Arg[Key]> } :
|
|
|
|
Arg;
|
|
|
|
export type Func0<R> = string | (() => R | Promise<R>);
|
|
|
|
export type Func1<Arg, R> = string | ((arg: Unboxed<Arg>) => R | Promise<R>);
|
|
|
|
export type FuncOn<On, Arg2, R> = string | ((on: On, arg2: Unboxed<Arg2>) => R | Promise<R>);
|
|
|
|
export type SmartHandle<T> = T extends Node ? dom.ElementHandle<T> : JSHandle<T>;
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
export interface ExecutionContextDelegate {
|
2021-04-29 09:28:19 -07:00
|
|
|
rawEvaluateJSON(expression: string): Promise<any>;
|
|
|
|
rawEvaluateHandle(expression: string): Promise<ObjectId>;
|
2021-03-10 11:43:26 -08:00
|
|
|
rawCallFunctionNoReply(func: Function, ...args: any[]): void;
|
2020-06-03 17:50:16 -07:00
|
|
|
evaluateWithArguments(expression: string, returnByValue: boolean, utilityScript: JSHandle<any>, values: any[], objectIds: ObjectId[]): Promise<any>;
|
2021-04-07 07:01:38 +08:00
|
|
|
getProperties(context: ExecutionContext, objectId: ObjectId): Promise<Map<string, JSHandle>>;
|
2020-05-27 22:19:05 -07:00
|
|
|
createHandle(context: ExecutionContext, remoteObject: RemoteObject): JSHandle;
|
2021-04-07 07:01:38 +08:00
|
|
|
releaseHandle(objectId: ObjectId): Promise<void>;
|
2019-11-27 12:39:53 -08:00
|
|
|
}
|
|
|
|
|
2021-02-09 09:00:00 -08:00
|
|
|
export class ExecutionContext extends SdkObject {
|
2019-11-28 12:50:52 -08:00
|
|
|
readonly _delegate: ExecutionContextDelegate;
|
2020-05-26 14:08:32 -07:00
|
|
|
private _utilityScriptPromise: Promise<JSHandle> | undefined;
|
2019-11-27 12:39:53 -08:00
|
|
|
|
2021-02-09 09:00:00 -08:00
|
|
|
constructor(parent: SdkObject, delegate: ExecutionContextDelegate) {
|
2021-04-20 23:03:56 -07:00
|
|
|
super(parent, 'execution-context');
|
2019-11-27 12:39:53 -08:00
|
|
|
this._delegate = delegate;
|
|
|
|
}
|
|
|
|
|
2021-03-18 03:03:21 +08:00
|
|
|
async waitForSignalsCreatedBy<T>(action: () => Promise<T>): Promise<T> {
|
|
|
|
return action();
|
|
|
|
}
|
|
|
|
|
2020-05-15 15:21:49 -07:00
|
|
|
adoptIfNeeded(handle: JSHandle): Promise<JSHandle> | null {
|
2020-03-20 15:08:17 -07:00
|
|
|
return null;
|
2019-11-27 12:39:53 -08:00
|
|
|
}
|
|
|
|
|
2020-06-12 11:10:18 -07:00
|
|
|
utilityScript(): Promise<JSHandle<UtilityScript>> {
|
2020-05-20 15:55:33 -07:00
|
|
|
if (!this._utilityScriptPromise) {
|
2021-01-08 16:15:05 -08:00
|
|
|
const source = `
|
|
|
|
(() => {
|
|
|
|
${utilityScriptSource.source}
|
|
|
|
return new pwExport();
|
|
|
|
})();`;
|
2021-04-29 09:28:19 -07:00
|
|
|
this._utilityScriptPromise = this._delegate.rawEvaluateHandle(source).then(objectId => new JSHandle(this, 'object', objectId));
|
2020-05-20 15:55:33 -07:00
|
|
|
}
|
|
|
|
return this._utilityScriptPromise;
|
|
|
|
}
|
|
|
|
|
2020-05-27 17:19:05 -07:00
|
|
|
createHandle(remoteObject: RemoteObject): JSHandle {
|
2020-05-27 22:19:05 -07:00
|
|
|
return this._delegate.createHandle(this, remoteObject);
|
2019-12-02 13:12:28 -08:00
|
|
|
}
|
2020-08-18 19:13:40 -07:00
|
|
|
|
2021-04-29 09:28:19 -07:00
|
|
|
async rawEvaluateJSON(expression: string): Promise<any> {
|
|
|
|
return await this._delegate.rawEvaluateJSON(expression);
|
2021-03-01 12:20:04 -08:00
|
|
|
}
|
|
|
|
|
2020-08-18 19:13:40 -07:00
|
|
|
async doSlowMo() {
|
|
|
|
// overrided in FrameExecutionContext
|
|
|
|
}
|
2019-11-27 12:39:53 -08:00
|
|
|
}
|
|
|
|
|
2021-02-09 09:00:00 -08:00
|
|
|
export class JSHandle<T = any> extends SdkObject {
|
2019-11-28 12:50:52 -08:00
|
|
|
readonly _context: ExecutionContext;
|
2019-11-27 12:41:26 -08:00
|
|
|
_disposed = false;
|
2020-06-03 17:50:16 -07:00
|
|
|
readonly _objectId: ObjectId | undefined;
|
2020-05-27 17:19:05 -07:00
|
|
|
readonly _value: any;
|
2020-06-03 11:23:24 -07:00
|
|
|
private _objectType: string;
|
2020-06-12 11:10:18 -07:00
|
|
|
protected _preview: string;
|
2020-06-30 10:55:11 -07:00
|
|
|
private _previewCallback: ((preview: string) => void) | undefined;
|
2019-11-27 12:41:26 -08:00
|
|
|
|
2020-06-03 17:50:16 -07:00
|
|
|
constructor(context: ExecutionContext, type: string, objectId?: ObjectId, value?: any) {
|
2021-04-20 23:03:56 -07:00
|
|
|
super(context, 'handle');
|
2019-11-27 12:41:26 -08:00
|
|
|
this._context = context;
|
2020-05-27 22:19:05 -07:00
|
|
|
this._objectId = objectId;
|
|
|
|
this._value = value;
|
2020-06-03 11:23:24 -07:00
|
|
|
this._objectType = type;
|
2020-06-12 11:10:18 -07:00
|
|
|
if (this._objectId)
|
|
|
|
this._value = 'JSHandle@' + this._objectType;
|
|
|
|
this._preview = 'JSHandle@' + String(this._objectId ? this._objectType : this._value);
|
2019-11-27 12:41:26 -08:00
|
|
|
}
|
|
|
|
|
2021-03-10 11:43:26 -08:00
|
|
|
callFunctionNoReply(func: Function, arg: any) {
|
|
|
|
this._context._delegate.rawCallFunctionNoReply(func, this, arg);
|
|
|
|
}
|
|
|
|
|
2021-03-18 01:47:07 +08:00
|
|
|
async evaluate<R, Arg>(pageFunction: FuncOn<T, Arg, R>, arg?: Arg): Promise<R> {
|
2020-06-03 17:50:16 -07:00
|
|
|
return evaluate(this._context, true /* returnByValue */, pageFunction, this, arg);
|
2019-11-27 12:41:26 -08:00
|
|
|
}
|
|
|
|
|
2021-03-18 01:47:07 +08:00
|
|
|
async evaluateHandle<R, Arg>(pageFunction: FuncOn<T, Arg, R>, arg?: Arg): Promise<SmartHandle<R>> {
|
2020-06-03 17:50:16 -07:00
|
|
|
return evaluate(this._context, false /* returnByValue */, pageFunction, this, arg);
|
2019-11-27 12:41:26 -08:00
|
|
|
}
|
|
|
|
|
2021-03-18 03:03:21 +08:00
|
|
|
async evaluateExpressionAndWaitForSignals(expression: string, isFunction: boolean | undefined, returnByValue: boolean, arg: any) {
|
|
|
|
const value = await evaluateExpressionAndWaitForSignals(this._context, returnByValue, expression, isFunction, this, arg);
|
2020-08-18 19:13:40 -07:00
|
|
|
await this._context.doSlowMo();
|
|
|
|
return value;
|
2020-06-25 08:30:56 -07:00
|
|
|
}
|
|
|
|
|
2020-03-16 13:23:04 -07:00
|
|
|
async getProperty(propertyName: string): Promise<JSHandle> {
|
2020-01-13 13:33:25 -08:00
|
|
|
const objectHandle = await this.evaluateHandle((object: any, propertyName) => {
|
|
|
|
const result: any = {__proto__: null};
|
2019-11-27 12:41:26 -08:00
|
|
|
result[propertyName] = object[propertyName];
|
|
|
|
return result;
|
|
|
|
}, propertyName);
|
|
|
|
const properties = await objectHandle.getProperties();
|
2020-03-16 13:23:04 -07:00
|
|
|
const result = properties.get(propertyName)!;
|
2020-03-04 17:57:35 -08:00
|
|
|
objectHandle.dispose();
|
2019-11-27 12:41:26 -08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-04-07 07:01:38 +08:00
|
|
|
async getProperties(): Promise<Map<string, JSHandle>> {
|
|
|
|
if (!this._objectId)
|
|
|
|
return new Map();
|
|
|
|
return this._context._delegate.getProperties(this._context, this._objectId);
|
|
|
|
}
|
|
|
|
|
|
|
|
rawValue() {
|
|
|
|
return this._value;
|
2019-11-27 12:41:26 -08:00
|
|
|
}
|
|
|
|
|
2020-06-03 17:50:16 -07:00
|
|
|
async jsonValue(): Promise<T> {
|
|
|
|
if (!this._objectId)
|
|
|
|
return this._value;
|
|
|
|
const utilityScript = await this._context.utilityScript();
|
2021-01-25 16:36:57 -08:00
|
|
|
const script = `(utilityScript, ...args) => utilityScript.jsonValue(...args)`;
|
2020-06-03 17:50:16 -07:00
|
|
|
return this._context._delegate.evaluateWithArguments(script, true, utilityScript, [true], [this._objectId]);
|
2019-11-27 12:41:26 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
asElement(): dom.ElementHandle | null {
|
2019-11-27 12:41:26 -08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-03-22 09:59:39 -07:00
|
|
|
dispose() {
|
2019-11-27 12:41:26 -08:00
|
|
|
if (this._disposed)
|
|
|
|
return;
|
|
|
|
this._disposed = true;
|
2021-04-07 07:01:38 +08:00
|
|
|
if (this._objectId)
|
|
|
|
this._context._delegate.releaseHandle(this._objectId).catch(e => {});
|
2019-11-27 12:41:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
toString(): string {
|
2020-06-12 11:10:18 -07:00
|
|
|
return this._preview;
|
2019-11-27 12:41:26 -08:00
|
|
|
}
|
2020-06-30 10:55:11 -07:00
|
|
|
|
|
|
|
_setPreviewCallback(callback: (preview: string) => void) {
|
|
|
|
this._previewCallback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
_setPreview(preview: string) {
|
|
|
|
this._preview = preview;
|
|
|
|
if (this._previewCallback)
|
|
|
|
this._previewCallback(preview);
|
|
|
|
}
|
2019-11-27 12:41:26 -08:00
|
|
|
}
|
2020-03-18 10:41:46 -07:00
|
|
|
|
2020-06-03 17:50:16 -07:00
|
|
|
export async function evaluate(context: ExecutionContext, returnByValue: boolean, pageFunction: Function | string, ...args: any[]): Promise<any> {
|
2020-06-25 08:30:56 -07:00
|
|
|
return evaluateExpression(context, returnByValue, String(pageFunction), typeof pageFunction === 'function', ...args);
|
|
|
|
}
|
|
|
|
|
2021-02-03 13:49:25 -08:00
|
|
|
export async function evaluateExpression(context: ExecutionContext, returnByValue: boolean, expression: string, isFunction: boolean | undefined, ...args: any[]): Promise<any> {
|
2020-06-03 17:50:16 -07:00
|
|
|
const utilityScript = await context.utilityScript();
|
2021-02-04 08:45:59 -08:00
|
|
|
expression = normalizeEvaluationExpression(expression, isFunction);
|
2020-05-27 17:19:05 -07:00
|
|
|
const handles: (Promise<JSHandle>)[] = [];
|
2020-03-19 13:07:33 -07:00
|
|
|
const toDispose: Promise<JSHandle>[] = [];
|
2020-05-27 17:19:05 -07:00
|
|
|
const pushHandle = (handle: Promise<JSHandle>): number => {
|
2020-03-18 10:41:46 -07:00
|
|
|
handles.push(handle);
|
2020-05-27 17:19:05 -07:00
|
|
|
return handles.length - 1;
|
2020-03-18 10:41:46 -07:00
|
|
|
};
|
|
|
|
|
2020-07-17 09:53:13 -07:00
|
|
|
args = args.map(arg => serializeAsCallArgument(arg, handle => {
|
2020-05-27 17:19:05 -07:00
|
|
|
if (handle instanceof JSHandle) {
|
|
|
|
if (!handle._objectId)
|
|
|
|
return { fallThrough: handle._value };
|
|
|
|
if (handle._disposed)
|
2020-03-18 10:41:46 -07:00
|
|
|
throw new Error('JSHandle is disposed!');
|
2020-05-27 17:19:05 -07:00
|
|
|
const adopted = context.adoptIfNeeded(handle);
|
2020-03-19 13:07:33 -07:00
|
|
|
if (adopted === null)
|
2020-05-27 17:19:05 -07:00
|
|
|
return { h: pushHandle(Promise.resolve(handle)) };
|
2020-03-19 13:07:33 -07:00
|
|
|
toDispose.push(adopted);
|
2020-05-27 17:19:05 -07:00
|
|
|
return { h: pushHandle(adopted) };
|
2020-03-19 13:07:33 -07:00
|
|
|
}
|
2020-05-27 17:19:05 -07:00
|
|
|
return { fallThrough: handle };
|
|
|
|
}));
|
2020-06-03 17:50:16 -07:00
|
|
|
|
|
|
|
const utilityScriptObjectIds: ObjectId[] = [];
|
2020-05-27 17:19:05 -07:00
|
|
|
for (const handle of await Promise.all(handles)) {
|
|
|
|
if (handle._context !== context)
|
|
|
|
throw new Error('JSHandles can be evaluated only in the context they were created!');
|
2020-06-03 17:50:16 -07:00
|
|
|
utilityScriptObjectIds.push(handle._objectId!);
|
2020-03-19 13:07:33 -07:00
|
|
|
}
|
2020-05-19 08:40:45 -07:00
|
|
|
|
2020-06-03 17:50:16 -07:00
|
|
|
// See UtilityScript for arguments.
|
2021-02-03 13:49:25 -08:00
|
|
|
const utilityScriptValues = [isFunction, returnByValue, expression, args.length, ...args];
|
2020-06-03 17:50:16 -07:00
|
|
|
|
2021-02-03 13:49:25 -08:00
|
|
|
const script = `(utilityScript, ...args) => utilityScript.evaluate(...args)`;
|
2020-06-03 17:50:16 -07:00
|
|
|
try {
|
2020-06-25 16:05:36 -07:00
|
|
|
return await context._delegate.evaluateWithArguments(script, returnByValue, utilityScript, utilityScriptValues, utilityScriptObjectIds);
|
2020-06-03 17:50:16 -07:00
|
|
|
} finally {
|
|
|
|
toDispose.map(handlePromise => handlePromise.then(handle => handle.dispose()));
|
|
|
|
}
|
2020-05-27 17:19:05 -07:00
|
|
|
}
|
|
|
|
|
2021-03-18 03:03:21 +08:00
|
|
|
export async function evaluateExpressionAndWaitForSignals(context: ExecutionContext, returnByValue: boolean, expression: string, isFunction?: boolean, ...args: any[]): Promise<any> {
|
|
|
|
return await context.waitForSignalsCreatedBy(() => evaluateExpression(context, returnByValue, expression, isFunction, ...args));
|
|
|
|
}
|
|
|
|
|
2020-05-27 22:19:05 -07:00
|
|
|
export function parseUnserializableValue(unserializableValue: string): any {
|
2020-05-27 17:19:05 -07:00
|
|
|
if (unserializableValue === 'NaN')
|
|
|
|
return NaN;
|
|
|
|
if (unserializableValue === 'Infinity')
|
|
|
|
return Infinity;
|
|
|
|
if (unserializableValue === '-Infinity')
|
|
|
|
return -Infinity;
|
|
|
|
if (unserializableValue === '-0')
|
|
|
|
return -0;
|
2020-03-18 10:41:46 -07:00
|
|
|
}
|
2021-02-04 08:45:59 -08:00
|
|
|
|
|
|
|
export function normalizeEvaluationExpression(expression: string, isFunction: boolean | undefined): string {
|
|
|
|
expression = expression.trim();
|
|
|
|
|
|
|
|
if (isFunction) {
|
|
|
|
try {
|
|
|
|
new Function('(' + expression + ')');
|
|
|
|
} catch (e1) {
|
|
|
|
// This means we might have a function shorthand. Try another
|
|
|
|
// time prefixing 'function '.
|
|
|
|
if (expression.startsWith('async '))
|
|
|
|
expression = 'async function ' + expression.substring('async '.length);
|
|
|
|
else
|
|
|
|
expression = 'function ' + expression;
|
|
|
|
try {
|
|
|
|
new Function('(' + expression + ')');
|
|
|
|
} catch (e2) {
|
|
|
|
// We tried hard to serialize, but there's a weird beast here.
|
|
|
|
throw new Error('Passed function is not well-serializable!');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (/^(async)?\s*function(\s|\()/.test(expression))
|
|
|
|
expression = '(' + expression + ')';
|
|
|
|
return expression;
|
|
|
|
}
|