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
|
|
|
|
2025-02-19 15:32:12 +01:00
|
|
|
import fs from 'fs';
|
2025-02-07 13:54:01 -08:00
|
|
|
|
|
|
|
import * as js from './javascript';
|
2025-06-16 10:13:31 +01:00
|
|
|
import { isAbortError, ProgressController } from './progress';
|
2025-05-21 21:10:41 +00:00
|
|
|
import { asLocator, isUnderTest } from '../utils';
|
2025-02-07 13:54:01 -08:00
|
|
|
import { prepareFilesForUpload } from './fileUploadUtils';
|
2022-01-18 19:13:51 -08:00
|
|
|
import { isSessionClosedError } from './protocolError';
|
2025-04-29 19:07:06 +00:00
|
|
|
import * as rawInjectedScriptSource from '../generated/injectedScriptSource';
|
2025-02-07 13:54:01 -08:00
|
|
|
|
2022-04-06 13:57:14 -08:00
|
|
|
import type * as frames from './frames';
|
2025-06-03 19:34:55 +01:00
|
|
|
import type { ElementState, HitTargetError, HitTargetInterceptionResult, InjectedScript, InjectedScriptOptions } from '@injected/injectedScript';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { CallMetadata } from './instrumentation';
|
|
|
|
import type { Page } from './page';
|
|
|
|
import type { Progress } from './progress';
|
2025-02-07 13:54:01 -08:00
|
|
|
import type { ScreenshotOptions } from './screenshotter';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type * as types from './types';
|
2025-02-07 13:54:01 -08:00
|
|
|
import type * as channels from '@protocol/channels';
|
|
|
|
|
2023-11-10 15:24:31 -08:00
|
|
|
export type InputFilesItems = {
|
|
|
|
filePayloads?: types.FilePayload[],
|
|
|
|
localPaths?: string[]
|
2024-06-12 22:20:18 +02:00
|
|
|
localDirectory?: string
|
2023-11-10 15:24:31 -08:00
|
|
|
};
|
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';
|
2025-06-03 19:34:55 +01:00
|
|
|
type PerformActionResult = 'error:notvisible' | 'error:notconnected' | 'error:notinviewport' | 'error:optionsnotfound' | { missingState: ElementState } | HitTargetError | '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>;
|
2024-09-23 15:48:11 -07:00
|
|
|
readonly world: types.World | null;
|
2019-11-28 12:50:52 -08:00
|
|
|
|
2024-09-23 15:48:11 -07: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)
|
2025-04-30 18:57:59 -07:00
|
|
|
return this.frame._page.delegate.adoptElementHandle(handle, this);
|
2020-03-19 13:07:33 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-05-31 14:44:26 -07:00
|
|
|
async evaluateExpression(expression: string, options: { isFunction?: boolean }, arg?: any): Promise<any> {
|
2022-11-29 16:57:11 -08:00
|
|
|
return js.evaluateExpression(this, expression, { ...options, returnByValue: true }, arg);
|
2021-07-09 16:19:42 +02:00
|
|
|
}
|
|
|
|
|
2024-05-31 14:44:26 -07:00
|
|
|
async evaluateExpressionHandle(expression: string, options: { isFunction?: boolean }, arg?: any): Promise<js.JSHandle<any>> {
|
2023-05-31 14:08:44 -07:00
|
|
|
return js.evaluateExpression(this, expression, { ...options, returnByValue: false }, arg);
|
2021-07-09 16:19:42 +02: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) {
|
2025-04-23 21:53:17 -07:00
|
|
|
const customEngines: InjectedScriptOptions['customEngines'] = [];
|
2025-04-30 18:57:59 -07:00
|
|
|
const selectorsRegistry = this.frame._page.browserContext.selectors();
|
2023-02-21 14:08:51 -08:00
|
|
|
for (const [name, { source }] of selectorsRegistry._engines)
|
2025-06-06 16:26:38 +02:00
|
|
|
customEngines.push({ name, source: `(${source})` });
|
2025-06-18 08:28:33 +01:00
|
|
|
const sdkLanguage = this.frame._page.browserContext._browser.sdkLanguage();
|
2025-04-23 21:53:17 -07:00
|
|
|
const options: InjectedScriptOptions = {
|
2025-05-21 21:10:41 +00:00
|
|
|
isUnderTest: isUnderTest(),
|
2025-04-23 21:53:17 -07:00
|
|
|
sdkLanguage,
|
|
|
|
testIdAttributeName: selectorsRegistry.testIdAttributeName(),
|
2025-04-30 18:57:59 -07:00
|
|
|
stableRafCount: this.frame._page.delegate.rafCountForStablePosition(),
|
|
|
|
browserName: this.frame._page.browserContext._browser.options.name,
|
2025-04-23 21:53:17 -07:00
|
|
|
customEngines,
|
|
|
|
};
|
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 = {};
|
2025-05-21 21:10:41 +00:00
|
|
|
${rawInjectedScriptSource.source}
|
2025-04-23 21:53:17 -07:00
|
|
|
return new (module.exports.InjectedScript())(globalThis, ${JSON.stringify(options)});
|
2021-01-08 16:15:05 -08:00
|
|
|
})();
|
2020-05-15 15:21:49 -07:00
|
|
|
`;
|
2025-02-24 12:11:17 -08:00
|
|
|
this._injectedScriptPromise = this.rawEvaluateHandle(source)
|
|
|
|
.then(handle => {
|
|
|
|
handle._setPreview('InjectedScript');
|
|
|
|
return handle;
|
|
|
|
});
|
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
|
|
|
|
2025-02-24 12:11:17 -08: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) {
|
2025-06-16 10:13:31 +01:00
|
|
|
if (isAbortError(e) || js.isJavaScriptErrorInEvaluate(e) || isSessionClosedError(e))
|
2021-09-17 22:18:00 -07:00
|
|
|
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) {
|
2025-06-16 10:13:31 +01:00
|
|
|
if (isAbortError(e) || js.isJavaScriptErrorInEvaluate(e) || isSessionClosedError(e))
|
2021-09-17 22:18:00 -07:00
|
|
|
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> {
|
2025-04-30 18:57:59 -07:00
|
|
|
const frameId = await this._page.delegate.getOwnerFrame(this);
|
2020-01-27 11:43:43 -08:00
|
|
|
if (!frameId)
|
|
|
|
return null;
|
2025-04-30 18:57:59 -07:00
|
|
|
const frame = this._page.frameManager.frame(frameId);
|
2020-04-20 13:01:06 -07:00
|
|
|
if (frame)
|
|
|
|
return frame;
|
2025-04-30 18:57:59 -07:00
|
|
|
for (const page of this._page.browserContext.pages()) {
|
|
|
|
const frame = page.frameManager.frame(frameId);
|
2020-01-27 11:43:43 -08:00
|
|
|
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;
|
2025-04-30 18:57:59 -07:00
|
|
|
return this._page.delegate.getContentFrame(this);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2025-01-27 14:49:38 -08:00
|
|
|
async generateLocatorString(): Promise<string | undefined> {
|
2025-05-26 12:32:58 -07:00
|
|
|
const selectors = await this._generateSelectorString();
|
|
|
|
if (!selectors.length)
|
|
|
|
return;
|
|
|
|
return asLocator('javascript', selectors.reverse().join(' >> internal:control=enter-frame >> '));
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _generateSelectorString(): Promise<string[]> {
|
2025-01-27 14:49:38 -08:00
|
|
|
const selector = await this.evaluateInUtility(async ([injected, node]) => {
|
|
|
|
return injected.generateSelectorSimple(node as unknown as Element);
|
|
|
|
}, {});
|
|
|
|
if (selector === 'error:notconnected')
|
2025-05-26 12:32:58 -07:00
|
|
|
return [];
|
|
|
|
|
|
|
|
let frame: frames.Frame | null = this._frame;
|
|
|
|
const result: string[] = [selector];
|
|
|
|
while (frame?.parentFrame()) {
|
|
|
|
const frameElement = await frame.frameElement();
|
|
|
|
if (frameElement) {
|
|
|
|
const selector = await frameElement.evaluateInUtility(async ([injected, node]) => {
|
|
|
|
return injected.generateSelectorSimple(node as unknown as Element);
|
|
|
|
}, {});
|
|
|
|
frameElement.dispose();
|
|
|
|
if (selector === 'error:notconnected')
|
|
|
|
return [];
|
|
|
|
result.push(selector);
|
|
|
|
}
|
|
|
|
frame = frame.parentFrame();
|
|
|
|
}
|
|
|
|
return result;
|
2025-01-27 14:49:38 -08:00
|
|
|
}
|
|
|
|
|
2023-06-09 07:18:13 -07:00
|
|
|
async getAttribute(metadata: CallMetadata, name: string): Promise<string | null> {
|
2025-05-20 14:51:33 +00:00
|
|
|
return this._frame.getAttribute(metadata, ':scope', name, { timeout: 0 }, this);
|
2020-04-09 16:49:23 -07:00
|
|
|
}
|
|
|
|
|
2023-06-09 07:18:13 -07:00
|
|
|
async inputValue(metadata: CallMetadata): Promise<string> {
|
2025-05-20 14:51:33 +00:00
|
|
|
return this._frame.inputValue(metadata, ':scope', { timeout: 0 }, this);
|
2023-06-09 07:18:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async textContent(metadata: CallMetadata): Promise<string | null> {
|
2025-05-20 14:51:33 +00:00
|
|
|
return this._frame.textContent(metadata, ':scope', { timeout: 0 }, this);
|
2023-06-09 07:18:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async innerText(metadata: CallMetadata): Promise<string> {
|
2025-05-20 14:51:33 +00:00
|
|
|
return this._frame.innerText(metadata, ':scope', { timeout: 0 }, this);
|
2023-06-09 07:18:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async innerHTML(metadata: CallMetadata): Promise<string> {
|
2025-05-20 14:51:33 +00:00
|
|
|
return this._frame.innerHTML(metadata, ':scope', { timeout: 0 }, this);
|
2023-06-09 07:18:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async dispatchEvent(metadata: CallMetadata, type: string, eventInit: Object = {}) {
|
2025-05-20 14:51:33 +00:00
|
|
|
return this._frame.dispatchEvent(metadata, ':scope', type, eventInit, { timeout: 0 }, this);
|
2020-04-23 14:58:37 -07:00
|
|
|
}
|
|
|
|
|
2025-06-16 10:13:31 +01:00
|
|
|
async _scrollRectIntoViewIfNeeded(progress: Progress, rect?: types.Rect): Promise<'error:notvisible' | 'error:notconnected' | 'done'> {
|
|
|
|
return await progress.race(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`);
|
2025-06-16 10:13:31 +01:00
|
|
|
const waitResult = await progress.race(this.evaluateInUtility(async ([injected, node, { waitForVisible }]) => {
|
2024-01-16 19:11:41 -08:00
|
|
|
return await injected.checkElementStates(node, waitForVisible ? ['visible', 'stable'] : ['stable']);
|
2025-06-16 10:13:31 +01:00
|
|
|
}, { waitForVisible }));
|
2024-01-16 19:11:41 -08:00
|
|
|
if (waitResult)
|
|
|
|
return waitResult;
|
2025-06-16 10:13:31 +01:00
|
|
|
return await this._scrollRectIntoViewIfNeeded(progress);
|
2024-01-16 19:11:41 -08:00
|
|
|
}, {});
|
|
|
|
assertDone(throwRetargetableDOMError(result));
|
2020-06-24 10:16:54 -07:00
|
|
|
}
|
|
|
|
|
2025-05-20 14:51:33 +00:00
|
|
|
async scrollIntoViewIfNeeded(metadata: CallMetadata, options: types.TimeoutOptions) {
|
2025-06-16 10:13:31 +01:00
|
|
|
const controller = new ProgressController(metadata, this, 'strict');
|
2021-02-09 14:44:48 -08:00
|
|
|
return controller.run(
|
2022-04-23 21:48:36 +01:00
|
|
|
progress => this._waitAndScrollIntoViewIfNeeded(progress, false /* waitForVisible */),
|
2025-05-20 14:51:33 +00:00
|
|
|
options.timeout);
|
2020-06-23 13:02:31 -07:00
|
|
|
}
|
|
|
|
|
2024-09-04 11:36:52 -07:00
|
|
|
private async _clickablePoint(): Promise<types.Point | 'error:notvisible' | 'error:notinviewport' | 'error:notconnected'> {
|
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([
|
2025-04-30 18:57:59 -07: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);
|
2024-09-04 11:36:52 -07:00
|
|
|
if (quads === 'error:notconnected')
|
|
|
|
return quads;
|
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';
|
2025-04-30 18:57:59 -07:00
|
|
|
if (this._page.browserContext._browser.options.name === 'firefox') {
|
2024-11-07 00:01:00 -08:00
|
|
|
// Firefox internally uses integer coordinates, so 8.x is converted to 8 or 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.x;8.y) will sometimes click at (9;9) and miss the target.
|
|
|
|
//
|
|
|
|
// Therefore, we try to find an integer point within a quad to make sure we click inside the element.
|
|
|
|
for (const quad of filtered) {
|
|
|
|
const integerPoint = findIntegerPointInsideQuad(quad);
|
|
|
|
if (integerPoint)
|
|
|
|
return integerPoint;
|
|
|
|
}
|
2019-12-05 09:54:50 -08:00
|
|
|
}
|
2024-11-07 00:01:00 -08:00
|
|
|
// Return the middle point of the first quad.
|
|
|
|
return quadMiddlePoint(filtered[0]);
|
2019-12-05 09:54:50 -08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2025-06-03 19:34:55 +01:00
|
|
|
async _retryAction(progress: Progress, actionName: string, action: () => Promise<PerformActionResult>, options: { trial?: boolean, force?: boolean, skipActionPreChecks?: 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];
|
|
|
|
|
2025-06-16 10:13:31 +01:00
|
|
|
while (true) {
|
|
|
|
progress.throwIfAborted();
|
2020-09-29 10:28:19 -07:00
|
|
|
if (retry) {
|
2024-11-01 13:38:01 -07:00
|
|
|
progress.log(`retrying ${actionName} action${options.trial ? ' (trial run)' : ''}`);
|
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`);
|
2025-06-16 10:13:31 +01:00
|
|
|
const result = await progress.race(this.evaluateInUtility(([injected, node, timeout]) => new Promise<void>(f => setTimeout(f, timeout)), timeout));
|
2021-09-17 22:18:00 -07:00
|
|
|
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-10-04 07:25:18 -07:00
|
|
|
if (!options.skipActionPreChecks && !options.force)
|
|
|
|
await this._frame._page.performActionPreChecks(progress);
|
2025-06-03 19:34:55 +01:00
|
|
|
const result = await action();
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-16 19:11:41 -08:00
|
|
|
async _retryPointerAction(progress: Progress, actionName: ActionName, waitForEnabled: boolean, action: (point: types.Point) => Promise<void>,
|
2025-06-16 10:13:31 +01:00
|
|
|
options: Omit<{ waitAfter: boolean | 'disabled' } & types.PointerActionOptions & types.PointerActionWaitOptions, 'timeout'>): Promise<'error:notconnected' | 'done'> {
|
2024-01-19 12:35:00 -08:00
|
|
|
// Note: do not perform locator handlers checkpoint to avoid moving the mouse in the middle of a drag operation.
|
2024-10-04 07:25:18 -07:00
|
|
|
const skipActionPreChecks = actionName === 'move and up';
|
2025-06-03 19:34:55 +01:00
|
|
|
// By default, we scroll with protocol method to reveal the action point.
|
|
|
|
// However, that might not work to scroll from under position:sticky and position:fixed 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' },
|
|
|
|
];
|
|
|
|
let scrollOptionIndex = 0;
|
|
|
|
return await this._retryAction(progress, actionName, async () => {
|
|
|
|
const forceScrollOptions = scrollOptions[scrollOptionIndex % scrollOptions.length];
|
|
|
|
const result = await this._performPointerAction(progress, actionName, waitForEnabled, action, forceScrollOptions, options);
|
|
|
|
if (typeof result === 'object' && 'hasPositionStickyOrFixed' in result && result.hasPositionStickyOrFixed)
|
|
|
|
++scrollOptionIndex;
|
|
|
|
else
|
|
|
|
scrollOptionIndex = 0;
|
|
|
|
return result;
|
2024-10-04 07:25:18 -07:00
|
|
|
}, { ...options, skipActionPreChecks });
|
2024-01-16 19:11:41 -08:00
|
|
|
}
|
|
|
|
|
2024-07-18 00:19:08 -07:00
|
|
|
async _performPointerAction(
|
|
|
|
progress: Progress,
|
|
|
|
actionName: ActionName,
|
|
|
|
waitForEnabled: boolean,
|
|
|
|
action: (point: types.Point) => Promise<void>,
|
|
|
|
forceScrollOptions: ScrollIntoViewOptions | undefined,
|
2025-06-16 10:13:31 +01:00
|
|
|
options: Omit<{ waitAfter: boolean | 'disabled' } & types.PointerActionOptions & types.PointerActionWaitOptions, 'timeout'>,
|
2024-07-18 00:19:08 -07:00
|
|
|
): 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);
|
|
|
|
}
|
2025-06-16 10:13:31 +01:00
|
|
|
return await this._scrollRectIntoViewIfNeeded(progress, position ? { x: position.x, y: position.y, width: 0, height: 0 } : undefined);
|
2023-10-02 17:21:06 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
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.
|
2025-06-16 10:13:31 +01:00
|
|
|
await progress.race(doScrollIntoView().catch(() => {}));
|
2023-10-02 17:21:06 -07:00
|
|
|
}
|
|
|
|
|
2020-06-26 16:31:51 -07:00
|
|
|
if ((options as any).__testHookBeforeStable)
|
2025-06-16 10:13:31 +01:00
|
|
|
await progress.race((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'}`);
|
2025-06-16 10:13:31 +01:00
|
|
|
const result = await progress.race(this.evaluateInUtility(async ([injected, node, { elementStates }]) => {
|
2024-01-16 19:11:41 -08:00
|
|
|
return await injected.checkElementStates(node, elementStates);
|
2025-06-16 10:13:31 +01:00
|
|
|
}, { elementStates }));
|
2024-01-16 19:11:41 -08:00
|
|
|
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)
|
2025-06-16 10:13:31 +01:00
|
|
|
await progress.race((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.
|
2025-06-16 10:13:31 +01:00
|
|
|
const scrolled = await progress.race(doScrollIntoView());
|
2023-10-02 17:21:06 -07:00
|
|
|
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
|
|
|
|
2025-06-16 10:13:31 +01:00
|
|
|
const maybePoint = position ? await progress.race(this._offsetPoint(position)) : await progress.race(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;
|
2025-06-16 10:13:31 +01:00
|
|
|
await progress.race(this.instrumentation.onBeforeInputAction(this, progress.metadata));
|
2021-11-05 17:31:28 -07:00
|
|
|
|
|
|
|
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)
|
2025-06-16 10:13:31 +01:00
|
|
|
await progress.race((options as any).__testHookBeforeHitTarget());
|
2021-11-05 17:31:28 -07:00
|
|
|
|
2025-06-16 10:13:31 +01:00
|
|
|
const frameCheckResult = await progress.race(this._checkFrameIsHitTarget(point));
|
2022-06-24 13:17:25 -07:00
|
|
|
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');
|
2025-06-16 10:13:31 +01:00
|
|
|
const handle = await progress.race(this.evaluateHandleInUtility(([injected, node, { actionType, hitPoint, trial }]) => injected.setupHitTargetInterceptor(node, actionType, hitPoint, trial), { actionType, hitPoint, trial: !!options.trial } as const));
|
2022-06-01 15:23:41 -07:00
|
|
|
if (handle === 'error:notconnected')
|
|
|
|
return handle;
|
|
|
|
if (!handle._objectId) {
|
|
|
|
const error = handle.rawValue() as string;
|
|
|
|
if (error === 'error:notconnected')
|
|
|
|
return error;
|
2025-06-03 19:34:55 +01:00
|
|
|
return JSON.parse(error) as HitTargetError; // It is safe to parse, because we evaluated in utility.
|
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
|
|
|
}
|
|
|
|
|
2025-04-30 18:57:59 -07:00
|
|
|
const actionResult = await this._page.frameManager.waitForSignalsCreatedBy(progress, options.waitAfter === true, async () => {
|
2021-11-05 17:31:28 -07:00
|
|
|
if ((options as any).__testHookBeforePointerAction)
|
2025-06-16 10:13:31 +01:00
|
|
|
await progress.race((options as any).__testHookBeforePointerAction());
|
2021-11-05 17:31:28 -07:00
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
|
|
|
let restoreModifiers: types.KeyboardModifier[] | undefined;
|
|
|
|
if (options && options.modifiers)
|
2025-06-12 10:12:42 +01:00
|
|
|
restoreModifiers = await this._page.keyboard.ensureModifiers(progress, options.modifiers);
|
2021-11-05 17:31:28 -07:00
|
|
|
progress.log(` performing ${actionName} action`);
|
|
|
|
await action(point);
|
|
|
|
if (restoreModifiers)
|
2025-06-12 10:12:42 +01:00
|
|
|
await this._page.keyboard.ensureModifiers(progress, restoreModifiers);
|
2021-11-05 17:31:28 -07:00
|
|
|
if (hitTargetInterceptionHandle) {
|
2024-07-18 00:19:08 -07:00
|
|
|
const stopHitTargetInterception = this._frame.raceAgainstEvaluationStallingEvents(() => {
|
|
|
|
return hitTargetInterceptionHandle.evaluate(h => h.stop());
|
|
|
|
}).catch(e => 'done' as const).finally(() => {
|
2023-03-31 18:18:45 -07:00
|
|
|
hitTargetInterceptionHandle?.dispose();
|
|
|
|
});
|
2024-07-18 00:19:08 -07:00
|
|
|
if (options.waitAfter !== false) {
|
2021-11-05 17:31:28 -07:00
|
|
|
// When noWaitAfter is passed, we do not want to accidentally stall on
|
|
|
|
// non-committed navigation blocking the evaluate.
|
2025-06-16 10:13:31 +01:00
|
|
|
const hitTargetResult = await progress.race(stopHitTargetInterception);
|
2021-11-05 17:31:28 -07:00
|
|
|
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)
|
2025-06-16 10:13:31 +01:00
|
|
|
await progress.race((options as any).__testHookAfterPointerAction());
|
2021-11-05 17:31:28 -07:00
|
|
|
return 'done';
|
2024-07-18 00:19:08 -07:00
|
|
|
});
|
2021-11-05 17:31:28 -07:00
|
|
|
if (actionResult !== 'done')
|
|
|
|
return actionResult;
|
|
|
|
progress.log(' navigations have finished');
|
|
|
|
return 'done';
|
|
|
|
}
|
|
|
|
|
2025-06-16 10:13:31 +01:00
|
|
|
private async _markAsTargetElement(progress: Progress) {
|
|
|
|
if (!progress.metadata.id)
|
2024-10-02 00:00:45 -07:00
|
|
|
return;
|
2025-06-16 10:13:31 +01:00
|
|
|
await progress.race(this.evaluateInUtility(([injected, node, callId]) => {
|
2024-10-02 00:00:45 -07:00
|
|
|
if (node.nodeType === 1 /* Node.ELEMENT_NODE */)
|
|
|
|
injected.markTargetElements(new Set([node as Node as Element]), callId);
|
2025-06-16 10:13:31 +01:00
|
|
|
}, progress.metadata.id));
|
2024-10-02 00:00:45 -07:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
async hover(metadata: CallMetadata, options: types.PointerActionOptions & types.PointerActionWaitOptions): Promise<void> {
|
2025-06-16 10:13:31 +01:00
|
|
|
const controller = new ProgressController(metadata, this, 'strict');
|
2020-09-17 09:32:54 -07:00
|
|
|
return controller.run(async progress => {
|
2025-06-16 10:13:31 +01:00
|
|
|
await this._markAsTargetElement(progress);
|
2020-09-17 09:32:54 -07:00
|
|
|
const result = await this._hover(progress, options);
|
|
|
|
return assertDone(throwRetargetableDOMError(result));
|
2025-05-20 14:51:33 +00:00
|
|
|
}, options.timeout);
|
2020-06-03 09:14:53 -07:00
|
|
|
}
|
|
|
|
|
2024-07-18 00:19:08 -07:00
|
|
|
_hover(progress: Progress, options: types.PointerActionOptions & types.PointerActionWaitOptions): Promise<'error:notconnected' | 'done'> {
|
2025-06-12 10:12:42 +01:00
|
|
|
return this._retryPointerAction(progress, 'hover', false /* waitForEnabled */, point => this._page.mouse._move(progress, point.x, point.y), { ...options, waitAfter: 'disabled' });
|
2020-06-03 09:14:53 -07:00
|
|
|
}
|
|
|
|
|
2025-05-20 14:51:33 +00:00
|
|
|
async click(metadata: CallMetadata, options: { noWaitAfter?: boolean } & types.MouseClickOptions & types.PointerActionWaitOptions): Promise<void> {
|
2025-06-16 10:13:31 +01:00
|
|
|
const controller = new ProgressController(metadata, this, 'strict');
|
2020-09-17 09:32:54 -07:00
|
|
|
return controller.run(async progress => {
|
2025-06-16 10:13:31 +01:00
|
|
|
await this._markAsTargetElement(progress);
|
2024-07-18 00:19:08 -07:00
|
|
|
const result = await this._click(progress, { ...options, waitAfter: !options.noWaitAfter });
|
2020-09-17 09:32:54 -07:00
|
|
|
return assertDone(throwRetargetableDOMError(result));
|
2025-05-20 14:51:33 +00:00
|
|
|
}, options.timeout);
|
2020-06-03 09:14:53 -07:00
|
|
|
}
|
|
|
|
|
2024-07-18 00:19:08 -07:00
|
|
|
_click(progress: Progress, options: { waitAfter: boolean | 'disabled' } & types.MouseClickOptions & types.PointerActionWaitOptions): Promise<'error:notconnected' | 'done'> {
|
2025-06-12 10:12:42 +01:00
|
|
|
return this._retryPointerAction(progress, 'click', true /* waitForEnabled */, point => this._page.mouse._click(progress, point.x, point.y, options), options);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2024-07-18 00:19:08 -07:00
|
|
|
async dblclick(metadata: CallMetadata, options: types.MouseMultiClickOptions & types.PointerActionWaitOptions): Promise<void> {
|
2025-06-16 10:13:31 +01:00
|
|
|
const controller = new ProgressController(metadata, this, 'strict');
|
2020-09-17 09:32:54 -07:00
|
|
|
return controller.run(async progress => {
|
2025-06-16 10:13:31 +01:00
|
|
|
await this._markAsTargetElement(progress);
|
2020-09-17 09:32:54 -07:00
|
|
|
const result = await this._dblclick(progress, options);
|
|
|
|
return assertDone(throwRetargetableDOMError(result));
|
2025-05-20 14:51:33 +00:00
|
|
|
}, options.timeout);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2024-07-18 00:19:08 -07:00
|
|
|
_dblclick(progress: Progress, options: types.MouseMultiClickOptions & types.PointerActionWaitOptions): Promise<'error:notconnected' | 'done'> {
|
2025-06-12 10:12:42 +01:00
|
|
|
return this._retryPointerAction(progress, 'dblclick', true /* waitForEnabled */, point => this._page.mouse._click(progress, point.x, point.y, { ...options, clickCount: 2 }), { ...options, waitAfter: 'disabled' });
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2025-05-20 14:51:33 +00:00
|
|
|
async tap(metadata: CallMetadata, options: types.PointerActionWaitOptions): Promise<void> {
|
2025-06-16 10:13:31 +01:00
|
|
|
const controller = new ProgressController(metadata, this, 'strict');
|
2020-10-19 10:07:33 -07:00
|
|
|
return controller.run(async progress => {
|
2025-06-16 10:13:31 +01:00
|
|
|
await this._markAsTargetElement(progress);
|
2020-10-19 10:07:33 -07:00
|
|
|
const result = await this._tap(progress, options);
|
|
|
|
return assertDone(throwRetargetableDOMError(result));
|
2025-05-20 14:51:33 +00:00
|
|
|
}, options.timeout);
|
2020-10-19 10:07:33 -07:00
|
|
|
}
|
|
|
|
|
2024-07-18 00:19:08 -07:00
|
|
|
_tap(progress: Progress, options: types.PointerActionWaitOptions): Promise<'error:notconnected' | 'done'> {
|
2025-06-12 10:12:42 +01:00
|
|
|
return this._retryPointerAction(progress, 'tap', true /* waitForEnabled */, point => this._page.touchscreen._tap(progress, point.x, point.y), { ...options, waitAfter: 'disabled' });
|
2020-10-19 10:07:33 -07:00
|
|
|
}
|
|
|
|
|
2024-08-23 14:50:43 -07:00
|
|
|
async selectOption(metadata: CallMetadata, elements: ElementHandle[], values: types.SelectOption[], options: types.CommonActionOptions): Promise<string[]> {
|
2025-06-16 10:13:31 +01:00
|
|
|
const controller = new ProgressController(metadata, this, 'strict');
|
2020-09-17 09:32:54 -07:00
|
|
|
return controller.run(async progress => {
|
2025-06-16 10:13:31 +01:00
|
|
|
await this._markAsTargetElement(progress);
|
2020-09-17 09:32:54 -07:00
|
|
|
const result = await this._selectOption(progress, elements, values, options);
|
|
|
|
return throwRetargetableDOMError(result);
|
2025-05-20 14:51:33 +00:00
|
|
|
}, options.timeout);
|
2020-06-03 11:23:24 -07:00
|
|
|
}
|
|
|
|
|
2024-08-23 14:50:43 -07:00
|
|
|
async _selectOption(progress: Progress, elements: ElementHandle[], values: types.SelectOption[], options: types.CommonActionOptions): Promise<string[] | 'error:notconnected'> {
|
2024-01-16 19:11:41 -08:00
|
|
|
let resultingOptions: string[] = [];
|
2025-04-14 16:51:08 -07:00
|
|
|
const result = await this._retryAction(progress, 'select option', async () => {
|
2025-06-16 10:13:31 +01:00
|
|
|
await progress.race(this.instrumentation.onBeforeInputAction(this, progress.metadata));
|
2024-01-16 19:11:41 -08:00
|
|
|
if (!options.force)
|
|
|
|
progress.log(` waiting for element to be visible and enabled`);
|
|
|
|
const optionsToSelect = [...elements, ...values];
|
2025-06-16 10:13:31 +01:00
|
|
|
const result = await progress.race(this.evaluateInUtility(async ([injected, node, { optionsToSelect, force }]) => {
|
2024-01-16 19:11:41 -08:00
|
|
|
if (!force) {
|
|
|
|
const checkResult = await injected.checkElementStates(node, ['visible', 'enabled']);
|
|
|
|
if (checkResult)
|
|
|
|
return checkResult;
|
|
|
|
}
|
|
|
|
return injected.selectOptions(node, optionsToSelect);
|
2025-06-16 10:13:31 +01: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);
|
2025-04-14 16:51:08 -07:00
|
|
|
if (result === 'error:notconnected')
|
|
|
|
return result;
|
2024-01-16 19:11:41 -08:00
|
|
|
return resultingOptions;
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2025-05-20 14:51:33 +00:00
|
|
|
async fill(metadata: CallMetadata, value: string, options: types.CommonActionOptions): Promise<void> {
|
2025-06-16 10:13:31 +01:00
|
|
|
const controller = new ProgressController(metadata, this, 'strict');
|
2020-09-17 09:32:54 -07:00
|
|
|
return controller.run(async progress => {
|
2025-06-16 10:13:31 +01:00
|
|
|
await this._markAsTargetElement(progress);
|
2020-09-17 09:32:54 -07:00
|
|
|
const result = await this._fill(progress, value, options);
|
|
|
|
assertDone(throwRetargetableDOMError(result));
|
2025-05-20 14:51:33 +00:00
|
|
|
}, options.timeout);
|
2020-06-03 09:14:53 -07:00
|
|
|
}
|
|
|
|
|
2024-07-18 00:19:08 -07:00
|
|
|
async _fill(progress: Progress, value: string, options: types.CommonActionOptions): Promise<'error:notconnected' | 'done'> {
|
2024-01-16 19:11:41 -08:00
|
|
|
progress.log(` fill("${value}")`);
|
|
|
|
return await this._retryAction(progress, 'fill', async () => {
|
2025-06-16 10:13:31 +01:00
|
|
|
await progress.race(this.instrumentation.onBeforeInputAction(this, progress.metadata));
|
2024-07-18 00:19:08 -07:00
|
|
|
if (!options.force)
|
|
|
|
progress.log(' waiting for element to be visible, enabled and editable');
|
2025-06-16 10:13:31 +01:00
|
|
|
const result = await progress.race(this.evaluateInUtility(async ([injected, node, { value, force }]) => {
|
2024-07-18 00:19:08 -07:00
|
|
|
if (!force) {
|
|
|
|
const checkResult = await injected.checkElementStates(node, ['visible', 'enabled', 'editable']);
|
|
|
|
if (checkResult)
|
|
|
|
return checkResult;
|
2024-01-16 19:11:41 -08:00
|
|
|
}
|
2024-07-18 00:19:08 -07:00
|
|
|
return injected.fill(node, value);
|
2025-06-16 10:13:31 +01:00
|
|
|
}, { value, force: options.force }));
|
2024-07-18 00:19:08 -07:00
|
|
|
progress.throwIfAborted(); // Avoid action that has side-effects.
|
|
|
|
if (result === 'needsinput') {
|
|
|
|
if (value)
|
2025-06-12 10:12:42 +01:00
|
|
|
await this._page.keyboard._insertText(progress, value);
|
2024-07-18 00:19:08 -07:00
|
|
|
else
|
2025-06-12 10:12:42 +01:00
|
|
|
await this._page.keyboard._press(progress, 'Delete');
|
2024-07-18 00:19:08 -07:00
|
|
|
return 'done';
|
|
|
|
} else {
|
|
|
|
return result;
|
|
|
|
}
|
2024-01-16 19:11:41 -08:00
|
|
|
}, options);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2025-05-20 14:51:33 +00:00
|
|
|
async selectText(metadata: CallMetadata, options: types.CommonActionOptions): Promise<void> {
|
2025-06-16 10:13:31 +01:00
|
|
|
const controller = new ProgressController(metadata, this, 'strict');
|
2021-02-09 14:44:48 -08:00
|
|
|
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');
|
2025-06-16 10:13:31 +01:00
|
|
|
return await progress.race(this.evaluateInUtility(async ([injected, node, { force }]) => {
|
2024-01-16 19:11:41 -08:00
|
|
|
if (!force) {
|
|
|
|
const checkResult = await injected.checkElementStates(node, ['visible']);
|
|
|
|
if (checkResult)
|
|
|
|
return checkResult;
|
|
|
|
}
|
|
|
|
return injected.selectText(node);
|
2025-06-16 10:13:31 +01:00
|
|
|
}, { force: options.force }));
|
2024-01-16 19:11:41 -08:00
|
|
|
}, options);
|
2020-06-24 15:12:17 -07:00
|
|
|
assertDone(throwRetargetableDOMError(result));
|
2025-05-20 14:51:33 +00:00
|
|
|
}, options.timeout);
|
2020-04-14 17:09:26 -07:00
|
|
|
}
|
|
|
|
|
2023-11-10 15:24:31 -08:00
|
|
|
async setInputFiles(metadata: CallMetadata, params: channels.ElementHandleSetInputFilesParams) {
|
2025-06-16 10:13:31 +01:00
|
|
|
const controller = new ProgressController(metadata, this, 'strict');
|
2020-09-17 09:32:54 -07:00
|
|
|
return controller.run(async progress => {
|
2025-06-16 10:13:31 +01:00
|
|
|
const inputFileItems = await progress.race(prepareFilesForUpload(this._frame, params));
|
|
|
|
await this._markAsTargetElement(progress);
|
2024-07-18 00:19:08 -07:00
|
|
|
const result = await this._setInputFiles(progress, inputFileItems);
|
2020-09-17 09:32:54 -07:00
|
|
|
return assertDone(throwRetargetableDOMError(result));
|
2025-05-20 14:51:33 +00:00
|
|
|
}, params.timeout);
|
2020-06-03 11:23:24 -07:00
|
|
|
}
|
|
|
|
|
2024-07-18 00:19:08 -07:00
|
|
|
async _setInputFiles(progress: Progress, items: InputFilesItems): Promise<'error:notconnected' | 'done'> {
|
2024-06-12 22:20:18 +02:00
|
|
|
const { filePayloads, localPaths, localDirectory } = items;
|
2023-11-10 15:24:31 -08:00
|
|
|
const multiple = filePayloads && filePayloads.length > 1 || localPaths && localPaths.length > 1;
|
2025-06-16 10:13:31 +01:00
|
|
|
const result = await progress.race(this.evaluateHandleInUtility(([injected, node, { multiple, directoryUpload }]): 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');
|
2024-06-12 22:20:18 +02:00
|
|
|
const inputElement = element as HTMLInputElement;
|
|
|
|
if (multiple && !inputElement.multiple && !inputElement.webkitdirectory)
|
2021-09-24 20:51:09 -07:00
|
|
|
throw injected.createStacklessError('Non-multiple file input can only accept single file');
|
2024-06-12 22:20:18 +02:00
|
|
|
if (directoryUpload && !inputElement.webkitdirectory)
|
|
|
|
throw injected.createStacklessError('File input does not support directories, pass individual files instead');
|
2024-07-16 15:55:35 +02:00
|
|
|
if (!directoryUpload && inputElement.webkitdirectory)
|
|
|
|
throw injected.createStacklessError('[webkitdirectory] input requires passing a path to a directory');
|
2024-06-12 22:20:18 +02:00
|
|
|
return inputElement;
|
2025-06-16 10:13:31 +01:00
|
|
|
}, { multiple, directoryUpload: !!localDirectory }));
|
2021-11-01 15:59:47 -07:00
|
|
|
if (result === 'error:notconnected' || !result.asElement())
|
|
|
|
return 'error:notconnected';
|
|
|
|
const retargeted = result.asElement() as ElementHandle<HTMLInputElement>;
|
2025-06-16 10:13:31 +01:00
|
|
|
await progress.race(this.instrumentation.onBeforeInputAction(this, progress.metadata));
|
2024-07-18 00:19:08 -07:00
|
|
|
if (localPaths || localDirectory) {
|
|
|
|
const localPathsOrDirectory = localDirectory ? [localDirectory] : localPaths!;
|
2025-06-16 10:13:31 +01:00
|
|
|
await progress.race(Promise.all((localPathsOrDirectory).map(localPath => (
|
2024-07-18 00:19:08 -07:00
|
|
|
fs.promises.access(localPath, fs.constants.F_OK)
|
2025-06-16 10:13:31 +01:00
|
|
|
))));
|
2024-07-18 00:19:08 -07:00
|
|
|
// Browsers traverse the given directory asynchronously and we want to ensure all files are uploaded.
|
|
|
|
const waitForInputEvent = localDirectory ? this.evaluate(node => new Promise<any>(fulfill => {
|
|
|
|
node.addEventListener('input', fulfill, { once: true });
|
|
|
|
})).catch(() => {}) : Promise.resolve();
|
2025-06-16 10:13:31 +01:00
|
|
|
await progress.race(this._page.delegate.setInputFilePaths(retargeted, localPathsOrDirectory));
|
|
|
|
await progress.race(waitForInputEvent);
|
2024-07-18 00:19:08 -07:00
|
|
|
} else {
|
2025-06-16 10:13:31 +01:00
|
|
|
await progress.race(retargeted.evaluateInUtility(([injected, node, files]) =>
|
|
|
|
injected.setInputFiles(node, files), filePayloads!));
|
2024-07-18 00:19:08 -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> {
|
2025-06-16 10:13:31 +01:00
|
|
|
const controller = new ProgressController(metadata, this, 'strict');
|
2021-02-09 14:44:48 -08:00
|
|
|
await controller.run(async progress => {
|
2025-06-16 10:13:31 +01:00
|
|
|
await this._markAsTargetElement(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'> {
|
2025-06-16 10:13:31 +01:00
|
|
|
return await progress.race(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'> {
|
2025-06-16 10:13:31 +01:00
|
|
|
return await progress.race(this.evaluateInUtility(([injected, node]) => injected.blurNode(node), {}));
|
2022-10-25 06:10:40 -07:00
|
|
|
}
|
|
|
|
|
2024-07-18 00:19:08 -07:00
|
|
|
async type(metadata: CallMetadata, text: string, options: { delay?: number } & types.TimeoutOptions & types.StrictOptions): Promise<void> {
|
2025-06-16 10:13:31 +01:00
|
|
|
const controller = new ProgressController(metadata, this, 'strict');
|
2020-09-17 09:32:54 -07:00
|
|
|
return controller.run(async progress => {
|
2025-06-16 10:13:31 +01:00
|
|
|
await this._markAsTargetElement(progress);
|
2020-09-17 09:32:54 -07:00
|
|
|
const result = await this._type(progress, text, options);
|
|
|
|
return assertDone(throwRetargetableDOMError(result));
|
2025-05-20 14:51:33 +00:00
|
|
|
}, options.timeout);
|
2020-06-03 11:23:24 -07:00
|
|
|
}
|
|
|
|
|
2024-07-18 00:19:08 -07:00
|
|
|
async _type(progress: Progress, text: string, options: { delay?: number } & types.TimeoutOptions & types.StrictOptions): Promise<'error:notconnected' | 'done'> {
|
2020-08-17 14:12:31 -07:00
|
|
|
progress.log(`elementHandle.type("${text}")`);
|
2025-06-16 10:13:31 +01:00
|
|
|
await progress.race(this.instrumentation.onBeforeInputAction(this, progress.metadata));
|
2024-07-18 00:19:08 -07:00
|
|
|
const result = await this._focus(progress, true /* resetSelectionIfNotFocused */);
|
|
|
|
if (result !== 'done')
|
|
|
|
return result;
|
2025-06-12 10:12:42 +01:00
|
|
|
await this._page.keyboard._type(progress, text, options);
|
2024-07-18 00:19:08 -07:00
|
|
|
return 'done';
|
2020-06-03 11:23:24 -07:00
|
|
|
}
|
|
|
|
|
2024-07-18 00:19:08 -07:00
|
|
|
async press(metadata: CallMetadata, key: string, options: { delay?: number, noWaitAfter?: boolean } & types.TimeoutOptions & types.StrictOptions): Promise<void> {
|
2025-06-16 10:13:31 +01:00
|
|
|
const controller = new ProgressController(metadata, this, 'strict');
|
2020-09-17 09:32:54 -07:00
|
|
|
return controller.run(async progress => {
|
2025-06-16 10:13:31 +01:00
|
|
|
await this._markAsTargetElement(progress);
|
2020-09-17 09:32:54 -07:00
|
|
|
const result = await this._press(progress, key, options);
|
|
|
|
return assertDone(throwRetargetableDOMError(result));
|
2025-05-20 14:51:33 +00:00
|
|
|
}, options.timeout);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2024-07-18 00:19:08 -07:00
|
|
|
async _press(progress: Progress, key: string, options: { delay?: number, noWaitAfter?: boolean } & types.TimeoutOptions & types.StrictOptions): Promise<'error:notconnected' | 'done'> {
|
2020-08-17 14:12:31 -07:00
|
|
|
progress.log(`elementHandle.press("${key}")`);
|
2025-06-16 10:13:31 +01:00
|
|
|
await progress.race(this.instrumentation.onBeforeInputAction(this, progress.metadata));
|
2025-04-30 18:57:59 -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;
|
2025-06-12 10:12:42 +01:00
|
|
|
await this._page.keyboard._press(progress, key, options);
|
2020-06-12 14:59:26 -07:00
|
|
|
return 'done';
|
2024-07-18 00:19:08 -07:00
|
|
|
});
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
2020-02-19 09:34:57 -08:00
|
|
|
|
2024-07-18 00:19:08 -07:00
|
|
|
async check(metadata: CallMetadata, options: { position?: types.Point } & types.PointerActionWaitOptions) {
|
2025-06-16 10:13:31 +01:00
|
|
|
const controller = new ProgressController(metadata, this, 'strict');
|
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));
|
2025-05-20 14:51:33 +00:00
|
|
|
}, options.timeout);
|
2020-02-04 14:39:11 -08:00
|
|
|
}
|
|
|
|
|
2024-07-18 00:19:08 -07:00
|
|
|
async uncheck(metadata: CallMetadata, options: { position?: types.Point } & types.PointerActionWaitOptions) {
|
2025-06-16 10:13:31 +01:00
|
|
|
const controller = new ProgressController(metadata, this, 'strict');
|
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));
|
2025-05-20 14:51:33 +00:00
|
|
|
}, options.timeout);
|
2020-02-04 14:39:11 -08:00
|
|
|
}
|
|
|
|
|
2024-07-18 00:19:08 -07:00
|
|
|
async _setChecked(progress: Progress, state: boolean, options: { position?: types.Point } & types.PointerActionWaitOptions): Promise<'error:notconnected' | 'done'> {
|
2021-02-10 12:36:26 -08:00
|
|
|
const isChecked = async () => {
|
2025-06-16 10:13:31 +01:00
|
|
|
const result = await progress.race(this.evaluateInUtility(([injected, node]) => injected.elementState(node, 'checked'), {}));
|
2025-01-07 15:31:18 -08:00
|
|
|
if (result === 'error:notconnected' || result.received === 'error:notconnected')
|
|
|
|
throwElementIsNotAttached();
|
|
|
|
return result.matches;
|
2021-02-10 12:36:26 -08:00
|
|
|
};
|
2025-06-16 10:13:31 +01:00
|
|
|
await this._markAsTargetElement(progress);
|
2021-02-10 12:36:26 -08:00
|
|
|
if (await isChecked() === state)
|
2020-06-24 15:12:17 -07:00
|
|
|
return 'done';
|
2024-07-18 00:19:08 -07:00
|
|
|
const result = await this._click(progress, { ...options, waitAfter: 'disabled' });
|
2020-06-24 15:12:17 -07:00
|
|
|
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> {
|
2025-04-30 18:57:59 -07:00
|
|
|
return this._page.delegate.getBoundingBox(this);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2025-05-09 16:15:16 -07:00
|
|
|
async ariaSnapshot(options?: { forAI?: boolean, refPrefix?: string }): Promise<string> {
|
2025-01-27 14:49:38 -08:00
|
|
|
return await this.evaluateInUtility(([injected, element, options]) => injected.ariaSnapshot(element, options), options);
|
2024-10-15 18:47:26 -07:00
|
|
|
}
|
|
|
|
|
2025-05-20 14:51:33 +00:00
|
|
|
async screenshot(metadata: CallMetadata, options: ScreenshotOptions & types.TimeoutOptions): Promise<Buffer> {
|
2021-02-09 14:44:48 -08:00
|
|
|
const controller = new ProgressController(metadata, this);
|
|
|
|
return controller.run(
|
2025-04-30 18:57:59 -07:00
|
|
|
progress => this._page.screenshotter.screenshotElement(progress, this, options),
|
2025-05-20 14:51:33 +00:00
|
|
|
options.timeout);
|
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> {
|
2025-05-20 14:51:33 +00:00
|
|
|
return this._frame.isEnabled(metadata, ':scope', { timeout: 0 }, this);
|
2021-01-08 12:27:54 -08:00
|
|
|
}
|
|
|
|
|
2023-06-09 07:18:13 -07:00
|
|
|
async isDisabled(metadata: CallMetadata): Promise<boolean> {
|
2025-05-20 14:51:33 +00:00
|
|
|
return this._frame.isDisabled(metadata, ':scope', { timeout: 0 }, this);
|
2021-01-08 12:27:54 -08:00
|
|
|
}
|
|
|
|
|
2023-06-09 07:18:13 -07:00
|
|
|
async isEditable(metadata: CallMetadata): Promise<boolean> {
|
2025-05-20 14:51:33 +00:00
|
|
|
return this._frame.isEditable(metadata, ':scope', { timeout: 0 }, this);
|
2021-01-08 12:27:54 -08:00
|
|
|
}
|
|
|
|
|
2023-06-09 07:18:13 -07:00
|
|
|
async isChecked(metadata: CallMetadata): Promise<boolean> {
|
2025-05-20 14:51:33 +00:00
|
|
|
return this._frame.isChecked(metadata, ':scope', { timeout: 0 }, this);
|
2021-01-08 17:36:17 -08:00
|
|
|
}
|
|
|
|
|
2025-05-20 14:51:33 +00:00
|
|
|
async waitForElementState(metadata: CallMetadata, state: 'visible' | 'hidden' | 'stable' | 'enabled' | 'disabled' | 'editable', options: types.TimeoutOptions): Promise<void> {
|
2025-06-16 10:13:31 +01:00
|
|
|
const controller = new ProgressController(metadata, this, 'strict');
|
2021-02-09 14:44:48 -08:00
|
|
|
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 () => {
|
2025-06-16 10:13:31 +01:00
|
|
|
return await progress.race(this.evaluateInUtility(async ([injected, node, state]) => {
|
2024-01-16 19:11:41 -08:00
|
|
|
return (await injected.checkElementStates(node, [state])) || 'done';
|
2025-06-16 10:13:31 +01:00
|
|
|
}, state));
|
2024-01-16 19:11:41 -08:00
|
|
|
}, {});
|
2021-11-01 15:59:47 -07:00
|
|
|
assertDone(throwRetargetableDOMError(result));
|
2025-05-20 14:51:33 +00:00
|
|
|
}, options.timeout);
|
2020-08-17 16:22:34 -07:00
|
|
|
}
|
|
|
|
|
2025-05-20 14:51:33 +00: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) {
|
2025-04-30 18:57:59 -07:00
|
|
|
const adopted = await this._page.delegate.adoptElementHandle(this, context);
|
2020-08-14 14:47:24 -07:00
|
|
|
this.dispose();
|
|
|
|
return adopted;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2025-06-03 19:34:55 +01:00
|
|
|
async _checkFrameIsHitTarget(point: types.Point): Promise<{ framePoint: types.Point | undefined } | 'error:notconnected' | HitTargetError> {
|
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')
|
2025-01-07 15:31:18 -08:00
|
|
|
throwElementIsNotAttached();
|
2020-06-12 14:59:26 -07:00
|
|
|
return result;
|
2020-04-18 18:29:31 -07:00
|
|
|
}
|
2020-04-29 11:05:23 -07:00
|
|
|
|
2025-01-07 15:31:18 -08:00
|
|
|
export function throwElementIsNotAttached(): never {
|
|
|
|
throw new Error('Element is not attached to the DOM');
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2024-11-07 00:01:00 -08:00
|
|
|
function quadMiddlePoint(quad: types.Quad): types.Point {
|
|
|
|
const result = { x: 0, y: 0 };
|
|
|
|
for (const point of quad) {
|
|
|
|
result.x += point.x / 4;
|
|
|
|
result.y += point.y / 4;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function triangleArea(p1: types.Point, p2: types.Point, p3: types.Point): number {
|
|
|
|
return Math.abs(p1.x * (p2.y - p3.y) + p2.x * (p3.y - p1.y) + p3.x * (p1.y - p2.y)) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isPointInsideQuad(point: types.Point, quad: types.Quad): boolean {
|
|
|
|
const area1 = triangleArea(point, quad[0], quad[1]) + triangleArea(point, quad[1], quad[2]) + triangleArea(point, quad[2], quad[3]) + triangleArea(point, quad[3], quad[0]);
|
|
|
|
const area2 = triangleArea(quad[0], quad[1], quad[2]) + triangleArea(quad[1], quad[2], quad[3]);
|
|
|
|
// Check that point is inside the quad.
|
|
|
|
if (Math.abs(area1 - area2) > 0.1)
|
|
|
|
return false;
|
|
|
|
// Check that point is not on the right/bottom edge, because clicking
|
|
|
|
// there does not actually click the element.
|
|
|
|
return point.x < Math.max(quad[0].x, quad[1].x, quad[2].x, quad[3].x) &&
|
|
|
|
point.y < Math.max(quad[0].y, quad[1].y, quad[2].y, quad[3].y);
|
|
|
|
}
|
|
|
|
|
|
|
|
function findIntegerPointInsideQuad(quad: types.Quad): types.Point | undefined {
|
|
|
|
// Try all four rounding directions of the middle point.
|
|
|
|
const point = quadMiddlePoint(quad);
|
|
|
|
point.x = Math.floor(point.x);
|
|
|
|
point.y = Math.floor(point.y);
|
|
|
|
if (isPointInsideQuad(point, quad))
|
|
|
|
return point;
|
|
|
|
point.x += 1;
|
|
|
|
if (isPointInsideQuad(point, quad))
|
|
|
|
return point;
|
|
|
|
point.y += 1;
|
|
|
|
if (isPointInsideQuad(point, quad))
|
|
|
|
return point;
|
|
|
|
point.x -= 1;
|
|
|
|
if (isPointInsideQuad(point, quad))
|
|
|
|
return point;
|
2020-08-20 16:49:19 -07:00
|
|
|
}
|
|
|
|
|
2021-06-02 20:17:24 -07:00
|
|
|
export const kUnableToAdoptErrorMessage = 'Unable to adopt element handle from a different document';
|