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
|
|
|
|
2021-09-22 17:17:49 -07:00
|
|
|
import * as mime from 'mime';
|
|
|
|
import * as injectedScriptSource from '../generated/injectedScriptSource';
|
2021-05-19 01:30:20 +00:00
|
|
|
import * as channels from '../protocol/channels';
|
2021-09-22 17:17:49 -07:00
|
|
|
import { isSessionClosedError } from './common/protocolError';
|
2019-11-27 16:02:31 -08:00
|
|
|
import * as frames from './frames';
|
2021-09-23 16:46:46 -07:00
|
|
|
import type { InjectedScript, InjectedScriptPoll, LogEntry } from './injected/injectedScript';
|
2021-09-22 17:17:49 -07:00
|
|
|
import { CallMetadata } from './instrumentation';
|
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';
|
2021-09-22 17:17:49 -07:00
|
|
|
import { Progress, ProgressController } from './progress';
|
2020-09-02 16:15:43 -07:00
|
|
|
import { SelectorInfo } from './selectors';
|
2020-04-01 14:42:47 -07:00
|
|
|
import * as types from './types';
|
2019-11-27 16:02:31 -08:00
|
|
|
|
2021-05-19 01:30:20 +00:00
|
|
|
type SetInputFilesFiles = channels.ElementHandleSetInputFilesParams['files'];
|
|
|
|
|
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>;
|
2020-12-02 13:43:16 -08:00
|
|
|
readonly world: types.World | null;
|
2019-11-28 12:50:52 -08:00
|
|
|
|
2020-12-02 13:43:16 -08:00
|
|
|
constructor(delegate: js.ExecutionContextDelegate, frame: frames.Frame, world: types.World|null) {
|
2021-02-09 09:00:00 -08:00
|
|
|
super(frame, delegate);
|
2019-12-19 15:19:22 -08:00
|
|
|
this.frame = frame;
|
2020-12-02 13:43:16 -08:00
|
|
|
this.world = world;
|
2019-12-02 13:12:28 -08:00
|
|
|
}
|
|
|
|
|
2021-08-25 10:11:18 -04:00
|
|
|
override async waitForSignalsCreatedBy<T>(action: () => Promise<T>): Promise<T> {
|
2021-03-18 03:03:21 +08:00
|
|
|
return this.frame._page._frameManager.waitForSignalsCreatedBy(null, false, action);
|
|
|
|
}
|
|
|
|
|
2021-08-25 10:11:18 -04:00
|
|
|
override 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
|
|
|
|
2021-07-09 16:19:42 +02:00
|
|
|
async evaluate<Arg, R>(pageFunction: js.Func1<Arg, R>, arg?: Arg): Promise<R> {
|
|
|
|
return js.evaluate(this, true /* returnByValue */, pageFunction, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
async evaluateHandle<Arg, R>(pageFunction: js.Func1<Arg, R>, arg?: Arg): Promise<js.SmartHandle<R>> {
|
|
|
|
return js.evaluate(this, false /* returnByValue */, pageFunction, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
async evaluateExpression(expression: string, isFunction: boolean | undefined, arg?: any): Promise<any> {
|
|
|
|
return js.evaluateExpression(this, true /* returnByValue */, expression, isFunction, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
async evaluateAndWaitForSignals<Arg, R>(pageFunction: js.Func1<Arg, R>, arg?: Arg): Promise<R> {
|
|
|
|
return await this.frame._page._frameManager.waitForSignalsCreatedBy(null, false /* noWaitFor */, async () => {
|
|
|
|
return this.evaluate(pageFunction, arg);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async evaluateExpressionAndWaitForSignals(expression: string, isFunction: boolean | undefined, arg?: any): Promise<any> {
|
|
|
|
return await this.frame._page._frameManager.waitForSignalsCreatedBy(null, false /* noWaitFor */, async () => {
|
|
|
|
return this.evaluateExpression(expression, isFunction, arg);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async evaluateExpressionHandleAndWaitForSignals(expression: string, isFunction: boolean | undefined, arg: any): Promise<any> {
|
|
|
|
return await this.frame._page._frameManager.waitForSignalsCreatedBy(null, false /* noWaitFor */, async () => {
|
|
|
|
return js.evaluateExpression(this, false /* returnByValue */, expression, isFunction, arg);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-25 10:11:18 -04:00
|
|
|
override 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[] = [];
|
2020-09-02 16:15:43 -07:00
|
|
|
for (const [name, { source }] of this.frame._page.selectors._engines)
|
2020-05-15 15:21:49 -07:00
|
|
|
custom.push(`{ name: '${name}', engine: (${source}) }`);
|
|
|
|
const source = `
|
2021-01-08 16:15:05 -08:00
|
|
|
(() => {
|
|
|
|
${injectedScriptSource.source}
|
2021-02-10 12:36:26 -08:00
|
|
|
return new pwExport(
|
|
|
|
${this.frame._page._delegate.rafCountForStablePosition()},
|
2021-09-24 20:51:09 -07:00
|
|
|
"${this.frame._page._browserContext._browser.options.name}",
|
2021-02-10 12:36:26 -08:00
|
|
|
[${custom.join(',\n')}]
|
|
|
|
);
|
2021-01-08 16:15:05 -08:00
|
|
|
})();
|
2020-05-15 15:21:49 -07:00
|
|
|
`;
|
2021-08-06 11:37:36 -07:00
|
|
|
this._injectedScriptPromise = this._delegate.rawEvaluateHandle(source).then(objectId => new js.JSHandle(this, 'object', undefined, objectId));
|
2019-11-28 12:50:52 -08:00
|
|
|
}
|
2020-06-11 18:18:33 -07:00
|
|
|
return this._injectedScriptPromise;
|
|
|
|
}
|
|
|
|
|
2021-08-25 10:11:18 -04:00
|
|
|
override async doSlowMo() {
|
2020-08-18 19:13:40 -07:00
|
|
|
return this.frame._page._doSlowMo();
|
|
|
|
}
|
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> {
|
2021-08-03 12:21:07 -04:00
|
|
|
declare readonly _context: FrameExecutionContext;
|
2019-12-12 21:11:52 -08:00
|
|
|
readonly _page: Page;
|
2021-08-03 12:21:07 -04:00
|
|
|
declare readonly _objectId: string;
|
2019-11-27 16:02:31 -08:00
|
|
|
|
2020-05-27 22:19:05 -07:00
|
|
|
constructor(context: FrameExecutionContext, objectId: string) {
|
2021-08-06 11:37:36 -07:00
|
|
|
super(context, 'node', undefined, objectId);
|
2019-12-19 15:19:22 -08:00
|
|
|
this._page = context.frame._page;
|
2020-09-16 15:26:59 -07:00
|
|
|
this._initializePreview().catch(e => {});
|
2020-06-12 11:10:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async _initializePreview() {
|
|
|
|
const utility = await this._context.injectedScript();
|
2020-09-16 15:26:59 -07:00
|
|
|
this._setPreview(await utility.evaluate((injected, e) => 'JSHandle@' + injected.previewNode(e), this));
|
2019-12-18 13:51:45 -08:00
|
|
|
}
|
|
|
|
|
2021-08-25 10:11:18 -04:00
|
|
|
override asElement(): ElementHandle<T> | null {
|
2019-11-27 16:02:31 -08:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-03-18 01:47:07 +08:00
|
|
|
private async _evaluateInMainAndWaitForSignals<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();
|
2021-07-09 16:19:42 +02:00
|
|
|
return main.evaluateAndWaitForSignals(pageFunction, [await main.injectedScript(), this, arg]);
|
2020-04-23 14:58:37 -07:00
|
|
|
}
|
|
|
|
|
2021-09-17 22:18:00 -07:00
|
|
|
async evaluateInUtility<R, Arg>(pageFunction: js.Func1<[js.JSHandle<InjectedScript>, ElementHandle<T>, Arg], R>, arg: Arg): Promise<R | 'error:notconnected'> {
|
|
|
|
try {
|
|
|
|
const utility = await this._context.frame._utilityContext();
|
|
|
|
return await utility.evaluate(pageFunction, [await utility.injectedScript(), this, arg]);
|
|
|
|
} catch (e) {
|
|
|
|
if (js.isJavaScriptErrorInEvaluate(e) || isSessionClosedError(e))
|
|
|
|
throw e;
|
|
|
|
return 'error:notconnected';
|
|
|
|
}
|
2019-12-19 15:19:22 -08:00
|
|
|
}
|
|
|
|
|
2021-09-17 22:18:00 -07:00
|
|
|
async evaluateHandleInUtility<R, Arg>(pageFunction: js.Func1<[js.JSHandle<InjectedScript>, ElementHandle<T>, Arg], R>, arg: Arg): Promise<js.JSHandle<R> | 'error:notconnected'> {
|
|
|
|
try {
|
|
|
|
const utility = await this._context.frame._utilityContext();
|
|
|
|
return await utility.evaluateHandle(pageFunction, [await utility.injectedScript(), this, arg]);
|
|
|
|
} catch (e) {
|
|
|
|
if (js.isJavaScriptErrorInEvaluate(e) || isSessionClosedError(e))
|
|
|
|
throw e;
|
|
|
|
return 'error:notconnected';
|
|
|
|
}
|
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> {
|
2021-09-17 22:18:00 -07:00
|
|
|
const isFrameElement = throwRetargetableDOMError(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> {
|
2021-09-24 20:51:09 -07:00
|
|
|
return throwRetargetableDOMError(await this.evaluateInUtility(([injected, node, name]) => {
|
2020-04-09 16:49:23 -07:00
|
|
|
if (node.nodeType !== Node.ELEMENT_NODE)
|
2021-09-24 20:51:09 -07:00
|
|
|
throw injected.createStacklessError('Node is not an element');
|
2020-04-09 16:49:23 -07:00
|
|
|
const element = node as unknown as Element;
|
2020-06-24 15:12:17 -07:00
|
|
|
return { value: element.getAttribute(name) };
|
2021-09-24 20:51:09 -07:00
|
|
|
}, name)).value;
|
2020-04-09 16:49:23 -07:00
|
|
|
}
|
|
|
|
|
2021-06-23 22:19:20 -07:00
|
|
|
async inputValue(): Promise<string> {
|
2021-09-24 20:51:09 -07:00
|
|
|
return throwRetargetableDOMError(await this.evaluateInUtility(([injected, node]) => {
|
2021-08-03 10:22:40 -05:00
|
|
|
if (node.nodeType !== Node.ELEMENT_NODE || (node.nodeName !== 'INPUT' && node.nodeName !== 'TEXTAREA' && node.nodeName !== 'SELECT'))
|
2021-09-24 20:51:09 -07:00
|
|
|
throw injected.createStacklessError('Node is not an <input>, <textarea> or <select> element');
|
2021-06-23 22:19:20 -07:00
|
|
|
const element = node as unknown as (HTMLInputElement | HTMLTextAreaElement);
|
|
|
|
return { value: element.value };
|
2021-09-24 20:51:09 -07:00
|
|
|
}, undefined)).value;
|
2021-06-23 22:19:20 -07:00
|
|
|
}
|
|
|
|
|
2020-04-09 16:49:23 -07:00
|
|
|
async textContent(): Promise<string | null> {
|
2021-09-17 22:18:00 -07:00
|
|
|
return throwRetargetableDOMError(await this.evaluateInUtility(([injected, node]) => {
|
|
|
|
return { value: node.textContent };
|
|
|
|
}, undefined)).value;
|
2020-04-09 16:49:23 -07:00
|
|
|
}
|
|
|
|
|
2020-05-18 17:58:23 -07:00
|
|
|
async innerText(): Promise<string> {
|
2021-09-24 20:51:09 -07:00
|
|
|
return throwRetargetableDOMError(await this.evaluateInUtility(([injected, node]) => {
|
2020-04-09 16:49:23 -07:00
|
|
|
if (node.nodeType !== Node.ELEMENT_NODE)
|
2021-09-24 20:51:09 -07:00
|
|
|
throw injected.createStacklessError('Node is not an element');
|
2021-09-23 11:55:44 -04:00
|
|
|
if ((node as unknown as Element).namespaceURI !== 'http://www.w3.org/1999/xhtml')
|
2021-09-24 20:51:09 -07:00
|
|
|
throw injected.createStacklessError('Node is not an HTMLElement');
|
2020-04-09 16:49:23 -07:00
|
|
|
const element = node as unknown as HTMLElement;
|
2020-06-24 15:12:17 -07:00
|
|
|
return { value: element.innerText };
|
2021-09-24 20:51:09 -07:00
|
|
|
}, undefined)).value;
|
2020-04-09 16:49:23 -07:00
|
|
|
}
|
|
|
|
|
2020-05-18 17:58:23 -07:00
|
|
|
async innerHTML(): Promise<string> {
|
2021-09-24 20:51:09 -07:00
|
|
|
return throwRetargetableDOMError(await this.evaluateInUtility(([injected, node]) => {
|
2020-04-09 16:49:23 -07:00
|
|
|
if (node.nodeType !== Node.ELEMENT_NODE)
|
2021-09-24 20:51:09 -07:00
|
|
|
throw injected.createStacklessError('Node is not an element');
|
2020-04-09 16:49:23 -07:00
|
|
|
const element = node as unknown as Element;
|
2020-06-24 15:12:17 -07:00
|
|
|
return { value: element.innerHTML };
|
2021-09-24 20:51:09 -07:00
|
|
|
}, undefined)).value;
|
2020-04-09 16:49:23 -07:00
|
|
|
}
|
|
|
|
|
2020-04-23 14:58:37 -07:00
|
|
|
async dispatchEvent(type: string, eventInit: Object = {}) {
|
2021-03-18 01:47:07 +08:00
|
|
|
await this._evaluateInMainAndWaitForSignals(([injected, node, { type, eventInit }]) =>
|
2020-04-23 14:58:37 -07:00
|
|
|
injected.dispatchEvent(node, type, eventInit), { type, eventInit });
|
2020-08-18 19:13:40 -07:00
|
|
|
await this._page._doSlowMo();
|
2020-04-23 14:58:37 -07:00
|
|
|
}
|
|
|
|
|
2020-06-24 15:12:17 -07:00
|
|
|
async _scrollRectIntoViewIfNeeded(rect?: types.Rect): Promise<'error:notvisible' | 'error: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-24 10:16:54 -07:00
|
|
|
async _waitAndScrollIntoViewIfNeeded(progress: Progress): Promise<void> {
|
|
|
|
while (progress.isRunning()) {
|
2021-06-24 08:18:09 -07:00
|
|
|
assertDone(throwRetargetableDOMError(await this._waitForDisplayedAtStablePosition(progress, false /* force */, false /* waitForEnabled */)));
|
2020-06-24 10:16:54 -07:00
|
|
|
|
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
2020-06-24 15:12:17 -07:00
|
|
|
const result = throwRetargetableDOMError(await this._scrollRectIntoViewIfNeeded());
|
|
|
|
if (result === 'error:notvisible')
|
2020-06-24 10:16:54 -07:00
|
|
|
continue;
|
2020-06-24 15:12:17 -07:00
|
|
|
assertDone(result);
|
2020-06-24 10:16:54 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
async scrollIntoViewIfNeeded(metadata: CallMetadata, options: types.TimeoutOptions = {}) {
|
|
|
|
const controller = new ProgressController(metadata, this);
|
|
|
|
return controller.run(
|
2020-06-24 10:16:54 -07:00
|
|
|
progress => this._waitAndScrollIntoViewIfNeeded(progress),
|
2020-08-14 18:25:32 -07:00
|
|
|
this._page._timeoutSettings.timeout(options));
|
2020-06-23 13:02:31 -07:00
|
|
|
}
|
|
|
|
|
2020-06-24 15:12:17 -07:00
|
|
|
private async _clickablePoint(): Promise<types.Point | 'error:notvisible' | 'error: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),
|
2021-09-27 18:58:08 +02:00
|
|
|
this._page.mainFrame()._utilityContext().then(utility => utility.evaluate(() => ({ 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-24 15:12:17 -07:00
|
|
|
return 'error:notvisible';
|
2019-12-05 09:54:50 -08:00
|
|
|
|
2020-08-20 16:49:19 -07:00
|
|
|
// Allow 1x1 elements. Compensate for rounding errors by comparing with 0.99 instead.
|
|
|
|
const filtered = quads.map(quad => intersectQuadWithViewport(quad)).filter(quad => computeQuadArea(quad) > 0.99);
|
2019-12-05 09:54:50 -08:00
|
|
|
if (!filtered.length)
|
2020-06-24 15:12:17 -07:00
|
|
|
return 'error: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;
|
|
|
|
}
|
2020-08-20 16:49:19 -07:00
|
|
|
compensateHalfIntegerRoundingError(result);
|
2019-12-05 09:54:50 -08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-17 22:18:00 -07:00
|
|
|
private async _offsetPoint(offset: types.Point): Promise<types.Point | 'error:notvisible' | 'error:notconnected'> {
|
2019-12-05 09:54:50 -08:00
|
|
|
const [box, border] = await Promise.all([
|
|
|
|
this.boundingBox(),
|
2021-03-18 01:47:07 +08: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-24 15:12:17 -07:00
|
|
|
return 'error:notvisible';
|
2021-09-17 22:18:00 -07:00
|
|
|
if (border === 'error:notconnected')
|
|
|
|
return border;
|
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-08-14 13:18:32 -07:00
|
|
|
async _retryPointerAction(progress: Progress, actionName: string, waitForEnabled: boolean, action: (point: types.Point) => Promise<void>,
|
|
|
|
options: types.PointerActionOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): Promise<'error:notconnected' | 'done'> {
|
2020-09-29 10:28:19 -07:00
|
|
|
let retry = 0;
|
|
|
|
// We progressively wait longer between retries, up to 500ms.
|
2020-12-16 15:29:42 -08:00
|
|
|
const waitTime = [0, 20, 100, 100, 500];
|
|
|
|
|
|
|
|
// By default, we scroll with protocol method to reveal the action point.
|
|
|
|
// However, that might not work to scroll from under position:sticky elements
|
|
|
|
// that overlay the target element. To fight this, we cycle through different
|
|
|
|
// scroll alignments. This works in most scenarios.
|
|
|
|
const scrollOptions: (ScrollIntoViewOptions | undefined)[] = [
|
|
|
|
undefined,
|
|
|
|
{ block: 'end', inline: 'end' },
|
|
|
|
{ block: 'center', inline: 'center' },
|
|
|
|
{ block: 'start', inline: 'start' },
|
|
|
|
];
|
|
|
|
|
2020-06-04 16:43:48 -07:00
|
|
|
while (progress.isRunning()) {
|
2020-09-29 10:28:19 -07:00
|
|
|
if (retry) {
|
2021-04-21 12:22:19 -07:00
|
|
|
progress.log(`retrying ${actionName} action${options.trial ? ' (trial run)' : ''}, attempt #${retry}`);
|
2020-09-29 10:28:19 -07:00
|
|
|
const timeout = waitTime[Math.min(retry - 1, waitTime.length - 1)];
|
|
|
|
if (timeout) {
|
|
|
|
progress.log(` waiting ${timeout}ms`);
|
2021-09-17 22:18:00 -07:00
|
|
|
const result = await this.evaluateInUtility(([injected, node, timeout]) => new Promise<void>(f => setTimeout(f, timeout)), timeout);
|
|
|
|
if (result === 'error:notconnected')
|
|
|
|
return result;
|
2020-09-29 10:28:19 -07:00
|
|
|
}
|
|
|
|
} else {
|
2021-04-21 12:22:19 -07:00
|
|
|
progress.log(`attempting ${actionName} action${options.trial ? ' (trial run)' : ''}`);
|
2020-09-29 10:28:19 -07:00
|
|
|
}
|
2020-12-16 15:29:42 -08:00
|
|
|
const forceScrollOptions = scrollOptions[retry % scrollOptions.length];
|
|
|
|
const result = await this._performPointerAction(progress, actionName, waitForEnabled, action, forceScrollOptions, options);
|
2020-09-29 10:28:19 -07:00
|
|
|
++retry;
|
2020-06-24 15:12:17 -07:00
|
|
|
if (result === 'error:notvisible') {
|
2020-06-12 14:59:26 -07:00
|
|
|
if (options.force)
|
|
|
|
throw new Error('Element is not visible');
|
2020-08-17 14:12:31 -07:00
|
|
|
progress.log(' element is not visible');
|
2020-06-12 14:59:26 -07:00
|
|
|
continue;
|
|
|
|
}
|
2020-06-24 15:12:17 -07:00
|
|
|
if (result === 'error:notinviewport') {
|
2020-06-12 14:59:26 -07:00
|
|
|
if (options.force)
|
|
|
|
throw new Error('Element is outside of the viewport');
|
2020-08-17 14:12:31 -07:00
|
|
|
progress.log(' element is outside of the viewport');
|
2020-06-12 14:59:26 -07:00
|
|
|
continue;
|
|
|
|
}
|
2020-08-14 14:48:36 -07:00
|
|
|
if (typeof result === 'object' && 'hitTargetDescription' in result) {
|
2020-06-12 14:59:26 -07:00
|
|
|
if (options.force)
|
2020-08-14 14:48:36 -07:00
|
|
|
throw new Error(`Element does not receive pointer events, ${result.hitTargetDescription} intercepts them`);
|
2020-08-17 14:12:31 -07:00
|
|
|
progress.log(` ${result.hitTargetDescription} intercepts pointer events`);
|
2020-06-12 14:59:26 -07:00
|
|
|
continue;
|
|
|
|
}
|
2020-06-24 15:12:17 -07:00
|
|
|
return result;
|
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-12-16 15:29:42 -08:00
|
|
|
async _performPointerAction(progress: Progress, actionName: string, waitForEnabled: boolean, action: (point: types.Point) => Promise<void>, forceScrollOptions: ScrollIntoViewOptions | undefined, options: types.PointerActionOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): Promise<'error:notvisible' | 'error:notconnected' | 'error:notinviewport' | { hitTargetDescription: string } | 'done'> {
|
2020-04-29 11:05:23 -07:00
|
|
|
const { force = false, position } = options;
|
2020-06-26 16:31:51 -07:00
|
|
|
if ((options as any).__testHookBeforeStable)
|
|
|
|
await (options as any).__testHookBeforeStable();
|
2021-06-24 08:18:09 -07:00
|
|
|
const result = await this._waitForDisplayedAtStablePosition(progress, force, waitForEnabled);
|
|
|
|
if (result !== 'done')
|
|
|
|
return result;
|
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-08-17 14:12:31 -07:00
|
|
|
progress.log(' scrolling into view if needed');
|
2020-06-09 18:57:11 -07:00
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
2020-12-16 15:29:42 -08:00
|
|
|
if (forceScrollOptions) {
|
2021-09-17 22:18:00 -07:00
|
|
|
const scrolled = await this.evaluateInUtility(([injected, node, options]) => {
|
2020-12-16 15:29:42 -08:00
|
|
|
if (node.nodeType === 1 /* Node.ELEMENT_NODE */)
|
|
|
|
(node as Node as Element).scrollIntoView(options);
|
|
|
|
}, forceScrollOptions);
|
2021-09-17 22:18:00 -07:00
|
|
|
if (scrolled === 'error:notconnected')
|
|
|
|
return scrolled;
|
2020-12-16 15:29:42 -08:00
|
|
|
} else {
|
|
|
|
const scrolled = await this._scrollRectIntoViewIfNeeded(position ? { x: position.x, y: position.y, width: 0, height: 0 } : undefined);
|
|
|
|
if (scrolled !== 'done')
|
|
|
|
return scrolled;
|
|
|
|
}
|
2020-08-17 14:12:31 -07:00
|
|
|
progress.log(' done scrolling');
|
2020-06-01 11:14:16 -07:00
|
|
|
|
|
|
|
const maybePoint = position ? await this._offsetPoint(position) : await this._clickablePoint();
|
2020-06-24 15:12:17 -07:00
|
|
|
if (typeof maybePoint === 'string')
|
|
|
|
return maybePoint;
|
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-08-17 14:12:31 -07:00
|
|
|
progress.log(` 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);
|
2020-06-24 15:12:17 -07:00
|
|
|
if (hitTargetResult !== 'done')
|
|
|
|
return hitTargetResult;
|
2020-08-17 14:12:31 -07:00
|
|
|
progress.log(` element does receive pointer events`);
|
2020-04-29 11:05:23 -07:00
|
|
|
}
|
2020-06-01 11:14:16 -07:00
|
|
|
|
2021-03-17 03:06:12 +08:00
|
|
|
progress.metadata.point = point;
|
2021-04-21 12:22:19 -07:00
|
|
|
if (options.trial) {
|
|
|
|
progress.log(` trial ${actionName} has finished`);
|
|
|
|
return 'done';
|
|
|
|
}
|
2021-03-17 03:06:12 +08:00
|
|
|
|
2021-04-21 12:22:19 -07:00
|
|
|
await progress.beforeInputAction(this);
|
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-08-17 14:12:31 -07:00
|
|
|
progress.log(` performing ${actionName} action`);
|
2020-06-01 11:14:16 -07:00
|
|
|
await action(point);
|
2020-08-17 14:12:31 -07:00
|
|
|
progress.log(` ${actionName} action done`);
|
|
|
|
progress.log(' 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-08-17 14:12:31 -07:00
|
|
|
progress.log(' navigations have finished');
|
2020-06-01 11:14:16 -07:00
|
|
|
|
|
|
|
return 'done';
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
async hover(metadata: CallMetadata, options: types.PointerActionOptions & types.PointerActionWaitOptions): Promise<void> {
|
|
|
|
const controller = new ProgressController(metadata, this);
|
2020-09-17 09:32:54 -07:00
|
|
|
return controller.run(async progress => {
|
|
|
|
const result = await this._hover(progress, options);
|
|
|
|
return assertDone(throwRetargetableDOMError(result));
|
|
|
|
}, this._page._timeoutSettings.timeout(options));
|
2020-06-03 09:14:53 -07:00
|
|
|
}
|
|
|
|
|
2020-06-24 15:12:17 -07:00
|
|
|
_hover(progress: Progress, options: types.PointerActionOptions & types.PointerActionWaitOptions): Promise<'error:notconnected' | 'done'> {
|
2020-08-14 13:18:32 -07:00
|
|
|
return this._retryPointerAction(progress, 'hover', false /* waitForEnabled */, point => this._page.mouse.move(point.x, point.y), options);
|
2020-06-03 09:14:53 -07:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
async click(metadata: CallMetadata, options: types.MouseClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions = {}): Promise<void> {
|
|
|
|
const controller = new ProgressController(metadata, this);
|
2020-09-17 09:32:54 -07:00
|
|
|
return controller.run(async progress => {
|
|
|
|
const result = await this._click(progress, options);
|
|
|
|
return assertDone(throwRetargetableDOMError(result));
|
|
|
|
}, this._page._timeoutSettings.timeout(options));
|
2020-06-03 09:14:53 -07:00
|
|
|
}
|
|
|
|
|
2020-06-24 15:12:17 -07:00
|
|
|
_click(progress: Progress, options: types.MouseClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): Promise<'error:notconnected' | 'done'> {
|
2020-08-14 13:18:32 -07:00
|
|
|
return this._retryPointerAction(progress, 'click', true /* waitForEnabled */, point => this._page.mouse.click(point.x, point.y, options), options);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
async dblclick(metadata: CallMetadata, options: types.MouseMultiClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): Promise<void> {
|
|
|
|
const controller = new ProgressController(metadata, this);
|
2020-09-17 09:32:54 -07:00
|
|
|
return controller.run(async progress => {
|
|
|
|
const result = await this._dblclick(progress, options);
|
|
|
|
return assertDone(throwRetargetableDOMError(result));
|
|
|
|
}, this._page._timeoutSettings.timeout(options));
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2020-06-24 15:12:17 -07:00
|
|
|
_dblclick(progress: Progress, options: types.MouseMultiClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): Promise<'error:notconnected' | 'done'> {
|
2020-08-14 13:18:32 -07:00
|
|
|
return this._retryPointerAction(progress, 'dblclick', true /* waitForEnabled */, point => this._page.mouse.dblclick(point.x, point.y, options), options);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
async tap(metadata: CallMetadata, options: types.PointerActionWaitOptions & types.NavigatingActionWaitOptions = {}): Promise<void> {
|
|
|
|
const controller = new ProgressController(metadata, this);
|
2020-10-19 10:07:33 -07:00
|
|
|
return controller.run(async progress => {
|
|
|
|
const result = await this._tap(progress, options);
|
|
|
|
return assertDone(throwRetargetableDOMError(result));
|
|
|
|
}, this._page._timeoutSettings.timeout(options));
|
|
|
|
}
|
|
|
|
|
|
|
|
_tap(progress: Progress, options: types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): Promise<'error:notconnected' | 'done'> {
|
|
|
|
return this._retryPointerAction(progress, 'tap', true /* waitForEnabled */, point => this._page.touchscreen.tap(point.x, point.y), options);
|
|
|
|
}
|
|
|
|
|
2021-06-24 08:18:09 -07:00
|
|
|
async selectOption(metadata: CallMetadata, elements: ElementHandle[], values: types.SelectOption[], options: types.NavigatingActionWaitOptions & types.ForceOptions): Promise<string[]> {
|
2021-02-09 14:44:48 -08:00
|
|
|
const controller = new ProgressController(metadata, this);
|
2020-09-17 09:32:54 -07:00
|
|
|
return controller.run(async progress => {
|
|
|
|
const result = await this._selectOption(progress, elements, values, options);
|
|
|
|
return throwRetargetableDOMError(result);
|
|
|
|
}, this._page._timeoutSettings.timeout(options));
|
2020-06-03 11:23:24 -07:00
|
|
|
}
|
|
|
|
|
2021-06-24 08:18:09 -07:00
|
|
|
async _selectOption(progress: Progress, elements: ElementHandle[], values: types.SelectOption[], options: types.NavigatingActionWaitOptions & types.ForceOptions): Promise<string[] | 'error:notconnected'> {
|
2021-02-10 12:36:26 -08:00
|
|
|
const optionsToSelect = [...elements, ...values];
|
2021-03-17 03:06:12 +08:00
|
|
|
await progress.beforeInputAction(this);
|
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.
|
2021-01-19 11:27:05 -08:00
|
|
|
progress.log(' selecting specified option(s)');
|
2021-06-24 08:18:09 -07:00
|
|
|
const poll = await this.evaluateHandleInUtility(([injected, node, { optionsToSelect, force }]) => {
|
|
|
|
return injected.waitForElementStatesAndPerformAction(node, ['visible', 'enabled'], force, injected.selectOptions.bind(injected, optionsToSelect));
|
|
|
|
}, { optionsToSelect, force: options.force });
|
2021-09-17 22:18:00 -07:00
|
|
|
if (poll === 'error:notconnected')
|
|
|
|
return poll;
|
2021-01-19 11:27:05 -08:00
|
|
|
const pollHandler = new InjectedScriptPollHandler(progress, poll);
|
2021-10-20 12:01:05 -08:00
|
|
|
const result = await pollHandler.finishMaybeNotConnected();
|
2020-08-18 19:13:40 -07:00
|
|
|
await this._page._doSlowMo();
|
2021-01-19 11:27:05 -08:00
|
|
|
return result;
|
2020-06-03 11:23:24 -07:00
|
|
|
});
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2021-06-24 08:18:09 -07:00
|
|
|
async fill(metadata: CallMetadata, value: string, options: types.NavigatingActionWaitOptions & types.ForceOptions = {}): Promise<void> {
|
2021-02-09 14:44:48 -08:00
|
|
|
const controller = new ProgressController(metadata, this);
|
2020-09-17 09:32:54 -07:00
|
|
|
return controller.run(async progress => {
|
|
|
|
const result = await this._fill(progress, value, options);
|
|
|
|
assertDone(throwRetargetableDOMError(result));
|
|
|
|
}, this._page._timeoutSettings.timeout(options));
|
2020-06-03 09:14:53 -07:00
|
|
|
}
|
|
|
|
|
2021-06-24 08:18:09 -07:00
|
|
|
async _fill(progress: Progress, value: string, options: types.NavigatingActionWaitOptions & types.ForceOptions): Promise<'error:notconnected' | 'done'> {
|
2020-08-17 14:12:31 -07:00
|
|
|
progress.log(`elementHandle.fill("${value}")`);
|
2021-03-17 03:06:12 +08:00
|
|
|
await progress.beforeInputAction(this);
|
2020-06-12 14:59:26 -07:00
|
|
|
return this._page._frameManager.waitForSignalsCreatedBy(progress, options.noWaitAfter, async () => {
|
2020-08-17 14:12:31 -07:00
|
|
|
progress.log(' waiting for element to be visible, enabled and editable');
|
2021-06-24 08:18:09 -07:00
|
|
|
const poll = await this.evaluateHandleInUtility(([injected, node, { value, force }]) => {
|
|
|
|
return injected.waitForElementStatesAndPerformAction(node, ['visible', 'enabled', 'editable'], force, injected.fill.bind(injected, value));
|
|
|
|
}, { value, force: options.force });
|
2021-09-17 22:18:00 -07:00
|
|
|
if (poll === 'error:notconnected')
|
|
|
|
return poll;
|
2020-06-06 20:59:06 -07:00
|
|
|
const pollHandler = new InjectedScriptPollHandler(progress, poll);
|
2021-10-20 12:01:05 -08:00
|
|
|
const filled = await pollHandler.finishMaybeNotConnected();
|
2020-06-09 18:57:11 -07:00
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
2020-06-24 15:12:17 -07:00
|
|
|
if (filled === 'error:notconnected')
|
|
|
|
return filled;
|
2020-08-17 14:12:31 -07:00
|
|
|
progress.log(' element is visible, enabled and editable');
|
2020-06-12 14:59:26 -07:00
|
|
|
if (filled === 'needsinput') {
|
2020-06-24 15:12:17 -07:00
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
2020-06-03 09:14:53 -07:00
|
|
|
if (value)
|
|
|
|
await this._page.keyboard.insertText(value);
|
|
|
|
else
|
|
|
|
await this._page.keyboard.press('Delete');
|
2020-06-24 15:12:17 -07:00
|
|
|
} else {
|
|
|
|
assertDone(filled);
|
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
|
|
|
}
|
|
|
|
|
2021-06-24 08:18:09 -07:00
|
|
|
async selectText(metadata: CallMetadata, options: types.TimeoutOptions & types.ForceOptions = {}): Promise<void> {
|
2021-02-09 14:44:48 -08:00
|
|
|
const controller = new ProgressController(metadata, this);
|
|
|
|
return controller.run(async progress => {
|
2020-06-09 18:57:11 -07:00
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
2021-06-24 08:18:09 -07:00
|
|
|
const poll = await this.evaluateHandleInUtility(([injected, node, force]) => {
|
|
|
|
return injected.waitForElementStatesAndPerformAction(node, ['visible'], force, injected.selectText.bind(injected));
|
|
|
|
}, options.force);
|
2021-09-17 22:18:00 -07:00
|
|
|
const pollHandler = new InjectedScriptPollHandler(progress, throwRetargetableDOMError(poll));
|
2021-10-20 12:01:05 -08:00
|
|
|
const result = await pollHandler.finishMaybeNotConnected();
|
2020-06-24 15:12:17 -07:00
|
|
|
assertDone(throwRetargetableDOMError(result));
|
2020-08-14 18:25:32 -07:00
|
|
|
}, this._page._timeoutSettings.timeout(options));
|
2020-04-14 17:09:26 -07:00
|
|
|
}
|
|
|
|
|
2021-05-19 01:30:20 +00:00
|
|
|
async setInputFiles(metadata: CallMetadata, files: SetInputFilesFiles, options: types.NavigatingActionWaitOptions) {
|
2021-02-09 14:44:48 -08:00
|
|
|
const controller = new ProgressController(metadata, this);
|
2020-09-17 09:32:54 -07:00
|
|
|
return controller.run(async progress => {
|
|
|
|
const result = await this._setInputFiles(progress, files, options);
|
|
|
|
return assertDone(throwRetargetableDOMError(result));
|
|
|
|
}, this._page._timeoutSettings.timeout(options));
|
2020-06-03 11:23:24 -07:00
|
|
|
}
|
|
|
|
|
2021-05-19 01:30:20 +00:00
|
|
|
async _setInputFiles(progress: Progress, files: SetInputFilesFiles, options: types.NavigatingActionWaitOptions): Promise<'error:notconnected' | 'done'> {
|
|
|
|
for (const payload of files) {
|
|
|
|
if (!payload.mimeType)
|
|
|
|
payload.mimeType = mime.getType(payload.name) || 'application/octet-stream';
|
|
|
|
}
|
2021-09-24 20:51:09 -07:00
|
|
|
const retargeted = await this.evaluateHandleInUtility(([injected, node, multiple]): 'error:notconnected' | Element => {
|
2021-06-28 14:18:01 -07:00
|
|
|
const element = injected.retarget(node, 'follow-label');
|
|
|
|
if (!element)
|
|
|
|
return 'error:notconnected';
|
|
|
|
if (element.tagName !== 'INPUT')
|
2021-09-24 20:51:09 -07:00
|
|
|
throw injected.createStacklessError('Node is not an HTMLInputElement');
|
2021-06-28 14:18:01 -07:00
|
|
|
if (multiple && !(element as HTMLInputElement).multiple)
|
2021-09-24 20:51:09 -07:00
|
|
|
throw injected.createStacklessError('Non-multiple file input can only accept single file');
|
2021-06-28 14:18:01 -07:00
|
|
|
return element;
|
|
|
|
}, files.length > 1);
|
2021-09-17 22:18:00 -07:00
|
|
|
if (retargeted === 'error:notconnected')
|
|
|
|
return retargeted;
|
2021-06-28 14:18:01 -07:00
|
|
|
if (!retargeted._objectId)
|
2021-09-24 20:51:09 -07:00
|
|
|
return retargeted.rawValue() as 'error:notconnected';
|
2021-03-17 03:06:12 +08:00
|
|
|
await progress.beforeInputAction(this);
|
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.
|
2021-06-28 14:18:01 -07:00
|
|
|
await this._page._delegate.setInputFiles(retargeted as ElementHandle<HTMLInputElement>, files as types.FilePayload[]);
|
2020-06-03 11:23:24 -07:00
|
|
|
});
|
2020-08-18 19:13:40 -07:00
|
|
|
await this._page._doSlowMo();
|
2020-06-12 14:59:26 -07:00
|
|
|
return 'done';
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
async focus(metadata: CallMetadata): Promise<void> {
|
|
|
|
const controller = new ProgressController(metadata, this);
|
|
|
|
await controller.run(async progress => {
|
2020-06-24 15:12:17 -07:00
|
|
|
const result = await this._focus(progress);
|
2020-08-18 19:13:40 -07:00
|
|
|
await this._page._doSlowMo();
|
2020-06-24 15:12:17 -07:00
|
|
|
return assertDone(throwRetargetableDOMError(result));
|
2020-08-14 18:25:32 -07:00
|
|
|
}, 0);
|
2020-06-09 16:11:17 -07:00
|
|
|
}
|
|
|
|
|
2020-07-24 09:30:31 -07:00
|
|
|
async _focus(progress: Progress, resetSelectionIfNotFocused?: boolean): Promise<'error:notconnected' | 'done'> {
|
2020-06-09 18:57:11 -07:00
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
2021-09-24 20:51:09 -07:00
|
|
|
return await this.evaluateInUtility(([injected, node, resetSelectionIfNotFocused]) => injected.focusNode(node, resetSelectionIfNotFocused), resetSelectionIfNotFocused);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
async type(metadata: CallMetadata, text: string, options: { delay?: number } & types.NavigatingActionWaitOptions): Promise<void> {
|
|
|
|
const controller = new ProgressController(metadata, this);
|
2020-09-17 09:32:54 -07:00
|
|
|
return controller.run(async progress => {
|
|
|
|
const result = await this._type(progress, text, options);
|
|
|
|
return assertDone(throwRetargetableDOMError(result));
|
|
|
|
}, this._page._timeoutSettings.timeout(options));
|
2020-06-03 11:23:24 -07:00
|
|
|
}
|
|
|
|
|
2020-06-24 15:12:17 -07:00
|
|
|
async _type(progress: Progress, text: string, options: { delay?: number } & types.NavigatingActionWaitOptions): Promise<'error:notconnected' | 'done'> {
|
2020-08-17 14:12:31 -07:00
|
|
|
progress.log(`elementHandle.type("${text}")`);
|
2021-03-17 03:06:12 +08:00
|
|
|
await progress.beforeInputAction(this);
|
2020-06-03 11:23:24 -07:00
|
|
|
return this._page._frameManager.waitForSignalsCreatedBy(progress, options.noWaitAfter, async () => {
|
2020-07-24 09:30:31 -07:00
|
|
|
const result = await this._focus(progress, true /* resetSelectionIfNotFocused */);
|
2020-06-24 15:12:17 -07:00
|
|
|
if (result !== 'done')
|
|
|
|
return result;
|
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');
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
async press(metadata: CallMetadata, key: string, options: { delay?: number } & types.NavigatingActionWaitOptions): Promise<void> {
|
|
|
|
const controller = new ProgressController(metadata, this);
|
2020-09-17 09:32:54 -07:00
|
|
|
return controller.run(async progress => {
|
|
|
|
const result = await this._press(progress, key, options);
|
|
|
|
return assertDone(throwRetargetableDOMError(result));
|
|
|
|
}, this._page._timeoutSettings.timeout(options));
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2020-06-24 15:12:17 -07:00
|
|
|
async _press(progress: Progress, key: string, options: { delay?: number } & types.NavigatingActionWaitOptions): Promise<'error:notconnected' | 'done'> {
|
2020-08-17 14:12:31 -07:00
|
|
|
progress.log(`elementHandle.press("${key}")`);
|
2021-03-17 03:06:12 +08:00
|
|
|
await progress.beforeInputAction(this);
|
2020-06-03 11:23:24 -07:00
|
|
|
return this._page._frameManager.waitForSignalsCreatedBy(progress, options.noWaitAfter, async () => {
|
2020-07-24 09:30:31 -07:00
|
|
|
const result = await this._focus(progress, true /* resetSelectionIfNotFocused */);
|
2020-06-24 15:12:17 -07:00
|
|
|
if (result !== 'done')
|
|
|
|
return result;
|
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
|
|
|
|
2021-04-12 12:41:25 -07:00
|
|
|
async check(metadata: CallMetadata, options: { position?: types.Point } & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions) {
|
2021-02-09 14:44:48 -08:00
|
|
|
const controller = new ProgressController(metadata, this);
|
2020-09-17 09:32:54 -07:00
|
|
|
return controller.run(async progress => {
|
|
|
|
const result = await this._setChecked(progress, true, options);
|
|
|
|
return assertDone(throwRetargetableDOMError(result));
|
|
|
|
}, this._page._timeoutSettings.timeout(options));
|
2020-02-04 14:39:11 -08:00
|
|
|
}
|
|
|
|
|
2021-04-12 12:41:25 -07:00
|
|
|
async uncheck(metadata: CallMetadata, options: { position?: types.Point } & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions) {
|
2021-02-09 14:44:48 -08:00
|
|
|
const controller = new ProgressController(metadata, this);
|
2020-09-17 09:32:54 -07:00
|
|
|
return controller.run(async progress => {
|
|
|
|
const result = await this._setChecked(progress, false, options);
|
|
|
|
return assertDone(throwRetargetableDOMError(result));
|
|
|
|
}, this._page._timeoutSettings.timeout(options));
|
2020-02-04 14:39:11 -08:00
|
|
|
}
|
|
|
|
|
2021-04-12 12:41:25 -07:00
|
|
|
async _setChecked(progress: Progress, state: boolean, options: { position?: types.Point } & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): Promise<'error:notconnected' | 'done'> {
|
2021-02-10 12:36:26 -08:00
|
|
|
const isChecked = async () => {
|
2021-09-23 16:46:46 -07:00
|
|
|
const result = await this.evaluateInUtility(([injected, node]) => injected.elementState(node, 'checked'), {});
|
2021-09-24 20:51:09 -07:00
|
|
|
return throwRetargetableDOMError(result);
|
2021-02-10 12:36:26 -08:00
|
|
|
};
|
|
|
|
if (await isChecked() === state)
|
2020-06-24 15:12:17 -07:00
|
|
|
return 'done';
|
|
|
|
const result = await this._click(progress, options);
|
|
|
|
if (result !== 'done')
|
|
|
|
return result;
|
2021-04-21 12:22:19 -07:00
|
|
|
if (options.trial)
|
|
|
|
return 'done';
|
2021-02-10 12:36:26 -08:00
|
|
|
if (await isChecked() !== state)
|
|
|
|
throw new Error('Clicking the checkbox did not change its state');
|
2020-06-24 15:12:17 -07:00
|
|
|
return 'done';
|
2020-02-04 14:39:11 -08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
async screenshot(metadata: CallMetadata, options: types.ElementScreenshotOptions = {}): Promise<Buffer> {
|
|
|
|
const controller = new ProgressController(metadata, this);
|
|
|
|
return controller.run(
|
2020-06-24 10:16:54 -07:00
|
|
|
progress => this._page._screenshotter.screenshotElement(progress, this, options),
|
2020-08-14 18:25:32 -07:00
|
|
|
this._page._timeoutSettings.timeout(options));
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2021-07-26 22:00:23 -07:00
|
|
|
async querySelector(selector: string, options: types.StrictOptions): Promise<ElementHandle | null> {
|
2021-08-18 12:51:45 -07:00
|
|
|
return this._page.selectors.query(this._context.frame, selector, options, this);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2021-07-26 22:00:23 -07:00
|
|
|
async querySelectorAll(selector: string): Promise<ElementHandle<Element>[]> {
|
2020-09-14 10:38:14 -07:00
|
|
|
return this._page.selectors._queryAll(this._context.frame, selector, this, true /* adoptToMain */);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2021-07-26 22:00:23 -07:00
|
|
|
async evalOnSelectorAndWaitForSignals(selector: string, strict: boolean, expression: string, isFunction: boolean | undefined, arg: any): Promise<any> {
|
2021-08-18 12:51:45 -07:00
|
|
|
const handle = await this._page.selectors.query(this._context.frame, selector, { strict }, this);
|
2020-03-25 14:08:46 -07:00
|
|
|
if (!handle)
|
2019-12-17 14:30:02 -08:00
|
|
|
throw new Error(`Error: failed to find element matching selector "${selector}"`);
|
2021-03-18 03:03:21 +08:00
|
|
|
const result = await handle.evaluateExpressionAndWaitForSignals(expression, isFunction, true, arg);
|
2020-03-25 14:08:46 -07:00
|
|
|
handle.dispose();
|
2019-12-17 14:30:02 -08:00
|
|
|
return result;
|
2019-12-04 13:11:10 -08:00
|
|
|
}
|
|
|
|
|
2021-03-18 03:03:21 +08:00
|
|
|
async evalOnSelectorAllAndWaitForSignals(selector: string, expression: string, isFunction: boolean | undefined, arg: any): Promise<any> {
|
2020-09-02 16:15:43 -07:00
|
|
|
const arrayHandle = await this._page.selectors._queryArray(this._context.frame, selector, this);
|
2021-03-18 03:03:21 +08:00
|
|
|
const result = await arrayHandle.evaluateExpressionAndWaitForSignals(expression, isFunction, true, 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
|
|
|
}
|
|
|
|
|
2021-01-08 12:27:54 -08:00
|
|
|
async isVisible(): Promise<boolean> {
|
2021-09-23 16:46:46 -07:00
|
|
|
const result = await this.evaluateInUtility(([injected, node]) => injected.elementState(node, 'visible'), {});
|
2021-08-05 21:10:33 +02:00
|
|
|
if (result === 'error:notconnected')
|
|
|
|
return false;
|
2021-09-24 20:51:09 -07:00
|
|
|
return result;
|
2021-01-08 12:27:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async isHidden(): Promise<boolean> {
|
2021-09-23 16:46:46 -07:00
|
|
|
const result = await this.evaluateInUtility(([injected, node]) => injected.elementState(node, 'hidden'), {});
|
2021-09-24 20:51:09 -07:00
|
|
|
return throwRetargetableDOMError(result);
|
2021-01-08 12:27:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async isEnabled(): Promise<boolean> {
|
2021-09-23 16:46:46 -07:00
|
|
|
const result = await this.evaluateInUtility(([injected, node]) => injected.elementState(node, 'enabled'), {});
|
2021-09-24 20:51:09 -07:00
|
|
|
return throwRetargetableDOMError(result);
|
2021-01-08 12:27:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async isDisabled(): Promise<boolean> {
|
2021-09-23 16:46:46 -07:00
|
|
|
const result = await this.evaluateInUtility(([injected, node]) => injected.elementState(node, 'disabled'), {});
|
2021-09-24 20:51:09 -07:00
|
|
|
return throwRetargetableDOMError(result);
|
2021-01-08 12:27:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async isEditable(): Promise<boolean> {
|
2021-09-23 16:46:46 -07:00
|
|
|
const result = await this.evaluateInUtility(([injected, node]) => injected.elementState(node, 'editable'), {});
|
2021-09-24 20:51:09 -07:00
|
|
|
return throwRetargetableDOMError(result);
|
2021-01-08 12:27:54 -08:00
|
|
|
}
|
|
|
|
|
2021-01-08 17:36:17 -08:00
|
|
|
async isChecked(): Promise<boolean> {
|
2021-09-23 16:46:46 -07:00
|
|
|
const result = await this.evaluateInUtility(([injected, node]) => injected.elementState(node, 'checked'), {});
|
2021-09-24 20:51:09 -07:00
|
|
|
return throwRetargetableDOMError(result);
|
2021-01-08 17:36:17 -08:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
async waitForElementState(metadata: CallMetadata, state: 'visible' | 'hidden' | 'stable' | 'enabled' | 'disabled' | 'editable', options: types.TimeoutOptions = {}): Promise<void> {
|
|
|
|
const controller = new ProgressController(metadata, this);
|
|
|
|
return controller.run(async progress => {
|
2020-08-19 17:20:10 -07:00
|
|
|
progress.log(` waiting for element to be ${state}`);
|
2021-03-18 01:47:07 +08:00
|
|
|
const poll = await this.evaluateHandleInUtility(([injected, node, state]) => {
|
2021-06-24 08:18:09 -07:00
|
|
|
return injected.waitForElementStatesAndPerformAction(node, [state], false, () => 'done' as const);
|
2021-02-10 12:36:26 -08:00
|
|
|
}, state);
|
2021-09-17 22:18:00 -07:00
|
|
|
const pollHandler = new InjectedScriptPollHandler(progress, throwRetargetableDOMError(poll));
|
2021-10-20 12:01:05 -08:00
|
|
|
assertDone(throwRetargetableDOMError(await pollHandler.finishMaybeNotConnected()));
|
2020-08-17 16:22:34 -07:00
|
|
|
}, this._page._timeoutSettings.timeout(options));
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
async waitForSelector(metadata: CallMetadata, selector: string, options: types.WaitForElementOptions = {}): Promise<ElementHandle<Element> | null> {
|
2020-08-14 14:47:24 -07:00
|
|
|
const { state = 'visible' } = options;
|
|
|
|
if (!['attached', 'detached', 'visible', 'hidden'].includes(state))
|
|
|
|
throw new Error(`state: expected one of (attached|detached|visible|hidden)`);
|
2021-08-18 12:51:45 -07:00
|
|
|
const info = this._page.parseSelector(selector, options);
|
2021-09-28 13:57:11 -07:00
|
|
|
const task = waitForSelectorTask(info, state, false, this);
|
2021-02-09 14:44:48 -08:00
|
|
|
const controller = new ProgressController(metadata, this);
|
|
|
|
return controller.run(async progress => {
|
2020-08-17 14:12:31 -07:00
|
|
|
progress.log(`waiting for selector "${selector}"${state === 'attached' ? '' : ' to be ' + state}`);
|
2020-08-14 14:47:24 -07:00
|
|
|
const context = await this._context.frame._context(info.world);
|
|
|
|
const injected = await context.injectedScript();
|
|
|
|
const pollHandler = new InjectedScriptPollHandler(progress, await task(injected));
|
|
|
|
const result = await pollHandler.finishHandle();
|
|
|
|
if (!result.asElement()) {
|
|
|
|
result.dispose();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const handle = result.asElement() as ElementHandle<Element>;
|
|
|
|
return handle._adoptTo(await this._context.frame._mainContext());
|
2020-08-14 18:25:32 -07:00
|
|
|
}, this._page._timeoutSettings.timeout(options));
|
2020-08-14 14:47:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async _adoptTo(context: FrameExecutionContext): Promise<ElementHandle<T>> {
|
|
|
|
if (this._context !== context) {
|
|
|
|
const adopted = await this._page._delegate.adoptElementHandle(this, context);
|
|
|
|
this.dispose();
|
|
|
|
return adopted;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-06-24 08:18:09 -07:00
|
|
|
async _waitForDisplayedAtStablePosition(progress: Progress, force: boolean, waitForEnabled: boolean): Promise<'error:notconnected' | 'done'> {
|
2020-07-29 16:36:02 -07:00
|
|
|
if (waitForEnabled)
|
2021-02-10 12:36:26 -08:00
|
|
|
progress.log(` waiting for element to be visible, enabled and stable`);
|
2020-07-29 16:36:02 -07:00
|
|
|
else
|
2021-02-10 12:36:26 -08:00
|
|
|
progress.log(` waiting for element to be visible and stable`);
|
2021-09-17 22:18:00 -07:00
|
|
|
const poll = await this.evaluateHandleInUtility(([injected, node, { waitForEnabled, force }]) => {
|
2021-02-10 12:36:26 -08:00
|
|
|
return injected.waitForElementStatesAndPerformAction(node,
|
2021-06-24 08:18:09 -07:00
|
|
|
waitForEnabled ? ['visible', 'stable', 'enabled'] : ['visible', 'stable'], force, () => 'done' as const);
|
|
|
|
}, { waitForEnabled, force });
|
2021-09-17 22:18:00 -07:00
|
|
|
if (poll === 'error:notconnected')
|
|
|
|
return poll;
|
|
|
|
const pollHandler = new InjectedScriptPollHandler(progress, poll);
|
2021-10-20 12:01:05 -08:00
|
|
|
const result = await pollHandler.finishMaybeNotConnected();
|
2020-07-29 16:36:02 -07:00
|
|
|
if (waitForEnabled)
|
2021-02-10 12:36:26 -08:00
|
|
|
progress.log(' element is visible, enabled and stable');
|
2020-07-29 16:36:02 -07:00
|
|
|
else
|
2021-02-10 12:36:26 -08:00
|
|
|
progress.log(' element is visible and stable');
|
2021-09-24 20:51:09 -07:00
|
|
|
return result;
|
2020-02-19 09:34:57 -08:00
|
|
|
}
|
|
|
|
|
2020-08-14 14:48:36 -07:00
|
|
|
async _checkHitTargetAt(point: types.Point): Promise<'error:notconnected' | { hitTargetDescription: string } | '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-24 15:12:17 -07:00
|
|
|
return 'error: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 };
|
|
|
|
}
|
2021-03-18 01:47:07 +08:00
|
|
|
return this.evaluateInUtility(([injected, node, point]) => injected.checkHitTargetAt(node, point), point);
|
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-08-24 15:30:45 -07:00
|
|
|
private _poll: js.JSHandle<InjectedScriptPoll<T>> | null;
|
2020-06-01 15:48:23 -07:00
|
|
|
|
2020-08-24 15:30:45 -07:00
|
|
|
constructor(progress: Progress, poll: js.JSHandle<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-25 13:13:10 -07:00
|
|
|
this._streamLogs();
|
2020-06-01 15:48:23 -07:00
|
|
|
}
|
|
|
|
|
2020-06-25 13:13:10 -07:00
|
|
|
private async _streamLogs() {
|
|
|
|
while (this._poll && this._progress.isRunning()) {
|
2021-09-23 16:46:46 -07:00
|
|
|
const log = await this._poll.evaluate(poll => poll.takeNextLogs()).catch(e => [] as LogEntry[]);
|
2020-06-25 13:13:10 -07:00
|
|
|
if (!this._poll || !this._progress.isRunning())
|
2020-06-01 15:48:23 -07:00
|
|
|
return;
|
2021-09-23 16:46:46 -07:00
|
|
|
for (const entry of log)
|
|
|
|
this._progress.logEntry(entry);
|
2020-06-25 13:13:10 -07:00
|
|
|
}
|
2020-06-01 15:48:23 -07:00
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async finishHandle(): Promise<js.SmartHandle<T>> {
|
2020-06-06 20:59:06 -07:00
|
|
|
try {
|
2021-01-19 11:27:05 -08:00
|
|
|
const result = await this._poll!.evaluateHandle(poll => poll.run());
|
2020-06-06 20:59:06 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 12:01:05 -08:00
|
|
|
async finish(): Promise<T> {
|
|
|
|
try {
|
|
|
|
const result = await this._poll!.evaluate(poll => poll.run());
|
|
|
|
await this._finishInternal();
|
|
|
|
return result;
|
|
|
|
} finally {
|
|
|
|
await this.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async finishMaybeNotConnected(): Promise<T | 'error:notconnected'> {
|
2020-06-06 20:59:06 -07:00
|
|
|
try {
|
2021-01-19 11:27:05 -08:00
|
|
|
const result = await this._poll!.evaluate(poll => poll.run());
|
2020-06-06 20:59:06 -07:00
|
|
|
await this._finishInternal();
|
|
|
|
return result;
|
2021-09-17 22:18:00 -07:00
|
|
|
} catch (e) {
|
|
|
|
if (js.isJavaScriptErrorInEvaluate(e) || isSessionClosedError(e))
|
|
|
|
throw e;
|
|
|
|
return 'error:notconnected';
|
2020-06-06 20:59:06 -07:00
|
|
|
} 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.
|
2021-09-23 16:46:46 -07:00
|
|
|
const log = await this._poll.evaluate(poll => poll.takeLastLogs()).catch(e => [] as LogEntry[]);
|
|
|
|
for (const entry of log)
|
|
|
|
this._progress.logEntry(entry);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 20:51:09 -07:00
|
|
|
export function throwRetargetableDOMError<T>(result: T | 'error:notconnected'): T {
|
2020-06-24 15:12:17 -07:00
|
|
|
if (result === 'error:notconnected')
|
2020-06-12 14:59:26 -07:00
|
|
|
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
|
|
|
|
2020-09-11 13:28:24 -07:00
|
|
|
export function assertDone(result: 'done'): void {
|
2020-06-24 15:12:17 -07:00
|
|
|
// This function converts 'done' to void and ensures typescript catches unhandled errors.
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
2020-06-26 16:32:42 -07:00
|
|
|
|
2020-08-20 16:49:19 -07:00
|
|
|
function compensateHalfIntegerRoundingError(point: types.Point) {
|
|
|
|
// Firefox internally uses integer coordinates, so 8.5 is converted to 9 when clicking.
|
|
|
|
//
|
|
|
|
// This does not work nicely for small elements. For example, 1x1 square with corners
|
|
|
|
// (8;8) and (9;9) is targeted when clicking at (8;8) but not when clicking at (9;9).
|
|
|
|
// So, clicking at (8.5;8.5) will effectively click at (9;9) and miss the target.
|
|
|
|
//
|
|
|
|
// Therefore, we skew half-integer values from the interval (8.49, 8.51) towards
|
|
|
|
// (8.47, 8.49) that is rounded towards 8. This means clicking at (8.5;8.5) will
|
|
|
|
// be replaced with (8.48;8.48) and will effectively click at (8;8).
|
|
|
|
//
|
|
|
|
// Other browsers use float coordinates, so this change should not matter.
|
|
|
|
const remainderX = point.x - Math.floor(point.x);
|
|
|
|
if (remainderX > 0.49 && remainderX < 0.51)
|
|
|
|
point.x -= 0.02;
|
|
|
|
const remainderY = point.y - Math.floor(point.y);
|
|
|
|
if (remainderY > 0.49 && remainderY < 0.51)
|
|
|
|
point.y -= 0.02;
|
|
|
|
}
|
|
|
|
|
2020-08-24 15:30:45 -07:00
|
|
|
export type SchedulableTask<T> = (injectedScript: js.JSHandle<InjectedScript>) => Promise<js.JSHandle<InjectedScriptPoll<T>>>;
|
2020-06-26 16:32:42 -07:00
|
|
|
|
2021-09-28 13:57:11 -07:00
|
|
|
export function waitForSelectorTask(selector: SelectorInfo, state: 'attached' | 'detached' | 'visible' | 'hidden', omitReturnValue?: boolean, root?: ElementHandle): SchedulableTask<Element | undefined> {
|
|
|
|
return injectedScript => injectedScript.evaluateHandle((injected, { parsed, strict, state, omitReturnValue, root }) => {
|
2020-06-26 16:32:42 -07:00
|
|
|
let lastElement: Element | undefined;
|
|
|
|
|
|
|
|
return injected.pollRaf((progress, continuePolling) => {
|
2021-07-15 22:06:08 +02:00
|
|
|
const elements = injected.querySelectorAll(parsed, root || document);
|
2021-09-28 13:57:11 -07:00
|
|
|
let element: Element | undefined = elements[0];
|
2020-06-26 16:32:42 -07:00
|
|
|
const visible = element ? injected.isVisible(element) : false;
|
|
|
|
|
|
|
|
if (lastElement !== element) {
|
|
|
|
lastElement = element;
|
2021-07-15 22:06:08 +02:00
|
|
|
if (!element) {
|
2020-06-26 16:32:42 -07:00
|
|
|
progress.log(` selector did not resolve to any element`);
|
2021-07-15 22:06:08 +02:00
|
|
|
} else {
|
2021-07-26 22:00:23 -07:00
|
|
|
if (elements.length > 1) {
|
|
|
|
if (strict)
|
2021-08-26 21:21:19 -07:00
|
|
|
throw injected.strictModeViolationError(parsed, elements);
|
2021-07-15 22:06:08 +02:00
|
|
|
progress.log(` selector resolved to ${elements.length} elements. Proceeding with the first one.`);
|
2021-07-26 22:00:23 -07:00
|
|
|
}
|
2020-06-26 16:32:42 -07:00
|
|
|
progress.log(` selector resolved to ${visible ? 'visible' : 'hidden'} ${injected.previewNode(element)}`);
|
2021-07-15 22:06:08 +02:00
|
|
|
}
|
2020-06-26 16:32:42 -07:00
|
|
|
}
|
|
|
|
|
2021-09-28 13:57:11 -07:00
|
|
|
const hasElement = !!element;
|
|
|
|
if (omitReturnValue)
|
|
|
|
element = undefined;
|
|
|
|
|
2020-06-26 16:32:42 -07:00
|
|
|
switch (state) {
|
|
|
|
case 'attached':
|
2021-09-28 13:57:11 -07:00
|
|
|
return hasElement ? element : continuePolling;
|
2020-06-26 16:32:42 -07:00
|
|
|
case 'detached':
|
2021-09-28 13:57:11 -07:00
|
|
|
return !hasElement ? undefined : continuePolling;
|
2020-06-26 16:32:42 -07:00
|
|
|
case 'visible':
|
|
|
|
return visible ? element : continuePolling;
|
|
|
|
case 'hidden':
|
|
|
|
return !visible ? undefined : continuePolling;
|
|
|
|
}
|
|
|
|
});
|
2021-09-28 13:57:11 -07:00
|
|
|
}, { parsed: selector.parsed, strict: selector.strict, state, omitReturnValue, root });
|
2020-06-26 16:32:42 -07:00
|
|
|
}
|
|
|
|
|
2021-06-02 20:17:24 -07:00
|
|
|
export const kUnableToAdoptErrorMessage = 'Unable to adopt element handle from a different document';
|