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 16:02:31 -08:00
|
|
|
|
2020-04-01 14:42:47 -07:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as mime from 'mime';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as util from 'util';
|
2019-11-27 16:02:31 -08:00
|
|
|
import * as frames from './frames';
|
2020-04-20 07:52:26 -07:00
|
|
|
import { assert, helper } from './helper';
|
2020-05-15 15:21:49 -07:00
|
|
|
import InjectedScript from './injected/injectedScript';
|
|
|
|
import * as injectedScriptSource from './generated/injectedScriptSource';
|
2020-06-11 18:18:33 -07:00
|
|
|
import * as debugScriptSource from './generated/debugScriptSource';
|
2019-11-27 16:03:51 -08:00
|
|
|
import * as js from './javascript';
|
2019-12-12 21:11:52 -08:00
|
|
|
import { Page } from './page';
|
2020-03-25 14:08:46 -07:00
|
|
|
import { selectors } from './selectors';
|
2020-04-01 14:42:47 -07:00
|
|
|
import * as types from './types';
|
2020-06-16 17:11:19 -07:00
|
|
|
import { Progress, ProgressController } from './progress';
|
2020-06-11 18:18:33 -07:00
|
|
|
import DebugScript from './debug/injected/debugScript';
|
2019-11-27 16:02:31 -08:00
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
export class FrameExecutionContext extends js.ExecutionContext {
|
2019-12-19 15:19:22 -08:00
|
|
|
readonly frame: frames.Frame;
|
2020-06-11 18:18:33 -07:00
|
|
|
private _injectedScriptPromise?: Promise<js.JSHandle>;
|
|
|
|
private _debugScriptPromise?: Promise<js.JSHandle | undefined>;
|
2019-11-28 12:50:52 -08:00
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
constructor(delegate: js.ExecutionContextDelegate, frame: frames.Frame) {
|
2020-06-09 16:11:17 -07:00
|
|
|
super(delegate);
|
2019-12-19 15:19:22 -08:00
|
|
|
this.frame = frame;
|
2019-12-02 13:12:28 -08:00
|
|
|
}
|
|
|
|
|
2020-05-15 15:21:49 -07:00
|
|
|
adoptIfNeeded(handle: js.JSHandle): Promise<js.JSHandle> | null {
|
2020-03-19 13:07:33 -07:00
|
|
|
if (handle instanceof ElementHandle && handle._context !== this)
|
|
|
|
return this.frame._page._delegate.adoptElementHandle(handle, this);
|
|
|
|
return null;
|
|
|
|
}
|
2019-12-19 11:44:07 -08:00
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async evaluateInternal<R>(pageFunction: js.Func0<R>): Promise<R>;
|
|
|
|
async evaluateInternal<Arg, R>(pageFunction: js.Func1<Arg, R>, arg: Arg): Promise<R>;
|
2020-06-03 13:22:05 -07:00
|
|
|
async evaluateInternal(pageFunction: never, ...args: never[]): Promise<any> {
|
|
|
|
return await this.frame._page._frameManager.waitForSignalsCreatedBy(null, false /* noWaitFor */, async () => {
|
2020-06-03 17:50:16 -07:00
|
|
|
return js.evaluate(this, true /* returnByValue */, pageFunction, ...args);
|
2020-06-03 13:22:05 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async evaluateHandleInternal<R>(pageFunction: js.Func0<R>): Promise<js.SmartHandle<R>>;
|
|
|
|
async evaluateHandleInternal<Arg, R>(pageFunction: js.Func1<Arg, R>, arg: Arg): Promise<js.SmartHandle<R>>;
|
2020-06-03 13:22:05 -07:00
|
|
|
async evaluateHandleInternal(pageFunction: never, ...args: never[]): Promise<any> {
|
|
|
|
return await this.frame._page._frameManager.waitForSignalsCreatedBy(null, false /* noWaitFor */, async () => {
|
2020-06-03 17:50:16 -07:00
|
|
|
return js.evaluate(this, false /* returnByValue */, pageFunction, ...args);
|
2020-06-03 11:23:24 -07:00
|
|
|
});
|
2019-12-19 11:44:07 -08:00
|
|
|
}
|
|
|
|
|
2020-05-27 22:19:05 -07:00
|
|
|
createHandle(remoteObject: js.RemoteObject): js.JSHandle {
|
2019-12-19 15:19:22 -08:00
|
|
|
if (this.frame._page._delegate.isElementHandle(remoteObject))
|
2020-05-27 22:19:05 -07:00
|
|
|
return new ElementHandle(this, remoteObject.objectId!);
|
2020-05-15 15:21:49 -07:00
|
|
|
return super.createHandle(remoteObject);
|
2019-12-12 21:11:52 -08:00
|
|
|
}
|
|
|
|
|
2020-05-15 15:21:49 -07:00
|
|
|
injectedScript(): Promise<js.JSHandle<InjectedScript>> {
|
2020-06-11 18:18:33 -07:00
|
|
|
if (!this._injectedScriptPromise) {
|
2020-05-15 15:21:49 -07:00
|
|
|
const custom: string[] = [];
|
|
|
|
for (const [name, { source }] of selectors._engines)
|
|
|
|
custom.push(`{ name: '${name}', engine: (${source}) }`);
|
|
|
|
const source = `
|
|
|
|
new (${injectedScriptSource.source})([
|
|
|
|
${custom.join(',\n')}
|
|
|
|
])
|
|
|
|
`;
|
2020-06-11 18:18:33 -07:00
|
|
|
this._injectedScriptPromise = this._delegate.rawEvaluate(source).then(objectId => new js.JSHandle(this, 'object', objectId));
|
2019-11-28 12:50:52 -08:00
|
|
|
}
|
2020-06-11 18:18:33 -07:00
|
|
|
return this._injectedScriptPromise;
|
|
|
|
}
|
|
|
|
|
2020-06-15 15:27:03 -07:00
|
|
|
createDebugScript(options: { record?: boolean, console?: boolean }): Promise<js.JSHandle<DebugScript> | undefined> {
|
2020-06-11 18:18:33 -07:00
|
|
|
if (!this._debugScriptPromise) {
|
|
|
|
const source = `new (${debugScriptSource.source})()`;
|
|
|
|
this._debugScriptPromise = this._delegate.rawEvaluate(source).then(objectId => new js.JSHandle(this, 'object', objectId)).then(async debugScript => {
|
|
|
|
const injectedScript = await this.injectedScript();
|
2020-06-15 15:27:03 -07:00
|
|
|
await debugScript.evaluate((debugScript: DebugScript, { injectedScript, options }) => debugScript.initialize(injectedScript, options), { injectedScript, options });
|
2020-06-11 18:18:33 -07:00
|
|
|
return debugScript;
|
|
|
|
}).catch(e => undefined);
|
|
|
|
}
|
|
|
|
return this._debugScriptPromise;
|
2019-11-28 12:50:52 -08:00
|
|
|
}
|
2019-12-02 17:33:44 -08:00
|
|
|
}
|
2019-12-02 13:12:28 -08:00
|
|
|
|
2019-12-05 16:26:09 -08:00
|
|
|
export class ElementHandle<T extends Node = Node> extends js.JSHandle<T> {
|
2019-12-12 21:11:52 -08:00
|
|
|
readonly _context: FrameExecutionContext;
|
|
|
|
readonly _page: Page;
|
2020-05-27 22:19:05 -07:00
|
|
|
readonly _objectId: string;
|
2019-11-27 16:02:31 -08:00
|
|
|
|
2020-05-27 22:19:05 -07:00
|
|
|
constructor(context: FrameExecutionContext, objectId: string) {
|
|
|
|
super(context, 'node', objectId);
|
|
|
|
this._objectId = objectId;
|
2020-01-13 13:33:25 -08:00
|
|
|
this._context = context;
|
2019-12-19 15:19:22 -08:00
|
|
|
this._page = context.frame._page;
|
2020-06-12 11:10:18 -07:00
|
|
|
this._initializePreview().catch(e => {});
|
|
|
|
}
|
|
|
|
|
2020-06-16 17:11:19 -07:00
|
|
|
private _runAbortableTask<T>(task: (progress: Progress) => Promise<T>, timeout: number, apiName: string): Promise<T> {
|
|
|
|
const controller = new ProgressController(this._page._logger, timeout, `elementHandle.${apiName}`);
|
|
|
|
return controller.run(task);
|
|
|
|
}
|
|
|
|
|
2020-06-12 11:10:18 -07:00
|
|
|
async _initializePreview() {
|
|
|
|
const utility = await this._context.injectedScript();
|
|
|
|
this._preview = await utility.evaluate((injected, e) => 'JSHandle@' + injected.previewNode(e), this);
|
2019-12-18 13:51:45 -08:00
|
|
|
}
|
|
|
|
|
2019-12-05 16:26:09 -08:00
|
|
|
asElement(): ElementHandle<T> | null {
|
2019-11-27 16:02:31 -08:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async _evaluateInMain<R, Arg>(pageFunction: js.Func1<[js.JSHandle<InjectedScript>, ElementHandle<T>, Arg], R>, arg: Arg): Promise<R> {
|
2020-04-23 14:58:37 -07:00
|
|
|
const main = await this._context.frame._mainContext();
|
2020-06-03 13:22:05 -07:00
|
|
|
return main.evaluateInternal(pageFunction, [await main.injectedScript(), this, arg]);
|
2020-04-23 14:58:37 -07:00
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async _evaluateInUtility<R, Arg>(pageFunction: js.Func1<[js.JSHandle<InjectedScript>, ElementHandle<T>, Arg], R>, arg: Arg): Promise<R> {
|
2019-12-19 15:19:22 -08:00
|
|
|
const utility = await this._context.frame._utilityContext();
|
2020-06-03 13:22:05 -07:00
|
|
|
return utility.evaluateInternal(pageFunction, [await utility.injectedScript(), this, arg]);
|
2019-12-19 15:19:22 -08:00
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async _evaluateHandleInUtility<R, Arg>(pageFunction: js.Func1<[js.JSHandle<InjectedScript>, ElementHandle<T>, Arg], R>, arg: Arg): Promise<js.JSHandle<R>> {
|
2020-05-30 15:00:53 -07:00
|
|
|
const utility = await this._context.frame._utilityContext();
|
2020-06-03 13:22:05 -07:00
|
|
|
return utility.evaluateHandleInternal(pageFunction, [await utility.injectedScript(), this, arg]);
|
2020-05-30 15:00:53 -07:00
|
|
|
}
|
|
|
|
|
2019-12-19 15:19:22 -08:00
|
|
|
async ownerFrame(): Promise<frames.Frame | null> {
|
2020-01-27 11:43:43 -08:00
|
|
|
const frameId = await this._page._delegate.getOwnerFrame(this);
|
|
|
|
if (!frameId)
|
|
|
|
return null;
|
2020-04-20 13:01:06 -07:00
|
|
|
const frame = this._page._frameManager.frame(frameId);
|
|
|
|
if (frame)
|
|
|
|
return frame;
|
2020-03-13 11:33:33 -07:00
|
|
|
for (const page of this._page._browserContext.pages()) {
|
2020-01-27 11:43:43 -08:00
|
|
|
const frame = page._frameManager.frame(frameId);
|
|
|
|
if (frame)
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
return null;
|
2019-12-19 15:19:22 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
async contentFrame(): Promise<frames.Frame | null> {
|
2020-06-03 13:22:05 -07:00
|
|
|
const isFrameElement = await this._evaluateInUtility(([injected, node]) => node && (node.nodeName === 'IFRAME' || node.nodeName === 'FRAME'), {});
|
2019-12-19 15:19:22 -08:00
|
|
|
if (!isFrameElement)
|
|
|
|
return null;
|
2019-12-12 21:11:52 -08:00
|
|
|
return this._page._delegate.getContentFrame(this);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2020-04-09 16:49:23 -07:00
|
|
|
async getAttribute(name: string): Promise<string | null> {
|
2020-06-03 13:22:05 -07:00
|
|
|
return this._evaluateInUtility(([injeced, node, name]) => {
|
2020-04-09 16:49:23 -07:00
|
|
|
if (node.nodeType !== Node.ELEMENT_NODE)
|
|
|
|
throw new Error('Not an element');
|
|
|
|
const element = node as unknown as Element;
|
|
|
|
return element.getAttribute(name);
|
|
|
|
}, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
async textContent(): Promise<string | null> {
|
2020-06-03 13:22:05 -07:00
|
|
|
return this._evaluateInUtility(([injected, node]) => node.textContent, {});
|
2020-04-09 16:49:23 -07:00
|
|
|
}
|
|
|
|
|
2020-05-18 17:58:23 -07:00
|
|
|
async innerText(): Promise<string> {
|
2020-06-03 13:22:05 -07:00
|
|
|
return this._evaluateInUtility(([injected, node]) => {
|
2020-04-09 16:49:23 -07:00
|
|
|
if (node.nodeType !== Node.ELEMENT_NODE)
|
|
|
|
throw new Error('Not an element');
|
2020-05-18 17:58:23 -07:00
|
|
|
if (node.namespaceURI !== 'http://www.w3.org/1999/xhtml')
|
|
|
|
throw new Error('Not an HTMLElement');
|
2020-04-09 16:49:23 -07:00
|
|
|
const element = node as unknown as HTMLElement;
|
|
|
|
return element.innerText;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
2020-05-18 17:58:23 -07:00
|
|
|
async innerHTML(): Promise<string> {
|
2020-06-03 13:22:05 -07:00
|
|
|
return this._evaluateInUtility(([injected, node]) => {
|
2020-04-09 16:49:23 -07:00
|
|
|
if (node.nodeType !== Node.ELEMENT_NODE)
|
|
|
|
throw new Error('Not an element');
|
|
|
|
const element = node as unknown as Element;
|
|
|
|
return element.innerHTML;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
2020-04-23 14:58:37 -07:00
|
|
|
async dispatchEvent(type: string, eventInit: Object = {}) {
|
2020-06-03 13:22:05 -07:00
|
|
|
await this._evaluateInMain(([injected, node, { type, eventInit }]) =>
|
2020-04-23 14:58:37 -07:00
|
|
|
injected.dispatchEvent(node, type, eventInit), { type, eventInit });
|
|
|
|
}
|
|
|
|
|
2020-06-12 14:59:26 -07:00
|
|
|
async _scrollRectIntoViewIfNeeded(rect?: types.Rect): Promise<'notvisible' | 'notconnected' | 'done'> {
|
2020-05-22 11:15:57 -07:00
|
|
|
return await this._page._delegate.scrollRectIntoViewIfNeeded(this, rect);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2020-06-23 13:02:31 -07:00
|
|
|
async scrollIntoViewIfNeeded(options: types.TimeoutOptions = {}) {
|
|
|
|
return this._runAbortableTask(async progress => {
|
|
|
|
while (progress.isRunning()) {
|
|
|
|
const waited = await this._waitForVisible(progress);
|
|
|
|
throwIfNotConnected(waited);
|
|
|
|
|
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
|
|
|
const result = await this._scrollRectIntoViewIfNeeded();
|
|
|
|
throwIfNotConnected(result);
|
|
|
|
if (result === 'notvisible')
|
|
|
|
continue;
|
|
|
|
assert(result === 'done');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}, this._page._timeoutSettings.timeout(options), 'scrollIntoViewIfNeeded');
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _waitForVisible(progress: Progress): Promise<'notconnected' | 'done'> {
|
|
|
|
const poll = await this._evaluateHandleInUtility(([injected, node]) => {
|
|
|
|
return injected.waitForNodeVisible(node);
|
|
|
|
}, {});
|
|
|
|
const pollHandler = new InjectedScriptPollHandler(progress, poll);
|
|
|
|
return throwIfError(await pollHandler.finish());
|
2019-12-05 09:54:50 -08:00
|
|
|
}
|
|
|
|
|
2020-06-12 14:59:26 -07:00
|
|
|
private async _clickablePoint(): Promise<types.Point | 'notvisible' | 'notinviewport'> {
|
2019-12-05 09:54:50 -08:00
|
|
|
const intersectQuadWithViewport = (quad: types.Quad): types.Quad => {
|
|
|
|
return quad.map(point => ({
|
|
|
|
x: Math.min(Math.max(point.x, 0), metrics.width),
|
|
|
|
y: Math.min(Math.max(point.y, 0), metrics.height),
|
|
|
|
})) as types.Quad;
|
|
|
|
};
|
|
|
|
|
|
|
|
const computeQuadArea = (quad: types.Quad) => {
|
|
|
|
// Compute sum of all directed areas of adjacent triangles
|
|
|
|
// https://en.wikipedia.org/wiki/Polygon#Simple_polygons
|
|
|
|
let area = 0;
|
|
|
|
for (let i = 0; i < quad.length; ++i) {
|
|
|
|
const p1 = quad[i];
|
|
|
|
const p2 = quad[(i + 1) % quad.length];
|
|
|
|
area += (p1.x * p2.y - p2.x * p1.y) / 2;
|
|
|
|
}
|
|
|
|
return Math.abs(area);
|
|
|
|
};
|
|
|
|
|
|
|
|
const [quads, metrics] = await Promise.all([
|
2019-12-12 21:11:52 -08:00
|
|
|
this._page._delegate.getContentQuads(this),
|
2020-06-22 14:03:31 -07:00
|
|
|
this._page.mainFrame()._utilityContext().then(utility => utility.evaluateInternal(() => ({width: innerWidth, height: innerHeight}))),
|
2020-02-05 16:53:36 -08:00
|
|
|
] as const);
|
2019-12-05 09:54:50 -08:00
|
|
|
if (!quads || !quads.length)
|
2020-06-12 14:59:26 -07:00
|
|
|
return 'notvisible';
|
2019-12-05 09:54:50 -08:00
|
|
|
|
|
|
|
const filtered = quads.map(quad => intersectQuadWithViewport(quad)).filter(quad => computeQuadArea(quad) > 1);
|
|
|
|
if (!filtered.length)
|
2020-06-12 14:59:26 -07:00
|
|
|
return 'notinviewport';
|
2019-12-05 09:54:50 -08:00
|
|
|
// Return the middle point of the first quad.
|
|
|
|
const result = { x: 0, y: 0 };
|
|
|
|
for (const point of filtered[0]) {
|
|
|
|
result.x += point.x / 4;
|
|
|
|
result.y += point.y / 4;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-06-12 14:59:26 -07:00
|
|
|
private async _offsetPoint(offset: types.Point): Promise<types.Point | 'notvisible'> {
|
2019-12-05 09:54:50 -08:00
|
|
|
const [box, border] = await Promise.all([
|
|
|
|
this.boundingBox(),
|
2020-06-09 16:11:17 -07:00
|
|
|
this._evaluateInUtility(([injected, node]) => injected.getElementBorderWidth(node), {}).catch(e => {}),
|
2019-12-05 09:54:50 -08:00
|
|
|
]);
|
2020-05-22 11:15:57 -07:00
|
|
|
if (!box || !border)
|
2020-06-12 14:59:26 -07:00
|
|
|
return 'notvisible';
|
2020-05-22 11:15:57 -07:00
|
|
|
// Make point relative to the padding box to align with offsetX/offsetY.
|
|
|
|
return {
|
|
|
|
x: box.x + border.left + offset.x,
|
|
|
|
y: box.y + border.top + offset.y,
|
|
|
|
};
|
2019-12-05 09:54:50 -08:00
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async _retryPointerAction(progress: Progress, action: (point: types.Point) => Promise<void>, options: types.PointerActionOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): Promise<'notconnected' | 'done'> {
|
2020-06-06 20:59:06 -07:00
|
|
|
let first = true;
|
2020-06-04 16:43:48 -07:00
|
|
|
while (progress.isRunning()) {
|
2020-06-16 17:11:19 -07:00
|
|
|
progress.logger.info(`${first ? 'attempting' : 'retrying'} ${progress.apiName} action`);
|
2020-06-03 09:14:53 -07:00
|
|
|
const result = await this._performPointerAction(progress, action, options);
|
2020-06-06 20:59:06 -07:00
|
|
|
first = false;
|
2020-06-12 14:59:26 -07:00
|
|
|
if (result === 'notvisible') {
|
|
|
|
if (options.force)
|
|
|
|
throw new Error('Element is not visible');
|
2020-06-16 17:11:19 -07:00
|
|
|
progress.logger.info(' element is not visible');
|
2020-06-12 14:59:26 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (result === 'notinviewport') {
|
|
|
|
if (options.force)
|
|
|
|
throw new Error('Element is outside of the viewport');
|
2020-06-16 17:11:19 -07:00
|
|
|
progress.logger.info(' element is outside of the viewport');
|
2020-06-12 14:59:26 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (result === 'nothittarget') {
|
|
|
|
if (options.force)
|
|
|
|
throw new Error('Element does not receive pointer events');
|
2020-06-16 17:11:19 -07:00
|
|
|
progress.logger.info(' element does not receive pointer events');
|
2020-06-12 14:59:26 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (result === 'notconnected')
|
|
|
|
return result;
|
|
|
|
break;
|
2020-04-29 11:05:23 -07:00
|
|
|
}
|
2020-06-12 14:59:26 -07:00
|
|
|
return 'done';
|
2020-04-29 11:05:23 -07:00
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async _performPointerAction(progress: Progress, action: (point: types.Point) => Promise<void>, options: types.PointerActionOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): Promise<'notvisible' | 'notconnected' | 'notinviewport' | 'nothittarget' | 'done'> {
|
2020-04-29 11:05:23 -07:00
|
|
|
const { force = false, position } = options;
|
2020-06-12 14:59:26 -07:00
|
|
|
if (!force) {
|
|
|
|
const result = await this._waitForDisplayedAtStablePositionAndEnabled(progress);
|
|
|
|
if (result === 'notconnected')
|
|
|
|
return 'notconnected';
|
|
|
|
}
|
2020-06-11 15:19:35 -07:00
|
|
|
if ((options as any).__testHookAfterStable)
|
|
|
|
await (options as any).__testHookAfterStable();
|
2020-04-07 14:35:34 -07:00
|
|
|
|
2020-06-16 17:11:19 -07:00
|
|
|
progress.logger.info(' scrolling into view if needed');
|
2020-06-09 18:57:11 -07:00
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
2020-06-01 11:14:16 -07:00
|
|
|
const scrolled = await this._scrollRectIntoViewIfNeeded(position ? { x: position.x, y: position.y, width: 0, height: 0 } : undefined);
|
2020-06-12 14:59:26 -07:00
|
|
|
if (scrolled === 'notvisible')
|
|
|
|
return 'notvisible';
|
|
|
|
if (scrolled === 'notconnected')
|
|
|
|
return 'notconnected';
|
2020-06-16 17:11:19 -07:00
|
|
|
progress.logger.info(' done scrolling');
|
2020-06-01 11:14:16 -07:00
|
|
|
|
|
|
|
const maybePoint = position ? await this._offsetPoint(position) : await this._clickablePoint();
|
2020-06-12 14:59:26 -07:00
|
|
|
if (maybePoint === 'notvisible')
|
|
|
|
return 'notvisible';
|
|
|
|
if (maybePoint === 'notinviewport')
|
|
|
|
return 'notinviewport';
|
2020-06-01 11:14:16 -07:00
|
|
|
const point = roundPoint(maybePoint);
|
|
|
|
|
|
|
|
if (!force) {
|
|
|
|
if ((options as any).__testHookBeforeHitTarget)
|
|
|
|
await (options as any).__testHookBeforeHitTarget();
|
2020-06-16 17:11:19 -07:00
|
|
|
progress.logger.info(` checking that element receives pointer events at (${point.x},${point.y})`);
|
2020-06-12 14:59:26 -07:00
|
|
|
const hitTargetResult = await this._checkHitTargetAt(point);
|
|
|
|
if (hitTargetResult === 'notconnected')
|
|
|
|
return 'notconnected';
|
|
|
|
if (hitTargetResult === 'nothittarget')
|
|
|
|
return 'nothittarget';
|
2020-06-16 17:11:19 -07:00
|
|
|
progress.logger.info(` element does receive pointer events`);
|
2020-04-29 11:05:23 -07:00
|
|
|
}
|
2020-06-01 11:14:16 -07:00
|
|
|
|
2020-06-03 11:23:24 -07:00
|
|
|
await this._page._frameManager.waitForSignalsCreatedBy(progress, options.noWaitAfter, async () => {
|
2020-06-09 18:57:11 -07:00
|
|
|
if ((options as any).__testHookBeforePointerAction)
|
|
|
|
await (options as any).__testHookBeforePointerAction();
|
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
2020-06-23 14:51:06 -07:00
|
|
|
let restoreModifiers: types.KeyboardModifier[] | undefined;
|
2020-06-01 11:14:16 -07:00
|
|
|
if (options && options.modifiers)
|
|
|
|
restoreModifiers = await this._page.keyboard._ensureModifiers(options.modifiers);
|
2020-06-16 17:11:19 -07:00
|
|
|
progress.logger.info(` performing ${progress.apiName} action`);
|
2020-06-01 11:14:16 -07:00
|
|
|
await action(point);
|
2020-06-16 17:11:19 -07:00
|
|
|
progress.logger.info(` ${progress.apiName} action done`);
|
|
|
|
progress.logger.info(' waiting for scheduled navigations to finish');
|
2020-06-04 16:43:48 -07:00
|
|
|
if ((options as any).__testHookAfterPointerAction)
|
|
|
|
await (options as any).__testHookAfterPointerAction();
|
2020-06-01 11:14:16 -07:00
|
|
|
if (restoreModifiers)
|
|
|
|
await this._page.keyboard._ensureModifiers(restoreModifiers);
|
2020-06-03 11:23:24 -07:00
|
|
|
}, 'input');
|
2020-06-16 17:11:19 -07:00
|
|
|
progress.logger.info(' navigations have finished');
|
2020-06-01 11:14:16 -07:00
|
|
|
|
|
|
|
return 'done';
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
hover(options: types.PointerActionOptions & types.PointerActionWaitOptions = {}): Promise<void> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._runAbortableTask(async progress => {
|
2020-06-12 14:59:26 -07:00
|
|
|
throwIfNotConnected(await this._hover(progress, options));
|
2020-06-16 17:11:19 -07:00
|
|
|
}, this._page._timeoutSettings.timeout(options), 'hover');
|
2020-06-03 09:14:53 -07:00
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
_hover(progress: Progress, options: types.PointerActionOptions & types.PointerActionWaitOptions): Promise<'notconnected' | 'done'> {
|
2020-06-03 09:14:53 -07:00
|
|
|
return this._retryPointerAction(progress, point => this._page.mouse.move(point.x, point.y), options);
|
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
click(options: types.MouseClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions = {}): Promise<void> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._runAbortableTask(async progress => {
|
2020-06-12 14:59:26 -07:00
|
|
|
throwIfNotConnected(await this._click(progress, options));
|
2020-06-16 17:11:19 -07:00
|
|
|
}, this._page._timeoutSettings.timeout(options), 'click');
|
2020-06-03 09:14:53 -07:00
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
_click(progress: Progress, options: types.MouseClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): Promise<'notconnected' | 'done'> {
|
2020-06-03 09:14:53 -07:00
|
|
|
return this._retryPointerAction(progress, point => this._page.mouse.click(point.x, point.y, options), options);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
dblclick(options: types.MouseMultiClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions = {}): Promise<void> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._runAbortableTask(async progress => {
|
2020-06-12 14:59:26 -07:00
|
|
|
throwIfNotConnected(await this._dblclick(progress, options));
|
2020-06-16 17:11:19 -07:00
|
|
|
}, this._page._timeoutSettings.timeout(options), 'dblclick');
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
_dblclick(progress: Progress, options: types.MouseMultiClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): Promise<'notconnected' | 'done'> {
|
2020-06-03 09:14:53 -07:00
|
|
|
return this._retryPointerAction(progress, point => this._page.mouse.dblclick(point.x, point.y, options), options);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2020-06-10 13:37:29 -03:00
|
|
|
async selectOption(values: string | ElementHandle | types.SelectOption | string[] | ElementHandle[] | types.SelectOption[] | null, options: types.NavigatingActionWaitOptions = {}): Promise<string[]> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._runAbortableTask(async progress => {
|
2020-06-12 14:59:26 -07:00
|
|
|
return throwIfNotConnected(await this._selectOption(progress, values, options));
|
2020-06-16 17:11:19 -07:00
|
|
|
}, this._page._timeoutSettings.timeout(options), 'selectOption');
|
2020-06-03 11:23:24 -07:00
|
|
|
}
|
|
|
|
|
2020-06-12 14:59:26 -07:00
|
|
|
async _selectOption(progress: Progress, values: string | ElementHandle | types.SelectOption | string[] | ElementHandle[] | types.SelectOption[] | null, options: types.NavigatingActionWaitOptions): Promise<string[] | 'notconnected'> {
|
2020-03-06 08:24:32 -08:00
|
|
|
let vals: string[] | ElementHandle[] | types.SelectOption[];
|
2020-06-10 13:37:29 -03:00
|
|
|
if (values === null)
|
|
|
|
vals = [];
|
|
|
|
else if (!Array.isArray(values))
|
2020-03-06 08:24:32 -08:00
|
|
|
vals = [ values ] as (string[] | ElementHandle[] | types.SelectOption[]);
|
|
|
|
else
|
|
|
|
vals = values;
|
|
|
|
const selectOptions = (vals as any).map((value: any) => typeof value === 'object' ? value : { value });
|
|
|
|
for (const option of selectOptions) {
|
2020-06-10 13:37:29 -03:00
|
|
|
assert(option !== null, 'Value items must not be null');
|
2019-11-27 16:02:31 -08:00
|
|
|
if (option instanceof ElementHandle)
|
|
|
|
continue;
|
|
|
|
if (option.value !== undefined)
|
|
|
|
assert(helper.isString(option.value), 'Values must be strings. Found value "' + option.value + '" of type "' + (typeof option.value) + '"');
|
|
|
|
if (option.label !== undefined)
|
|
|
|
assert(helper.isString(option.label), 'Labels must be strings. Found label "' + option.label + '" of type "' + (typeof option.label) + '"');
|
|
|
|
if (option.index !== undefined)
|
|
|
|
assert(helper.isNumber(option.index), 'Indices must be numbers. Found index "' + option.index + '" of type "' + (typeof option.index) + '"');
|
|
|
|
}
|
2020-06-12 14:59:26 -07:00
|
|
|
return this._page._frameManager.waitForSignalsCreatedBy(progress, options.noWaitAfter, async () => {
|
2020-06-09 18:57:11 -07:00
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
2020-06-03 13:22:05 -07:00
|
|
|
const injectedResult = await this._evaluateInUtility(([injected, node, selectOptions]) => injected.selectOptions(node, selectOptions), selectOptions);
|
2020-06-12 14:59:26 -07:00
|
|
|
return throwIfError(injectedResult);
|
2020-06-03 11:23:24 -07:00
|
|
|
});
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2020-06-03 09:14:53 -07:00
|
|
|
async fill(value: string, options: types.NavigatingActionWaitOptions = {}): Promise<void> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._runAbortableTask(async progress => {
|
2020-06-12 14:59:26 -07:00
|
|
|
throwIfNotConnected(await this._fill(progress, value, options));
|
2020-06-16 17:11:19 -07:00
|
|
|
}, this._page._timeoutSettings.timeout(options), 'fill');
|
2020-06-03 09:14:53 -07:00
|
|
|
}
|
|
|
|
|
2020-06-12 14:59:26 -07:00
|
|
|
async _fill(progress: Progress, value: string, options: types.NavigatingActionWaitOptions): Promise<'notconnected' | 'done'> {
|
2020-06-16 17:11:19 -07:00
|
|
|
progress.logger.info(`elementHandle.fill("${value}")`);
|
2019-11-27 16:02:31 -08:00
|
|
|
assert(helper.isString(value), 'Value must be string. Found value "' + value + '" of type "' + (typeof value) + '"');
|
2020-06-12 14:59:26 -07:00
|
|
|
return this._page._frameManager.waitForSignalsCreatedBy(progress, options.noWaitAfter, async () => {
|
2020-06-16 17:11:19 -07:00
|
|
|
progress.logger.info(' waiting for element to be visible, enabled and editable');
|
2020-06-03 13:22:05 -07:00
|
|
|
const poll = await this._evaluateHandleInUtility(([injected, node, value]) => {
|
2020-06-01 18:56:49 -07:00
|
|
|
return injected.waitForEnabledAndFill(node, value);
|
2020-06-03 13:22:05 -07:00
|
|
|
}, value);
|
2020-06-06 20:59:06 -07:00
|
|
|
const pollHandler = new InjectedScriptPollHandler(progress, poll);
|
2020-06-12 14:59:26 -07:00
|
|
|
const filled = throwIfError(await pollHandler.finish());
|
2020-06-09 18:57:11 -07:00
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
2020-06-12 14:59:26 -07:00
|
|
|
if (filled === 'notconnected')
|
|
|
|
return 'notconnected';
|
2020-06-16 17:11:19 -07:00
|
|
|
progress.logger.info(' element is visible, enabled and editable');
|
2020-06-12 14:59:26 -07:00
|
|
|
if (filled === 'needsinput') {
|
2020-06-03 09:14:53 -07:00
|
|
|
if (value)
|
|
|
|
await this._page.keyboard.insertText(value);
|
|
|
|
else
|
|
|
|
await this._page.keyboard.press('Delete');
|
2020-04-07 10:07:06 -07:00
|
|
|
}
|
2020-06-12 14:59:26 -07:00
|
|
|
return 'done';
|
2020-06-03 11:23:24 -07:00
|
|
|
}, 'input');
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2020-06-23 13:02:31 -07:00
|
|
|
async selectText(options: types.TimeoutOptions = {}): Promise<void> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._runAbortableTask(async progress => {
|
2020-06-09 18:57:11 -07:00
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
2020-06-23 13:02:31 -07:00
|
|
|
const poll = await this._evaluateHandleInUtility(([injected, node]) => {
|
|
|
|
return injected.waitForVisibleAndSelectText(node);
|
|
|
|
}, {});
|
|
|
|
const pollHandler = new InjectedScriptPollHandler(progress, poll);
|
|
|
|
const result = throwIfError(await pollHandler.finish());
|
|
|
|
throwIfNotConnected(result);
|
|
|
|
}, this._page._timeoutSettings.timeout(options), 'selectText');
|
2020-04-14 17:09:26 -07:00
|
|
|
}
|
|
|
|
|
2020-06-03 11:23:24 -07:00
|
|
|
async setInputFiles(files: string | types.FilePayload | string[] | types.FilePayload[], options: types.NavigatingActionWaitOptions = {}) {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._runAbortableTask(async progress => {
|
2020-06-12 14:59:26 -07:00
|
|
|
throwIfNotConnected(await this._setInputFiles(progress, files, options));
|
2020-06-16 17:11:19 -07:00
|
|
|
}, this._page._timeoutSettings.timeout(options), 'setInputFiles');
|
2020-06-03 11:23:24 -07:00
|
|
|
}
|
|
|
|
|
2020-06-12 14:59:26 -07:00
|
|
|
async _setInputFiles(progress: Progress, files: string | types.FilePayload | string[] | types.FilePayload[], options: types.NavigatingActionWaitOptions): Promise<'notconnected' | 'done'> {
|
|
|
|
const multiple = throwIfError(await this._evaluateInUtility(([injected, node]): types.InjectedScriptResult<'notconnected' | boolean> => {
|
2020-03-20 15:08:17 -07:00
|
|
|
if (node.nodeType !== Node.ELEMENT_NODE || (node as Node as Element).tagName !== 'INPUT')
|
2020-06-12 14:59:26 -07:00
|
|
|
return { error: 'Node is not an HTMLInputElement', value: false };
|
2020-04-18 18:29:31 -07:00
|
|
|
if (!node.isConnected)
|
2020-06-12 14:59:26 -07:00
|
|
|
return { value: 'notconnected' };
|
2020-03-20 15:08:17 -07:00
|
|
|
const input = node as Node as HTMLInputElement;
|
2020-06-12 14:59:26 -07:00
|
|
|
return { value: input.multiple };
|
|
|
|
}, {}));
|
|
|
|
if (multiple === 'notconnected')
|
|
|
|
return 'notconnected';
|
2020-03-06 08:24:32 -08:00
|
|
|
let ff: string[] | types.FilePayload[];
|
|
|
|
if (!Array.isArray(files))
|
|
|
|
ff = [ files ] as string[] | types.FilePayload[];
|
|
|
|
else
|
|
|
|
ff = files;
|
|
|
|
assert(multiple || ff.length <= 1, 'Non-multiple file input can only accept single file!');
|
|
|
|
const filePayloads: types.FilePayload[] = [];
|
|
|
|
for (const item of ff) {
|
2020-01-03 12:59:06 -08:00
|
|
|
if (typeof item === 'string') {
|
|
|
|
const file: types.FilePayload = {
|
2020-04-01 14:42:47 -07:00
|
|
|
name: path.basename(item),
|
2020-04-16 10:25:28 -07:00
|
|
|
mimeType: mime.getType(item) || 'application/octet-stream',
|
|
|
|
buffer: await util.promisify(fs.readFile)(item)
|
2020-01-03 12:59:06 -08:00
|
|
|
};
|
2020-03-06 08:24:32 -08:00
|
|
|
filePayloads.push(file);
|
|
|
|
} else {
|
|
|
|
filePayloads.push(item);
|
2020-01-03 12:59:06 -08:00
|
|
|
}
|
2020-03-06 08:24:32 -08:00
|
|
|
}
|
2020-06-03 11:23:24 -07:00
|
|
|
await this._page._frameManager.waitForSignalsCreatedBy(progress, options.noWaitAfter, async () => {
|
2020-06-09 18:57:11 -07:00
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
2020-03-04 19:15:01 -08:00
|
|
|
await this._page._delegate.setInputFiles(this as any as ElementHandle<HTMLInputElement>, filePayloads);
|
2020-06-03 11:23:24 -07:00
|
|
|
});
|
2020-06-12 14:59:26 -07:00
|
|
|
return 'done';
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2020-06-12 14:59:26 -07:00
|
|
|
async focus(): Promise<void> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._runAbortableTask(async progress => {
|
2020-06-12 14:59:26 -07:00
|
|
|
throwIfNotConnected(await this._focus(progress));
|
2020-06-16 17:11:19 -07:00
|
|
|
}, 0, 'focus');
|
2020-06-09 16:11:17 -07:00
|
|
|
}
|
|
|
|
|
2020-06-12 14:59:26 -07:00
|
|
|
async _focus(progress: Progress): Promise<'notconnected' | 'done'> {
|
2020-06-09 18:57:11 -07:00
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
2020-06-12 14:59:26 -07:00
|
|
|
return throwIfError(await this._evaluateInUtility(([injected, node]) => injected.focusNode(node), {}));
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2020-06-12 14:59:26 -07:00
|
|
|
async type(text: string, options: { delay?: number } & types.NavigatingActionWaitOptions = {}): Promise<void> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._runAbortableTask(async progress => {
|
2020-06-12 14:59:26 -07:00
|
|
|
throwIfNotConnected(await this._type(progress, text, options));
|
2020-06-16 17:11:19 -07:00
|
|
|
}, this._page._timeoutSettings.timeout(options), 'type');
|
2020-06-03 11:23:24 -07:00
|
|
|
}
|
|
|
|
|
2020-06-12 14:59:26 -07:00
|
|
|
async _type(progress: Progress, text: string, options: { delay?: number } & types.NavigatingActionWaitOptions): Promise<'notconnected' | 'done'> {
|
2020-06-16 17:11:19 -07:00
|
|
|
progress.logger.info(`elementHandle.type("${text}")`);
|
2020-06-03 11:23:24 -07:00
|
|
|
return this._page._frameManager.waitForSignalsCreatedBy(progress, options.noWaitAfter, async () => {
|
2020-06-12 14:59:26 -07:00
|
|
|
const focused = await this._focus(progress);
|
|
|
|
if (focused === 'notconnected')
|
|
|
|
return 'notconnected';
|
2020-06-09 18:57:11 -07:00
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
2020-03-04 19:15:01 -08:00
|
|
|
await this._page.keyboard.type(text, options);
|
2020-06-12 14:59:26 -07:00
|
|
|
return 'done';
|
2020-06-03 11:23:24 -07:00
|
|
|
}, 'input');
|
|
|
|
}
|
|
|
|
|
2020-06-12 14:59:26 -07:00
|
|
|
async press(key: string, options: { delay?: number } & types.NavigatingActionWaitOptions = {}): Promise<void> {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._runAbortableTask(async progress => {
|
2020-06-12 14:59:26 -07:00
|
|
|
throwIfNotConnected(await this._press(progress, key, options));
|
2020-06-16 17:11:19 -07:00
|
|
|
}, this._page._timeoutSettings.timeout(options), 'press');
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2020-06-12 14:59:26 -07:00
|
|
|
async _press(progress: Progress, key: string, options: { delay?: number } & types.NavigatingActionWaitOptions): Promise<'notconnected' | 'done'> {
|
2020-06-16 17:11:19 -07:00
|
|
|
progress.logger.info(`elementHandle.press("${key}")`);
|
2020-06-03 11:23:24 -07:00
|
|
|
return this._page._frameManager.waitForSignalsCreatedBy(progress, options.noWaitAfter, async () => {
|
2020-06-12 14:59:26 -07:00
|
|
|
const focused = await this._focus(progress);
|
|
|
|
if (focused === 'notconnected')
|
|
|
|
return 'notconnected';
|
2020-06-09 18:57:11 -07:00
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
2020-03-04 19:15:01 -08:00
|
|
|
await this._page.keyboard.press(key, options);
|
2020-06-12 14:59:26 -07:00
|
|
|
return 'done';
|
2020-06-03 11:23:24 -07:00
|
|
|
}, 'input');
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
2020-02-19 09:34:57 -08:00
|
|
|
|
2020-06-03 09:14:53 -07:00
|
|
|
async check(options: types.PointerActionWaitOptions & types.NavigatingActionWaitOptions = {}) {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._runAbortableTask(progress => this._setChecked(progress, true, options), this._page._timeoutSettings.timeout(options), 'check');
|
2020-02-04 14:39:11 -08:00
|
|
|
}
|
|
|
|
|
2020-06-03 09:14:53 -07:00
|
|
|
async uncheck(options: types.PointerActionWaitOptions & types.NavigatingActionWaitOptions = {}) {
|
2020-06-16 17:11:19 -07:00
|
|
|
return this._runAbortableTask(progress => this._setChecked(progress, false, options), this._page._timeoutSettings.timeout(options), 'uncheck');
|
2020-02-04 14:39:11 -08:00
|
|
|
}
|
|
|
|
|
2020-06-03 09:14:53 -07:00
|
|
|
async _setChecked(progress: Progress, state: boolean, options: types.PointerActionWaitOptions & types.NavigatingActionWaitOptions) {
|
2020-06-03 13:22:05 -07:00
|
|
|
if (await this._evaluateInUtility(([injected, node]) => injected.isCheckboxChecked(node), {}) === state)
|
2020-02-04 14:39:11 -08:00
|
|
|
return;
|
2020-06-03 09:14:53 -07:00
|
|
|
await this._click(progress, options);
|
2020-06-03 13:22:05 -07:00
|
|
|
if (await this._evaluateInUtility(([injected, node]) => injected.isCheckboxChecked(node), {}) !== state)
|
2020-02-04 14:39:11 -08:00
|
|
|
throw new Error('Unable to click checkbox');
|
|
|
|
}
|
2019-11-27 16:02:31 -08:00
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
async boundingBox(): Promise<types.Rect | null> {
|
2019-12-12 21:11:52 -08:00
|
|
|
return this._page._delegate.getBoundingBox(this);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2020-04-01 14:42:47 -07:00
|
|
|
async screenshot(options?: types.ElementScreenshotOptions): Promise<Buffer> {
|
2019-12-12 21:11:52 -08:00
|
|
|
return this._page._screenshotter.screenshotElement(this, options);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2020-04-20 13:01:06 -07:00
|
|
|
async $(selector: string): Promise<ElementHandle | null> {
|
2020-03-25 14:08:46 -07:00
|
|
|
return selectors._query(this._context.frame, selector, this);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2020-04-20 13:01:06 -07:00
|
|
|
async $$(selector: string): Promise<ElementHandle<Element>[]> {
|
2020-03-25 14:08:46 -07:00
|
|
|
return selectors._queryAll(this._context.frame, selector, this);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async $eval<R, Arg>(selector: string, pageFunction: js.FuncOn<Element, Arg, R>, arg: Arg): Promise<R>;
|
|
|
|
async $eval<R>(selector: string, pageFunction: js.FuncOn<Element, void, R>, arg?: any): Promise<R>;
|
|
|
|
async $eval<R, Arg>(selector: string, pageFunction: js.FuncOn<Element, Arg, R>, arg: Arg): Promise<R> {
|
2020-03-25 14:08:46 -07:00
|
|
|
const handle = await selectors._query(this._context.frame, selector, this);
|
|
|
|
if (!handle)
|
2019-12-17 14:30:02 -08:00
|
|
|
throw new Error(`Error: failed to find element matching selector "${selector}"`);
|
2020-03-25 14:08:46 -07:00
|
|
|
const result = await handle.evaluate(pageFunction, arg);
|
|
|
|
handle.dispose();
|
2019-12-17 14:30:02 -08:00
|
|
|
return result;
|
2019-12-04 13:11:10 -08:00
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async $$eval<R, Arg>(selector: string, pageFunction: js.FuncOn<Element[], Arg, R>, arg: Arg): Promise<R>;
|
|
|
|
async $$eval<R>(selector: string, pageFunction: js.FuncOn<Element[], void, R>, arg?: any): Promise<R>;
|
|
|
|
async $$eval<R, Arg>(selector: string, pageFunction: js.FuncOn<Element[], Arg, R>, arg: Arg): Promise<R> {
|
2020-03-25 14:08:46 -07:00
|
|
|
const arrayHandle = await selectors._queryArray(this._context.frame, selector, this);
|
2020-03-20 15:08:17 -07:00
|
|
|
const result = await arrayHandle.evaluate(pageFunction, arg);
|
2020-03-04 17:57:35 -08:00
|
|
|
arrayHandle.dispose();
|
2019-12-17 14:30:02 -08:00
|
|
|
return result;
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2020-06-12 14:59:26 -07:00
|
|
|
async _waitForDisplayedAtStablePositionAndEnabled(progress: Progress): Promise<'notconnected' | 'done'> {
|
2020-06-16 17:11:19 -07:00
|
|
|
progress.logger.info(' waiting for element to be visible, enabled and not moving');
|
2020-05-04 16:30:19 -07:00
|
|
|
const rafCount = this._page._delegate.rafCountForStablePosition();
|
2020-06-09 18:57:11 -07:00
|
|
|
const poll = this._evaluateHandleInUtility(([injected, node, rafCount]) => {
|
2020-05-30 15:00:53 -07:00
|
|
|
return injected.waitForDisplayedAtStablePositionAndEnabled(node, rafCount);
|
2020-06-03 13:22:05 -07:00
|
|
|
}, rafCount);
|
2020-06-12 14:59:26 -07:00
|
|
|
const pollHandler = new InjectedScriptPollHandler(progress, await poll);
|
|
|
|
const result = throwIfError(await pollHandler.finish());
|
2020-06-16 17:11:19 -07:00
|
|
|
progress.logger.info(' element is visible, enabled and does not move');
|
2020-06-12 14:59:26 -07:00
|
|
|
return result;
|
2020-02-19 09:34:57 -08:00
|
|
|
}
|
|
|
|
|
2020-06-12 14:59:26 -07:00
|
|
|
async _checkHitTargetAt(point: types.Point): Promise<'notconnected' | 'nothittarget' | 'done'> {
|
2020-02-19 09:34:57 -08:00
|
|
|
const frame = await this.ownerFrame();
|
|
|
|
if (frame && frame.parentFrame()) {
|
|
|
|
const element = await frame.frameElement();
|
|
|
|
const box = await element.boundingBox();
|
|
|
|
if (!box)
|
2020-06-12 14:59:26 -07:00
|
|
|
return 'notconnected';
|
2020-02-19 09:34:57 -08:00
|
|
|
// Translate from viewport coordinates to frame coordinates.
|
|
|
|
point = { x: point.x - box.x, y: point.y - box.y };
|
|
|
|
}
|
2020-06-03 13:22:05 -07:00
|
|
|
const injectedResult = await this._evaluateInUtility(([injected, node, point]) => {
|
2020-04-29 11:05:23 -07:00
|
|
|
return injected.checkHitTargetAt(node, point);
|
2020-06-03 13:22:05 -07:00
|
|
|
}, point);
|
2020-06-12 14:59:26 -07:00
|
|
|
return throwIfError(injectedResult);
|
2020-02-19 09:34:57 -08:00
|
|
|
}
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
2019-12-03 10:43:13 -08:00
|
|
|
|
2020-06-01 15:48:23 -07:00
|
|
|
// Handles an InjectedScriptPoll running in injected script:
|
|
|
|
// - streams logs into progress;
|
|
|
|
// - cancels the poll when progress cancels.
|
2020-06-06 20:59:06 -07:00
|
|
|
export class InjectedScriptPollHandler<T> {
|
2020-06-01 15:48:23 -07:00
|
|
|
private _progress: Progress;
|
2020-06-06 20:59:06 -07:00
|
|
|
private _poll: js.JSHandle<types.InjectedScriptPoll<T>> | null;
|
2020-06-01 15:48:23 -07:00
|
|
|
|
2020-06-06 20:59:06 -07:00
|
|
|
constructor(progress: Progress, poll: js.JSHandle<types.InjectedScriptPoll<T>>) {
|
2020-06-01 15:48:23 -07:00
|
|
|
this._progress = progress;
|
|
|
|
this._poll = poll;
|
2020-06-09 18:57:11 -07:00
|
|
|
// Ensure we cancel the poll before progress aborts and returns:
|
|
|
|
// - no unnecessary work in the page;
|
|
|
|
// - no possible side effects after progress promsie rejects.
|
2020-06-04 16:43:48 -07:00
|
|
|
this._progress.cleanupWhenAborted(() => this.cancel());
|
2020-06-01 15:48:23 -07:00
|
|
|
this._streamLogs(poll.evaluateHandle(poll => poll.logs));
|
|
|
|
}
|
|
|
|
|
|
|
|
private _streamLogs(logsPromise: Promise<js.JSHandle<types.InjectedScriptLogs>>) {
|
|
|
|
// We continuously get a chunk of logs, stream them to the progress and wait for the next chunk.
|
|
|
|
logsPromise.catch(e => null).then(logs => {
|
2020-06-04 16:43:48 -07:00
|
|
|
if (!logs || !this._poll || !this._progress.isRunning())
|
2020-06-01 15:48:23 -07:00
|
|
|
return;
|
|
|
|
logs.evaluate(logs => logs.current).catch(e => [] as string[]).then(messages => {
|
|
|
|
for (const message of messages)
|
2020-06-16 17:11:19 -07:00
|
|
|
this._progress.logger.info(message);
|
2020-06-01 15:48:23 -07:00
|
|
|
});
|
|
|
|
this._streamLogs(logs.evaluateHandle(logs => logs.next));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async finishHandle(): Promise<js.SmartHandle<T>> {
|
2020-06-06 20:59:06 -07:00
|
|
|
try {
|
|
|
|
const result = await this._poll!.evaluateHandle(poll => poll.result);
|
|
|
|
await this._finishInternal();
|
|
|
|
return result;
|
|
|
|
} finally {
|
2020-06-09 18:57:11 -07:00
|
|
|
await this.cancel();
|
2020-06-06 20:59:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async finish(): Promise<T> {
|
|
|
|
try {
|
|
|
|
const result = await this._poll!.evaluate(poll => poll.result);
|
|
|
|
await this._finishInternal();
|
|
|
|
return result;
|
|
|
|
} finally {
|
2020-06-09 18:57:11 -07:00
|
|
|
await this.cancel();
|
2020-06-06 20:59:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _finishInternal() {
|
|
|
|
if (!this._poll)
|
|
|
|
return;
|
|
|
|
// Retrieve all the logs before continuing.
|
|
|
|
const messages = await this._poll.evaluate(poll => poll.takeLastLogs()).catch(e => [] as string[]);
|
|
|
|
for (const message of messages)
|
2020-06-16 17:11:19 -07:00
|
|
|
this._progress.logger.info(message);
|
2020-06-06 20:59:06 -07:00
|
|
|
}
|
|
|
|
|
2020-06-09 18:57:11 -07:00
|
|
|
async cancel() {
|
2020-06-01 15:48:23 -07:00
|
|
|
if (!this._poll)
|
|
|
|
return;
|
|
|
|
const copy = this._poll;
|
|
|
|
this._poll = null;
|
2020-06-09 18:57:11 -07:00
|
|
|
await copy.evaluate(p => p.cancel()).catch(e => {});
|
|
|
|
copy.dispose();
|
2020-06-01 15:48:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 10:25:28 -07:00
|
|
|
export function toFileTransferPayload(files: types.FilePayload[]): types.FileTransferPayload[] {
|
|
|
|
return files.map(file => ({
|
|
|
|
name: file.name,
|
|
|
|
type: file.mimeType,
|
|
|
|
data: file.buffer.toString('base64')
|
2020-01-03 12:59:06 -08:00
|
|
|
}));
|
2020-04-16 10:25:28 -07:00
|
|
|
}
|
2020-04-18 18:29:31 -07:00
|
|
|
|
2020-06-12 14:59:26 -07:00
|
|
|
function throwIfError<T>(injectedResult: types.InjectedScriptResult<T>): T {
|
|
|
|
if (injectedResult.error)
|
2020-04-18 18:29:31 -07:00
|
|
|
throw new Error(injectedResult.error);
|
2020-06-12 14:59:26 -07:00
|
|
|
return injectedResult.value!;
|
|
|
|
}
|
|
|
|
|
|
|
|
function throwIfNotConnected<T>(result: 'notconnected' | T): T {
|
|
|
|
if (result === 'notconnected')
|
|
|
|
throw new Error('Element is not attached to the DOM');
|
|
|
|
return result;
|
2020-04-18 18:29:31 -07:00
|
|
|
}
|
2020-04-29 11:05:23 -07:00
|
|
|
|
|
|
|
function roundPoint(point: types.Point): types.Point {
|
|
|
|
return {
|
|
|
|
x: (point.x * 100 | 0) / 100,
|
|
|
|
y: (point.y * 100 | 0) / 100,
|
|
|
|
};
|
|
|
|
}
|