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
|
|
|
|
|
|
|
import * as types from './types';
|
2019-11-27 16:02:31 -08:00
|
|
|
import * as dom from './dom';
|
2019-11-27 12:39:53 -08:00
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
export interface ExecutionContextDelegate {
|
|
|
|
evaluate(context: ExecutionContext, returnByValue: boolean, pageFunction: string | Function, ...args: any[]): Promise<any>;
|
|
|
|
getProperties(handle: JSHandle): Promise<Map<string, JSHandle>>;
|
|
|
|
releaseHandle(handle: JSHandle): Promise<void>;
|
2019-12-02 13:01:01 -08:00
|
|
|
handleToString(handle: JSHandle, includeType: boolean): string;
|
2019-12-05 16:26:09 -08:00
|
|
|
handleJSONValue<T>(handle: JSHandle<T>): Promise<T>;
|
2019-11-27 12:39:53 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
export class ExecutionContext {
|
2019-11-28 12:50:52 -08:00
|
|
|
readonly _delegate: ExecutionContextDelegate;
|
2019-11-27 12:39:53 -08:00
|
|
|
|
2019-11-28 12:50:52 -08:00
|
|
|
constructor(delegate: ExecutionContextDelegate) {
|
2019-11-27 12:39:53 -08:00
|
|
|
this._delegate = delegate;
|
|
|
|
}
|
|
|
|
|
2020-03-11 22:52:03 -07:00
|
|
|
_evaluate(returnByValue: boolean, waitForNavigations: boolean, pageFunction: string | Function, ...args: any[]): Promise<any> {
|
2019-12-19 11:44:07 -08:00
|
|
|
return this._delegate.evaluate(this, returnByValue, pageFunction, ...args);
|
2019-11-27 12:39:53 -08:00
|
|
|
}
|
|
|
|
|
2019-12-19 11:44:07 -08:00
|
|
|
evaluate: types.Evaluate = async (pageFunction, ...args) => {
|
2020-03-11 22:52:03 -07:00
|
|
|
return this._evaluate(true /* returnByValue */, true /* waitForNavigations */, pageFunction, ...args);
|
2019-12-19 11:44:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
evaluateHandle: types.EvaluateHandle = async (pageFunction, ...args) => {
|
2020-03-11 22:52:03 -07:00
|
|
|
return this._evaluate(false /* returnByValue */, true /* waitForNavigations */, pageFunction, ...args);
|
2019-11-27 12:39:53 -08:00
|
|
|
}
|
2019-12-02 13:12:28 -08:00
|
|
|
|
|
|
|
_createHandle(remoteObject: any): JSHandle {
|
2019-12-12 21:11:52 -08:00
|
|
|
return new JSHandle(this, remoteObject);
|
2019-12-02 13:12:28 -08:00
|
|
|
}
|
2019-11-27 12:39:53 -08:00
|
|
|
}
|
|
|
|
|
2019-12-05 16:26:09 -08:00
|
|
|
export class JSHandle<T = any> {
|
2019-11-28 12:50:52 -08:00
|
|
|
readonly _context: ExecutionContext;
|
2019-12-02 13:12:28 -08:00
|
|
|
readonly _remoteObject: any;
|
2019-11-27 12:41:26 -08:00
|
|
|
_disposed = false;
|
|
|
|
|
2019-12-02 13:12:28 -08:00
|
|
|
constructor(context: ExecutionContext, remoteObject: any) {
|
2019-11-27 12:41:26 -08:00
|
|
|
this._context = context;
|
2019-12-02 13:12:28 -08:00
|
|
|
this._remoteObject = remoteObject;
|
2019-11-27 12:41:26 -08:00
|
|
|
}
|
|
|
|
|
2019-12-05 16:26:09 -08:00
|
|
|
evaluate: types.EvaluateOn<T> = (pageFunction, ...args) => {
|
2020-01-13 13:33:25 -08:00
|
|
|
return this._context.evaluate(pageFunction as any, this, ...args);
|
2019-11-27 12:41:26 -08:00
|
|
|
}
|
|
|
|
|
2019-12-05 16:26:09 -08:00
|
|
|
evaluateHandle: types.EvaluateHandleOn<T> = (pageFunction, ...args) => {
|
2020-01-13 13:33:25 -08:00
|
|
|
return this._context.evaluateHandle(pageFunction as any, this, ...args);
|
2019-11-27 12:41:26 -08: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;
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
getProperties(): Promise<Map<string, JSHandle>> {
|
2019-11-27 12:41:26 -08:00
|
|
|
return this._context._delegate.getProperties(this);
|
|
|
|
}
|
|
|
|
|
2019-12-05 16:26:09 -08:00
|
|
|
jsonValue(): Promise<T> {
|
2019-11-27 12:41:26 -08:00
|
|
|
return this._context._delegate.handleJSONValue(this);
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
asElement(): dom.ElementHandle | null {
|
2019-11-27 12:41:26 -08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async dispose() {
|
|
|
|
if (this._disposed)
|
|
|
|
return;
|
|
|
|
this._disposed = true;
|
|
|
|
await this._context._delegate.releaseHandle(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
toString(): string {
|
2019-12-02 13:01:01 -08:00
|
|
|
return this._context._delegate.handleToString(this, true /* includeType */);
|
2019-11-27 12:41:26 -08:00
|
|
|
}
|
|
|
|
}
|