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
|
|
|
|
2022-09-20 18:41:51 -07:00
|
|
|
import type * as channels from '@protocol/channels';
|
2023-11-10 15:24:31 -08:00
|
|
|
import * as injectedScriptSource from '../generated/injectedScriptSource';
|
2022-01-18 19:13:51 -08:00
|
|
|
import { isSessionClosedError } from './protocolError';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { ScreenshotOptions } from './screenshotter';
|
|
|
|
import type * as frames from './frames';
|
2024-01-16 19:11:41 -08:00
|
|
|
import type { InjectedScript, HitTargetInterceptionResult, ElementState } from './injected/injectedScript';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { CallMetadata } from './instrumentation';
|
2019-11-27 16:03:51 -08:00
|
|
|
import * as js from './javascript';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { Page } from './page';
|
|
|
|
import type { Progress } from './progress';
|
|
|
|
import { ProgressController } from './progress';
|
|
|
|
import type * as types from './types';
|
|
|
|
import type { TimeoutOptions } from '../common/types';
|
2022-06-01 15:22:43 -07:00
|
|
|
import { isUnderTest } from '../utils';
|
2023-11-10 15:24:31 -08:00
|
|
|
import { prepareFilesForUpload } from './fileUploadUtils';
|
|
|
|
|
|
|
|
export type InputFilesItems = {
|
|
|
|
filePayloads?: types.FilePayload[],
|
|
|
|
localPaths?: string[]
|
|
|
|
};
|
2019-11-27 16:02:31 -08:00
|
|
|
|
2022-01-31 16:21:35 -08:00
|
|
|
type ActionName = 'click' | 'hover' | 'dblclick' | 'tap' | 'move and up' | 'move and down';
|
2024-01-16 19:11:41 -08:00
|
|
|
type PerformActionResult = 'error:notvisible' | 'error:notconnected' | 'error:notinviewport' | 'error:optionsnotfound' | { missingState: ElementState } | { hitTargetDescription: string } | 'done';
|
2021-05-19 01:30:20 +00:00
|
|
|
|
2022-01-06 15:15:11 -08:00
|
|
|
export class NonRecoverableDOMError extends Error {
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isNonRecoverableDOMError(error: Error) {
|
|
|
|
return error instanceof NonRecoverableDOMError;
|
|
|
|
}
|
|
|
|
|
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) {
|
2023-03-31 18:18:45 -07:00
|
|
|
super(frame, delegate, world || 'content-script');
|
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 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);
|
|
|
|
}
|
|
|
|
|
2022-11-29 16:57:11 -08:00
|
|
|
async evaluateExpression(expression: string, options: { isFunction?: boolean, exposeUtilityScript?: boolean }, arg?: any): Promise<any> {
|
|
|
|
return js.evaluateExpression(this, expression, { ...options, returnByValue: true }, arg);
|
2021-07-09 16:19:42 +02:00
|
|
|
}
|
|
|
|
|
2023-05-31 14:08:44 -07:00
|
|
|
async evaluateExpressionHandle(expression: string, options: { isFunction?: boolean, exposeUtilityScript?: boolean }, arg?: any): Promise<js.JSHandle<any>> {
|
|
|
|
return js.evaluateExpression(this, expression, { ...options, returnByValue: false }, arg);
|
2021-07-09 16:19:42 +02:00
|
|
|
}
|
|
|
|
|
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[] = [];
|
2023-02-21 14:08:51 -08:00
|
|
|
const selectorsRegistry = this.frame._page.context().selectors();
|
|
|
|
for (const [name, { source }] of selectorsRegistry._engines)
|
2020-05-15 15:21:49 -07:00
|
|
|
custom.push(`{ name: '${name}', engine: (${source}) }`);
|
2023-06-01 17:54:43 -07:00
|
|
|
const sdkLanguage = this.frame.attribution.playwright.options.sdkLanguage;
|
2020-05-15 15:21:49 -07:00
|
|
|
const source = `
|
2021-01-08 16:15:05 -08:00
|
|
|
(() => {
|
2022-03-28 22:10:17 -08:00
|
|
|
const module = {};
|
2021-01-08 16:15:05 -08:00
|
|
|
${injectedScriptSource.source}
|
2023-03-05 20:01:35 -08:00
|
|
|
return new (module.exports.InjectedScript())(
|
2023-02-17 11:19:53 -08:00
|
|
|
globalThis,
|
2022-03-01 13:56:21 -08:00
|
|
|
${isUnderTest()},
|
2022-10-18 16:09:54 -04:00
|
|
|
"${sdkLanguage}",
|
2023-02-21 14:08:51 -08:00
|
|
|
${JSON.stringify(selectorsRegistry.testIdAttributeName())},
|
2021-02-10 12:36:26 -08:00
|
|
|
${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
|
|
|
`;
|
2023-03-31 18:18:45 -07:00
|
|
|
this._injectedScriptPromise = this.rawEvaluateHandle(source).then(objectId => new js.JSHandle(this, 'object', 'InjectedScript', objectId));
|
2019-11-28 12:50:52 -08:00
|
|
|
}
|
2020-06-11 18:18:33 -07:00
|
|
|
return this._injectedScriptPromise;
|
|
|
|
}
|
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> {
|
2022-12-05 17:22:25 -08:00
|
|
|
__elementhandle: T = true as any;
|
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;
|
2022-02-28 13:25:59 -07:00
|
|
|
readonly _frame: frames.Frame;
|
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;
|
2021-11-05 10:06:04 -08:00
|
|
|
this._frame = context.frame;
|
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-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 {
|
2021-11-05 10:06:04 -08:00
|
|
|
const utility = await this._frame._utilityContext();
|
2021-09-17 22:18:00 -07:00
|
|
|
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 {
|
2021-11-05 10:06:04 -08:00
|
|
|
const utility = await this._frame._utilityContext();
|
2021-09-17 22:18:00 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-05 15:36:01 -08:00
|
|
|
async isIframeElement(): Promise<boolean | 'error:notconnected'> {
|
|
|
|
return this.evaluateInUtility(([injected, node]) => node && (node.nodeName === 'IFRAME' || node.nodeName === 'FRAME'), {});
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
async contentFrame(): Promise<frames.Frame | null> {
|
2021-11-05 15:36:01 -08:00
|
|
|
const isFrameElement = throwRetargetableDOMError(await this.isIframeElement());
|
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
|
|
|
}
|
|
|
|
|
2023-06-09 07:18:13 -07:00
|
|
|
async getAttribute(metadata: CallMetadata, name: string): Promise<string | null> {
|
|
|
|
return this._frame.getAttribute(metadata, ':scope', name, {}, this);
|
2020-04-09 16:49:23 -07:00
|
|
|
}
|
|
|
|
|
2023-06-09 07:18:13 -07:00
|
|
|
async inputValue(metadata: CallMetadata): Promise<string> {
|
|
|
|
return this._frame.inputValue(metadata, ':scope', {}, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
async textContent(metadata: CallMetadata): Promise<string | null> {
|
|
|
|
return this._frame.textContent(metadata, ':scope', {}, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
async innerText(metadata: CallMetadata): Promise<string> {
|
|
|
|
return this._frame.innerText(metadata, ':scope', {}, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
async innerHTML(metadata: CallMetadata): Promise<string> {
|
|
|
|
return this._frame.innerHTML(metadata, ':scope', {}, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
async dispatchEvent(metadata: CallMetadata, type: string, eventInit: Object = {}) {
|
|
|
|
return this._frame.dispatchEvent(metadata, ':scope', type, eventInit, {}, this);
|
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
|
|
|
}
|
|
|
|
|
2022-04-23 21:48:36 +01:00
|
|
|
async _waitAndScrollIntoViewIfNeeded(progress: Progress, waitForVisible: boolean): Promise<void> {
|
2024-01-16 19:11:41 -08:00
|
|
|
const result = await this._retryAction(progress, 'scroll into view', async () => {
|
|
|
|
progress.log(` waiting for element to be stable`);
|
|
|
|
const waitResult = await this.evaluateInUtility(async ([injected, node, { waitForVisible }]) => {
|
|
|
|
return await injected.checkElementStates(node, waitForVisible ? ['visible', 'stable'] : ['stable']);
|
|
|
|
}, { waitForVisible });
|
|
|
|
if (waitResult)
|
|
|
|
return waitResult;
|
|
|
|
return await this._scrollRectIntoViewIfNeeded();
|
|
|
|
}, {});
|
|
|
|
assertDone(throwRetargetableDOMError(result));
|
2020-06-24 10:16:54 -07:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
async scrollIntoViewIfNeeded(metadata: CallMetadata, options: types.TimeoutOptions = {}) {
|
|
|
|
const controller = new ProgressController(metadata, this);
|
|
|
|
return controller.run(
|
2022-04-23 21:48:36 +01:00
|
|
|
progress => this._waitAndScrollIntoViewIfNeeded(progress, false /* waitForVisible */),
|
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
|
|
|
}
|
|
|
|
|
2024-01-16 19:11:41 -08:00
|
|
|
async _retryAction(progress: Progress, actionName: string, action: (retry: number) => Promise<PerformActionResult>, options: { trial?: boolean, force?: boolean }): 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];
|
|
|
|
|
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
|
|
|
}
|
2024-01-16 19:11:41 -08:00
|
|
|
const result = await action(retry);
|
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)
|
2022-01-06 15:15:11 -08:00
|
|
|
throw new NonRecoverableDOMError('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)
|
2022-01-06 15:15:11 -08:00
|
|
|
throw new NonRecoverableDOMError('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;
|
|
|
|
}
|
2024-01-16 19:11:41 -08:00
|
|
|
if (result === 'error:optionsnotfound') {
|
|
|
|
progress.log(' did not find some options');
|
|
|
|
continue;
|
|
|
|
}
|
2020-08-14 14:48:36 -07:00
|
|
|
if (typeof result === 'object' && 'hitTargetDescription' in result) {
|
2020-08-17 14:12:31 -07:00
|
|
|
progress.log(` ${result.hitTargetDescription} intercepts pointer events`);
|
2020-06-12 14:59:26 -07:00
|
|
|
continue;
|
|
|
|
}
|
2024-01-16 19:11:41 -08:00
|
|
|
if (typeof result === 'object' && 'missingState' in result) {
|
|
|
|
progress.log(` element is not ${result.missingState}`);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-01-16 19:11:41 -08:00
|
|
|
async _retryPointerAction(progress: Progress, actionName: ActionName, waitForEnabled: boolean, action: (point: types.Point) => Promise<void>,
|
|
|
|
options: types.PointerActionOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): Promise<'error:notconnected' | 'done'> {
|
|
|
|
return await this._retryAction(progress, actionName, async retry => {
|
|
|
|
// 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' },
|
|
|
|
];
|
|
|
|
const forceScrollOptions = scrollOptions[retry % scrollOptions.length];
|
|
|
|
return await this._performPointerAction(progress, actionName, waitForEnabled, action, forceScrollOptions, options);
|
|
|
|
}, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
async _performPointerAction(progress: Progress, actionName: ActionName, waitForEnabled: boolean, action: (point: types.Point) => Promise<void>, forceScrollOptions: ScrollIntoViewOptions | undefined, options: types.PointerActionOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): Promise<PerformActionResult> {
|
2020-04-29 11:05:23 -07:00
|
|
|
const { force = false, position } = options;
|
2023-10-02 17:21:06 -07:00
|
|
|
|
|
|
|
const doScrollIntoView = async () => {
|
|
|
|
if (forceScrollOptions) {
|
|
|
|
return await this.evaluateInUtility(([injected, node, options]) => {
|
|
|
|
if (node.nodeType === 1 /* Node.ELEMENT_NODE */)
|
|
|
|
(node as Node as Element).scrollIntoView(options);
|
|
|
|
return 'done' as const;
|
|
|
|
}, forceScrollOptions);
|
|
|
|
}
|
|
|
|
return await this._scrollRectIntoViewIfNeeded(position ? { x: position.x, y: position.y, width: 0, height: 0 } : undefined);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this._frame.parentFrame()) {
|
|
|
|
// Best-effort scroll to make sure any iframes containing this element are scrolled
|
|
|
|
// into view and visible, so they are not throttled.
|
|
|
|
// See https://github.com/microsoft/playwright/issues/27196 for an example.
|
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
|
|
|
await doScrollIntoView().catch(() => {});
|
|
|
|
}
|
|
|
|
|
2020-06-26 16:31:51 -07:00
|
|
|
if ((options as any).__testHookBeforeStable)
|
|
|
|
await (options as any).__testHookBeforeStable();
|
2024-01-16 19:11:41 -08:00
|
|
|
|
|
|
|
if (!force) {
|
|
|
|
const elementStates: ElementState[] = waitForEnabled ? ['visible', 'enabled', 'stable'] : ['visible', 'stable'];
|
|
|
|
progress.log(` waiting for element to be ${waitForEnabled ? 'visible, enabled and stable' : 'visible and stable'}`);
|
|
|
|
const result = await this.evaluateInUtility(async ([injected, node, { elementStates }]) => {
|
|
|
|
return await injected.checkElementStates(node, elementStates);
|
|
|
|
}, { elementStates });
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
progress.log(` element is ${waitForEnabled ? 'visible, enabled and stable' : 'visible and stable'}`);
|
|
|
|
}
|
|
|
|
|
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.
|
2023-10-02 17:21:06 -07:00
|
|
|
const scrolled = await doScrollIntoView();
|
|
|
|
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);
|
2021-11-05 17:31:28 -07:00
|
|
|
progress.metadata.point = point;
|
|
|
|
await progress.beforeInputAction(this);
|
|
|
|
|
|
|
|
let hitTargetInterceptionHandle: js.JSHandle<HitTargetInterceptionResult> | undefined;
|
2024-01-16 19:11:41 -08:00
|
|
|
if (force) {
|
|
|
|
progress.log(` forcing action`);
|
|
|
|
} else {
|
2021-11-05 17:31:28 -07:00
|
|
|
if ((options as any).__testHookBeforeHitTarget)
|
|
|
|
await (options as any).__testHookBeforeHitTarget();
|
|
|
|
|
2022-06-24 13:17:25 -07:00
|
|
|
const frameCheckResult = await this._checkFrameIsHitTarget(point);
|
|
|
|
if (frameCheckResult === 'error:notconnected' || ('hitTargetDescription' in frameCheckResult))
|
|
|
|
return frameCheckResult;
|
|
|
|
const hitPoint = frameCheckResult.framePoint;
|
2022-06-01 15:23:41 -07:00
|
|
|
const actionType = actionName === 'move and up' ? 'drag' : ((actionName === 'hover' || actionName === 'tap') ? actionName : 'mouse');
|
|
|
|
const handle = await this.evaluateHandleInUtility(([injected, node, { actionType, hitPoint, trial }]) => injected.setupHitTargetInterceptor(node, actionType, hitPoint, trial), { actionType, hitPoint, trial: !!options.trial } as const);
|
|
|
|
if (handle === 'error:notconnected')
|
|
|
|
return handle;
|
|
|
|
if (!handle._objectId) {
|
|
|
|
const error = handle.rawValue() as string;
|
|
|
|
if (error === 'error:notconnected')
|
|
|
|
return error;
|
|
|
|
return { hitTargetDescription: error };
|
2022-01-31 16:21:35 -08:00
|
|
|
}
|
2022-06-01 15:23:41 -07:00
|
|
|
hitTargetInterceptionHandle = handle as any;
|
|
|
|
progress.cleanupWhenAborted(() => {
|
|
|
|
// Do not await here, just in case the renderer is stuck (e.g. on alert)
|
|
|
|
// and we won't be able to cleanup.
|
|
|
|
hitTargetInterceptionHandle!.evaluate(h => h.stop()).catch(e => {});
|
2023-03-31 18:18:45 -07:00
|
|
|
hitTargetInterceptionHandle!.dispose();
|
2022-06-01 15:23:41 -07:00
|
|
|
});
|
2021-11-05 17:31:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const actionResult = await this._page._frameManager.waitForSignalsCreatedBy(progress, options.noWaitAfter, async () => {
|
|
|
|
if ((options as any).__testHookBeforePointerAction)
|
|
|
|
await (options as any).__testHookBeforePointerAction();
|
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
|
|
|
let restoreModifiers: types.KeyboardModifier[] | undefined;
|
|
|
|
if (options && options.modifiers)
|
|
|
|
restoreModifiers = await this._page.keyboard._ensureModifiers(options.modifiers);
|
|
|
|
progress.log(` performing ${actionName} action`);
|
|
|
|
await action(point);
|
|
|
|
if (restoreModifiers)
|
|
|
|
await this._page.keyboard._ensureModifiers(restoreModifiers);
|
|
|
|
if (hitTargetInterceptionHandle) {
|
2023-03-31 18:18:45 -07:00
|
|
|
const stopHitTargetInterception = hitTargetInterceptionHandle.evaluate(h => h.stop()).catch(e => 'done' as const).finally(() => {
|
|
|
|
hitTargetInterceptionHandle?.dispose();
|
|
|
|
});
|
2021-11-05 17:31:28 -07:00
|
|
|
if (!options.noWaitAfter) {
|
|
|
|
// When noWaitAfter is passed, we do not want to accidentally stall on
|
|
|
|
// non-committed navigation blocking the evaluate.
|
|
|
|
const hitTargetResult = await stopHitTargetInterception;
|
|
|
|
if (hitTargetResult !== 'done')
|
|
|
|
return hitTargetResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
progress.log(` ${options.trial ? 'trial ' : ''}${actionName} action done`);
|
|
|
|
progress.log(' waiting for scheduled navigations to finish');
|
|
|
|
if ((options as any).__testHookAfterPointerAction)
|
|
|
|
await (options as any).__testHookAfterPointerAction();
|
|
|
|
return 'done';
|
|
|
|
}, 'input');
|
|
|
|
if (actionResult !== 'done')
|
|
|
|
return actionResult;
|
|
|
|
progress.log(' navigations have finished');
|
|
|
|
return 'done';
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-10-07 12:43:48 -07:00
|
|
|
_hover(progress: Progress, options: types.PointerActionOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): 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'> {
|
2024-01-16 19:11:41 -08:00
|
|
|
let resultingOptions: string[] = [];
|
|
|
|
await this._retryAction(progress, 'select option', async () => {
|
|
|
|
await progress.beforeInputAction(this);
|
|
|
|
if (!options.force)
|
|
|
|
progress.log(` waiting for element to be visible and enabled`);
|
|
|
|
const optionsToSelect = [...elements, ...values];
|
|
|
|
const result = await this.evaluateInUtility(async ([injected, node, { optionsToSelect, force }]) => {
|
|
|
|
if (!force) {
|
|
|
|
const checkResult = await injected.checkElementStates(node, ['visible', 'enabled']);
|
|
|
|
if (checkResult)
|
|
|
|
return checkResult;
|
|
|
|
}
|
|
|
|
return injected.selectOptions(node, optionsToSelect);
|
2021-06-24 08:18:09 -07:00
|
|
|
}, { optionsToSelect, force: options.force });
|
2024-01-16 19:11:41 -08:00
|
|
|
if (Array.isArray(result)) {
|
|
|
|
progress.log(' selected specified option(s)');
|
|
|
|
resultingOptions = result;
|
|
|
|
return 'done';
|
|
|
|
}
|
2021-01-19 11:27:05 -08:00
|
|
|
return result;
|
2024-01-16 19:11:41 -08:00
|
|
|
}, options);
|
|
|
|
return resultingOptions;
|
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'> {
|
2024-01-16 19:11:41 -08:00
|
|
|
progress.log(` fill("${value}")`);
|
|
|
|
return await this._retryAction(progress, 'fill', async () => {
|
|
|
|
await progress.beforeInputAction(this);
|
|
|
|
return this._page._frameManager.waitForSignalsCreatedBy(progress, options.noWaitAfter, async () => {
|
|
|
|
if (!options.force)
|
|
|
|
progress.log(' waiting for element to be visible, enabled and editable');
|
|
|
|
const result = await this.evaluateInUtility(async ([injected, node, { value, force }]) => {
|
|
|
|
if (!force) {
|
|
|
|
const checkResult = await injected.checkElementStates(node, ['visible', 'enabled', 'editable']);
|
|
|
|
if (checkResult)
|
|
|
|
return checkResult;
|
|
|
|
}
|
|
|
|
return injected.fill(node, value);
|
|
|
|
}, { value, force: options.force });
|
2020-06-24 15:12:17 -07:00
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
2024-01-16 19:11:41 -08:00
|
|
|
if (result === 'needsinput') {
|
|
|
|
if (value)
|
|
|
|
await this._page.keyboard.insertText(value);
|
|
|
|
else
|
|
|
|
await this._page.keyboard.press('Delete');
|
|
|
|
return 'done';
|
|
|
|
} else {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}, 'input');
|
|
|
|
}, options);
|
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 => {
|
2024-01-16 19:11:41 -08:00
|
|
|
const result = await this._retryAction(progress, 'selectText', async () => {
|
|
|
|
if (!options.force)
|
|
|
|
progress.log(' waiting for element to be visible');
|
|
|
|
return await this.evaluateInUtility(async ([injected, node, { force }]) => {
|
|
|
|
if (!force) {
|
|
|
|
const checkResult = await injected.checkElementStates(node, ['visible']);
|
|
|
|
if (checkResult)
|
|
|
|
return checkResult;
|
|
|
|
}
|
|
|
|
return injected.selectText(node);
|
|
|
|
}, { force: options.force });
|
|
|
|
}, options);
|
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
|
|
|
}
|
|
|
|
|
2023-11-10 15:24:31 -08:00
|
|
|
async setInputFiles(metadata: CallMetadata, params: channels.ElementHandleSetInputFilesParams) {
|
|
|
|
const inputFileItems = await prepareFilesForUpload(this._frame, params);
|
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 => {
|
2023-11-10 15:24:31 -08:00
|
|
|
const result = await this._setInputFiles(progress, inputFileItems, params);
|
2020-09-17 09:32:54 -07:00
|
|
|
return assertDone(throwRetargetableDOMError(result));
|
2023-11-10 15:24:31 -08:00
|
|
|
}, this._page._timeoutSettings.timeout(params));
|
2020-06-03 11:23:24 -07:00
|
|
|
}
|
|
|
|
|
2022-03-18 09:00:52 -07:00
|
|
|
async _setInputFiles(progress: Progress, items: InputFilesItems, options: types.NavigatingActionWaitOptions): Promise<'error:notconnected' | 'done'> {
|
2023-11-10 15:24:31 -08:00
|
|
|
const { filePayloads, localPaths } = items;
|
|
|
|
const multiple = filePayloads && filePayloads.length > 1 || localPaths && localPaths.length > 1;
|
2021-11-01 15:59:47 -07:00
|
|
|
const result = await this.evaluateHandleInUtility(([injected, node, multiple]): Element | undefined => {
|
2021-06-28 14:18:01 -07:00
|
|
|
const element = injected.retarget(node, 'follow-label');
|
|
|
|
if (!element)
|
2021-11-01 15:59:47 -07:00
|
|
|
return;
|
2021-06-28 14:18:01 -07:00
|
|
|
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;
|
2022-03-18 09:00:52 -07:00
|
|
|
}, multiple);
|
2021-11-01 15:59:47 -07:00
|
|
|
if (result === 'error:notconnected' || !result.asElement())
|
|
|
|
return 'error:notconnected';
|
|
|
|
const retargeted = result.asElement() as ElementHandle<HTMLInputElement>;
|
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.
|
2022-03-18 09:00:52 -07:00
|
|
|
if (localPaths)
|
2023-06-09 07:18:13 -07:00
|
|
|
await this._page._delegate.setInputFilePaths(progress, retargeted, localPaths);
|
2022-03-18 09:00:52 -07:00
|
|
|
else
|
2022-07-05 08:58:34 -07:00
|
|
|
await this._page._delegate.setInputFiles(retargeted, 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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-10-25 06:10:40 -07:00
|
|
|
async _blur(progress: Progress): Promise<'error:notconnected' | 'done'> {
|
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
|
|
|
return await this.evaluateInUtility(([injected, node]) => injected.blurNode(node), {});
|
|
|
|
}
|
|
|
|
|
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)
|
2022-01-06 15:15:11 -08:00
|
|
|
throw new NonRecoverableDOMError('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
|
|
|
}
|
|
|
|
|
2022-02-18 22:34:56 -07:00
|
|
|
async screenshot(metadata: CallMetadata, options: ScreenshotOptions & TimeoutOptions = {}): Promise<Buffer> {
|
2021-02-09 14:44:48 -08:00
|
|
|
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> {
|
2023-02-21 14:08:51 -08:00
|
|
|
return this._frame.selectors.query(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>[]> {
|
2023-02-21 14:08:51 -08:00
|
|
|
return this._frame.selectors.queryAll(selector, this);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2023-05-31 14:08:44 -07:00
|
|
|
async evalOnSelector(selector: string, strict: boolean, expression: string, isFunction: boolean | undefined, arg: any): Promise<any> {
|
2023-06-09 07:18:13 -07:00
|
|
|
return this._frame.evalOnSelector(selector, strict, expression, isFunction, arg, this);
|
2019-12-04 13:11:10 -08:00
|
|
|
}
|
|
|
|
|
2023-05-31 14:08:44 -07:00
|
|
|
async evalOnSelectorAll(selector: string, expression: string, isFunction: boolean | undefined, arg: any): Promise<any> {
|
2023-06-09 07:18:13 -07:00
|
|
|
return this._frame.evalOnSelectorAll(selector, expression, isFunction, arg, this);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2023-06-09 07:18:13 -07:00
|
|
|
async isVisible(metadata: CallMetadata): Promise<boolean> {
|
|
|
|
return this._frame.isVisible(metadata, ':scope', {}, this);
|
2021-01-08 12:27:54 -08:00
|
|
|
}
|
|
|
|
|
2023-06-09 07:18:13 -07:00
|
|
|
async isHidden(metadata: CallMetadata): Promise<boolean> {
|
|
|
|
return this._frame.isHidden(metadata, ':scope', {}, this);
|
2021-01-08 12:27:54 -08:00
|
|
|
}
|
|
|
|
|
2023-06-09 07:18:13 -07:00
|
|
|
async isEnabled(metadata: CallMetadata): Promise<boolean> {
|
|
|
|
return this._frame.isEnabled(metadata, ':scope', {}, this);
|
2021-01-08 12:27:54 -08:00
|
|
|
}
|
|
|
|
|
2023-06-09 07:18:13 -07:00
|
|
|
async isDisabled(metadata: CallMetadata): Promise<boolean> {
|
|
|
|
return this._frame.isDisabled(metadata, ':scope', {}, this);
|
2021-01-08 12:27:54 -08:00
|
|
|
}
|
|
|
|
|
2023-06-09 07:18:13 -07:00
|
|
|
async isEditable(metadata: CallMetadata): Promise<boolean> {
|
|
|
|
return this._frame.isEditable(metadata, ':scope', {}, this);
|
2021-01-08 12:27:54 -08:00
|
|
|
}
|
|
|
|
|
2023-06-09 07:18:13 -07:00
|
|
|
async isChecked(metadata: CallMetadata): Promise<boolean> {
|
|
|
|
return this._frame.isChecked(metadata, ':scope', {}, this);
|
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 => {
|
2024-01-16 19:11:41 -08:00
|
|
|
const actionName = `wait for ${state}`;
|
|
|
|
const result = await this._retryAction(progress, actionName, async () => {
|
|
|
|
return await this.evaluateInUtility(async ([injected, node, state]) => {
|
|
|
|
return (await injected.checkElementStates(node, [state])) || 'done';
|
|
|
|
}, state);
|
|
|
|
}, {});
|
2021-11-01 15:59:47 -07:00
|
|
|
assertDone(throwRetargetableDOMError(result));
|
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> {
|
2021-11-05 15:36:01 -08:00
|
|
|
return this._frame.waitForSelector(metadata, selector, options, this);
|
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;
|
|
|
|
}
|
|
|
|
|
2022-11-18 16:51:39 -08:00
|
|
|
async _checkFrameIsHitTarget(point: types.Point): Promise<{ framePoint: types.Point | undefined } | 'error:notconnected' | { hitTargetDescription: string }> {
|
2022-06-24 13:17:25 -07:00
|
|
|
let frame = this._frame;
|
|
|
|
const data: { frame: frames.Frame, frameElement: ElementHandle<Element> | null, pointInFrame: types.Point }[] = [];
|
|
|
|
while (frame.parentFrame()) {
|
|
|
|
const frameElement = await frame.frameElement() as ElementHandle<Element>;
|
|
|
|
const box = await frameElement.boundingBox();
|
2022-11-18 16:51:39 -08:00
|
|
|
const style = await frameElement.evaluateInUtility(([injected, iframe]) => injected.describeIFrameStyle(iframe), {}).catch(e => 'error:notconnected' as const);
|
|
|
|
if (!box || style === 'error:notconnected')
|
2020-06-24 15:12:17 -07:00
|
|
|
return 'error:notconnected';
|
2022-11-18 16:51:39 -08:00
|
|
|
if (style === 'transformed') {
|
|
|
|
// We cannot translate coordinates when iframe has any transform applied.
|
|
|
|
// The best we can do right now is to skip the hitPoint check,
|
|
|
|
// and solely rely on the event interceptor.
|
|
|
|
return { framePoint: undefined };
|
|
|
|
}
|
2020-02-19 09:34:57 -08:00
|
|
|
// Translate from viewport coordinates to frame coordinates.
|
2022-12-27 16:59:34 -08:00
|
|
|
const pointInFrame = { x: point.x - box.x - style.left, y: point.y - box.y - style.top };
|
2022-06-24 13:17:25 -07:00
|
|
|
data.push({ frame, frameElement, pointInFrame });
|
|
|
|
frame = frame.parentFrame()!;
|
2020-02-19 09:34:57 -08:00
|
|
|
}
|
2022-06-24 13:17:25 -07:00
|
|
|
// Add main frame.
|
|
|
|
data.push({ frame, frameElement: null, pointInFrame: point });
|
|
|
|
|
|
|
|
for (let i = data.length - 1; i > 0; i--) {
|
|
|
|
const element = data[i - 1].frameElement!;
|
|
|
|
const point = data[i].pointInFrame;
|
|
|
|
// Hit target in the parent frame should hit the child frame element.
|
|
|
|
const hitTargetResult = await element.evaluateInUtility(([injected, element, hitPoint]) => {
|
2022-09-06 17:55:15 -07:00
|
|
|
return injected.expectHitTarget(hitPoint, element);
|
2022-06-24 13:17:25 -07:00
|
|
|
}, point);
|
|
|
|
if (hitTargetResult !== 'done')
|
|
|
|
return hitTargetResult;
|
|
|
|
}
|
|
|
|
return { framePoint: data[0].pointInFrame };
|
2020-02-19 09:34:57 -08:00
|
|
|
}
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
2019-12-03 10:43:13 -08: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;
|
|
|
|
}
|
|
|
|
|
2021-06-02 20:17:24 -07:00
|
|
|
export const kUnableToAdoptErrorMessage = 'Unable to adopt element handle from a different document';
|