2022-09-19 10:49:13 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2019 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-06-23 09:31:43 +00:00
|
|
|
import {Protocol} from 'devtools-protocol';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2023-08-24 18:32:29 +00:00
|
|
|
import {AutofillData, ElementHandle, Point} from '../api/ElementHandle.js';
|
2023-02-13 19:25:07 +00:00
|
|
|
import {Page, ScreenshotOptions} from '../api/Page.js';
|
2022-08-17 12:39:41 +00:00
|
|
|
import {assert} from '../util/assert.js';
|
2023-08-31 12:04:40 +00:00
|
|
|
import {throwIfDisposed} from '../util/decorators.js';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2023-02-13 19:25:07 +00:00
|
|
|
import {CDPSession} from './Connection.js';
|
2022-06-23 09:31:43 +00:00
|
|
|
import {ExecutionContext} from './ExecutionContext.js';
|
2023-08-30 12:20:25 +00:00
|
|
|
import {CDPFrame} from './Frame.js';
|
2022-08-25 15:00:35 +00:00
|
|
|
import {FrameManager} from './FrameManager.js';
|
2022-08-26 10:55:30 +00:00
|
|
|
import {WaitForSelectorOptions} from './IsolatedWorld.js';
|
2023-02-15 23:09:31 +00:00
|
|
|
import {CDPJSHandle} from './JSHandle.js';
|
2023-06-12 09:32:19 +00:00
|
|
|
import {NodeFor} from './types.js';
|
|
|
|
import {debugError} from './util.js';
|
2022-06-23 09:31:43 +00:00
|
|
|
|
|
|
|
/**
|
2023-02-09 17:04:06 +00:00
|
|
|
* The CDPElementHandle extends ElementHandle now to keep compatibility
|
|
|
|
* with `instanceof` because of that we need to have methods for
|
|
|
|
* CDPJSHandle to in this implementation as well.
|
2022-06-23 09:31:43 +00:00
|
|
|
*
|
2023-02-09 17:04:06 +00:00
|
|
|
* @internal
|
2022-06-23 09:31:43 +00:00
|
|
|
*/
|
2023-02-09 17:04:06 +00:00
|
|
|
export class CDPElementHandle<
|
2023-07-17 08:52:54 +00:00
|
|
|
ElementType extends Node = Element,
|
2023-02-09 17:04:06 +00:00
|
|
|
> extends ElementHandle<ElementType> {
|
2023-08-30 12:20:25 +00:00
|
|
|
#frame: CDPFrame;
|
2023-03-01 10:09:17 +00:00
|
|
|
declare handle: CDPJSHandle<ElementType>;
|
2022-06-23 09:31:43 +00:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
context: ExecutionContext,
|
|
|
|
remoteObject: Protocol.Runtime.RemoteObject,
|
2023-08-30 12:20:25 +00:00
|
|
|
frame: CDPFrame
|
2022-06-23 09:31:43 +00:00
|
|
|
) {
|
2023-03-01 10:09:17 +00:00
|
|
|
super(new CDPJSHandle(context, remoteObject));
|
2022-06-23 09:31:43 +00:00
|
|
|
this.#frame = frame;
|
2022-08-25 15:01:30 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 16:00:34 +00:00
|
|
|
executionContext(): ExecutionContext {
|
2023-03-01 10:09:17 +00:00
|
|
|
return this.handle.executionContext();
|
2023-02-09 17:04:06 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 16:00:34 +00:00
|
|
|
get client(): CDPSession {
|
2023-03-01 10:09:17 +00:00
|
|
|
return this.handle.client;
|
2023-02-09 17:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override remoteObject(): Protocol.Runtime.RemoteObject {
|
2023-03-01 10:09:17 +00:00
|
|
|
return this.handle.remoteObject();
|
2023-02-09 17:04:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 15:01:30 +00:00
|
|
|
get #frameManager(): FrameManager {
|
|
|
|
return this.#frame._frameManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
get #page(): Page {
|
|
|
|
return this.#frame.page();
|
2022-06-23 09:31:43 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 12:20:25 +00:00
|
|
|
override get frame(): CDPFrame {
|
2022-08-25 15:38:02 +00:00
|
|
|
return this.#frame;
|
|
|
|
}
|
|
|
|
|
2023-08-31 12:04:40 +00:00
|
|
|
@throwIfDisposed()
|
2023-02-09 17:04:06 +00:00
|
|
|
override async $<Selector extends string>(
|
2022-08-10 21:34:29 +00:00
|
|
|
selector: Selector
|
2023-02-09 17:04:06 +00:00
|
|
|
): Promise<CDPElementHandle<NodeFor<Selector>> | null> {
|
2023-06-06 12:39:54 +00:00
|
|
|
return super.$(selector) as Promise<CDPElementHandle<
|
|
|
|
NodeFor<Selector>
|
|
|
|
> | null>;
|
2022-08-10 21:34:29 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 12:04:40 +00:00
|
|
|
@throwIfDisposed()
|
2023-02-09 17:04:06 +00:00
|
|
|
override async $$<Selector extends string>(
|
2022-08-10 21:34:29 +00:00
|
|
|
selector: Selector
|
2023-02-09 17:04:06 +00:00
|
|
|
): Promise<Array<CDPElementHandle<NodeFor<Selector>>>> {
|
2023-06-06 12:39:54 +00:00
|
|
|
return super.$$(selector) as Promise<
|
|
|
|
Array<CDPElementHandle<NodeFor<Selector>>>
|
|
|
|
>;
|
2022-08-10 21:34:29 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 12:04:40 +00:00
|
|
|
@throwIfDisposed()
|
2023-02-09 17:04:06 +00:00
|
|
|
override async waitForSelector<Selector extends string>(
|
2022-06-23 09:31:43 +00:00
|
|
|
selector: Selector,
|
2023-06-12 09:32:19 +00:00
|
|
|
options?: WaitForSelectorOptions
|
2023-02-09 17:04:06 +00:00
|
|
|
): Promise<CDPElementHandle<NodeFor<Selector>> | null> {
|
2023-06-12 09:32:19 +00:00
|
|
|
return (await super.waitForSelector(selector, options)) as CDPElementHandle<
|
|
|
|
NodeFor<Selector>
|
|
|
|
> | null;
|
2022-06-23 09:31:43 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:58:18 +00:00
|
|
|
override async contentFrame(
|
|
|
|
this: ElementHandle<HTMLIFrameElement>
|
2023-08-30 12:20:25 +00:00
|
|
|
): Promise<CDPFrame>;
|
2023-08-31 12:04:40 +00:00
|
|
|
@throwIfDisposed()
|
2023-08-30 12:20:25 +00:00
|
|
|
override async contentFrame(): Promise<CDPFrame | null> {
|
2022-08-11 09:45:35 +00:00
|
|
|
const nodeInfo = await this.client.send('DOM.describeNode', {
|
2023-05-15 14:39:47 +00:00
|
|
|
objectId: this.id,
|
2022-06-23 09:31:43 +00:00
|
|
|
});
|
|
|
|
if (typeof nodeInfo.node.frameId !== 'string') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return this.#frameManager.frame(nodeInfo.node.frameId);
|
|
|
|
}
|
|
|
|
|
2023-08-31 12:04:40 +00:00
|
|
|
@throwIfDisposed()
|
2023-04-12 05:17:18 +00:00
|
|
|
override async scrollIntoView(
|
2023-02-09 17:04:06 +00:00
|
|
|
this: CDPElementHandle<Element>
|
|
|
|
): Promise<void> {
|
2023-04-12 05:17:18 +00:00
|
|
|
await this.assertConnectedElement();
|
2022-07-08 06:53:45 +00:00
|
|
|
try {
|
2022-08-11 09:45:35 +00:00
|
|
|
await this.client.send('DOM.scrollIntoViewIfNeeded', {
|
2023-05-15 14:39:47 +00:00
|
|
|
objectId: this.id,
|
2022-07-08 06:53:45 +00:00
|
|
|
});
|
2023-04-12 05:17:18 +00:00
|
|
|
} catch (error) {
|
|
|
|
debugError(error);
|
2022-07-08 06:53:45 +00:00
|
|
|
// Fallback to Element.scrollIntoView if DOM.scrollIntoViewIfNeeded is not supported
|
2023-06-12 09:32:19 +00:00
|
|
|
await super.scrollIntoView();
|
2023-04-12 05:17:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-23 09:31:43 +00:00
|
|
|
/**
|
|
|
|
* This method creates and captures a dragevent from the element.
|
|
|
|
*/
|
2023-08-31 12:04:40 +00:00
|
|
|
@throwIfDisposed()
|
2023-02-09 17:04:06 +00:00
|
|
|
override async drag(
|
|
|
|
this: CDPElementHandle<Element>,
|
2022-07-06 07:05:37 +00:00
|
|
|
target: Point
|
|
|
|
): Promise<Protocol.Input.DragData> {
|
2022-06-23 09:31:43 +00:00
|
|
|
assert(
|
|
|
|
this.#page.isDragInterceptionEnabled(),
|
|
|
|
'Drag Interception is not enabled!'
|
|
|
|
);
|
2023-06-19 11:26:30 +00:00
|
|
|
await this.scrollIntoViewIfNeeded();
|
2022-06-23 09:31:43 +00:00
|
|
|
const start = await this.clickablePoint();
|
|
|
|
return await this.#page.mouse.drag(start, target);
|
|
|
|
}
|
|
|
|
|
2023-08-31 12:04:40 +00:00
|
|
|
@throwIfDisposed()
|
2023-02-09 17:04:06 +00:00
|
|
|
override async dragEnter(
|
|
|
|
this: CDPElementHandle<Element>,
|
2022-06-23 09:31:43 +00:00
|
|
|
data: Protocol.Input.DragData = {items: [], dragOperationsMask: 1}
|
|
|
|
): Promise<void> {
|
2023-06-19 11:26:30 +00:00
|
|
|
await this.scrollIntoViewIfNeeded();
|
2022-06-23 09:31:43 +00:00
|
|
|
const target = await this.clickablePoint();
|
|
|
|
await this.#page.mouse.dragEnter(target, data);
|
|
|
|
}
|
|
|
|
|
2023-08-31 12:04:40 +00:00
|
|
|
@throwIfDisposed()
|
2023-02-09 17:04:06 +00:00
|
|
|
override async dragOver(
|
|
|
|
this: CDPElementHandle<Element>,
|
2022-06-23 09:31:43 +00:00
|
|
|
data: Protocol.Input.DragData = {items: [], dragOperationsMask: 1}
|
|
|
|
): Promise<void> {
|
2023-06-19 11:26:30 +00:00
|
|
|
await this.scrollIntoViewIfNeeded();
|
2022-06-23 09:31:43 +00:00
|
|
|
const target = await this.clickablePoint();
|
|
|
|
await this.#page.mouse.dragOver(target, data);
|
|
|
|
}
|
|
|
|
|
2023-08-31 12:04:40 +00:00
|
|
|
@throwIfDisposed()
|
2023-02-09 17:04:06 +00:00
|
|
|
override async drop(
|
|
|
|
this: CDPElementHandle<Element>,
|
2022-06-23 09:31:43 +00:00
|
|
|
data: Protocol.Input.DragData = {items: [], dragOperationsMask: 1}
|
|
|
|
): Promise<void> {
|
2023-06-19 11:26:30 +00:00
|
|
|
await this.scrollIntoViewIfNeeded();
|
2022-06-23 09:31:43 +00:00
|
|
|
const destination = await this.clickablePoint();
|
|
|
|
await this.#page.mouse.drop(destination, data);
|
|
|
|
}
|
|
|
|
|
2023-08-31 12:04:40 +00:00
|
|
|
@throwIfDisposed()
|
2023-02-09 17:04:06 +00:00
|
|
|
override async dragAndDrop(
|
|
|
|
this: CDPElementHandle<Element>,
|
|
|
|
target: CDPElementHandle<Node>,
|
2022-06-23 09:31:43 +00:00
|
|
|
options?: {delay: number}
|
|
|
|
): Promise<void> {
|
2023-05-19 13:10:43 +00:00
|
|
|
assert(
|
|
|
|
this.#page.isDragInterceptionEnabled(),
|
|
|
|
'Drag Interception is not enabled!'
|
|
|
|
);
|
2023-06-19 11:26:30 +00:00
|
|
|
await this.scrollIntoViewIfNeeded();
|
2022-06-23 09:31:43 +00:00
|
|
|
const startPoint = await this.clickablePoint();
|
|
|
|
const targetPoint = await target.clickablePoint();
|
|
|
|
await this.#page.mouse.dragAndDrop(startPoint, targetPoint, options);
|
|
|
|
}
|
|
|
|
|
2023-08-31 12:04:40 +00:00
|
|
|
@throwIfDisposed()
|
2023-02-09 17:04:06 +00:00
|
|
|
override async uploadFile(
|
|
|
|
this: CDPElementHandle<HTMLInputElement>,
|
2022-06-23 09:31:43 +00:00
|
|
|
...filePaths: string[]
|
|
|
|
): Promise<void> {
|
|
|
|
const isMultiple = await this.evaluate(element => {
|
|
|
|
return element.multiple;
|
|
|
|
});
|
|
|
|
assert(
|
|
|
|
filePaths.length <= 1 || isMultiple,
|
|
|
|
'Multiple file uploads only work with <input type=file multiple>'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Locate all files and confirm that they exist.
|
|
|
|
let path: typeof import('path');
|
|
|
|
try {
|
|
|
|
path = await import('path');
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof TypeError) {
|
|
|
|
throw new Error(
|
|
|
|
`JSHandle#uploadFile can only be used in Node-like environments.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
const files = filePaths.map(filePath => {
|
|
|
|
if (path.win32.isAbsolute(filePath) || path.posix.isAbsolute(filePath)) {
|
|
|
|
return filePath;
|
|
|
|
} else {
|
|
|
|
return path.resolve(filePath);
|
|
|
|
}
|
|
|
|
});
|
2023-02-09 17:04:06 +00:00
|
|
|
const {node} = await this.client.send('DOM.describeNode', {
|
2023-05-15 14:39:47 +00:00
|
|
|
objectId: this.id,
|
2023-02-09 17:04:06 +00:00
|
|
|
});
|
2022-06-23 09:31:43 +00:00
|
|
|
const {backendNodeId} = node;
|
|
|
|
|
|
|
|
/* The zero-length array is a special case, it seems that
|
|
|
|
DOM.setFileInputFiles does not actually update the files in that case,
|
|
|
|
so the solution is to eval the element value to a new FileList directly.
|
|
|
|
*/
|
|
|
|
if (files.length === 0) {
|
|
|
|
await this.evaluate(element => {
|
|
|
|
element.files = new DataTransfer().files;
|
|
|
|
|
|
|
|
// Dispatch events for this case because it should behave akin to a user action.
|
|
|
|
element.dispatchEvent(new Event('input', {bubbles: true}));
|
|
|
|
element.dispatchEvent(new Event('change', {bubbles: true}));
|
|
|
|
});
|
|
|
|
} else {
|
2022-08-11 09:45:35 +00:00
|
|
|
await this.client.send('DOM.setFileInputFiles', {
|
2023-05-15 14:39:47 +00:00
|
|
|
objectId: this.id,
|
2022-06-23 09:31:43 +00:00
|
|
|
files,
|
|
|
|
backendNodeId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-31 12:04:40 +00:00
|
|
|
@throwIfDisposed()
|
2023-02-09 17:04:06 +00:00
|
|
|
override async screenshot(
|
|
|
|
this: CDPElementHandle<Element>,
|
2022-07-06 07:05:37 +00:00
|
|
|
options: ScreenshotOptions = {}
|
|
|
|
): Promise<string | Buffer> {
|
2022-06-23 09:31:43 +00:00
|
|
|
let needsViewportReset = false;
|
|
|
|
|
|
|
|
let boundingBox = await this.boundingBox();
|
|
|
|
assert(boundingBox, 'Node is either not visible or not an HTMLElement');
|
|
|
|
|
|
|
|
const viewport = this.#page.viewport();
|
|
|
|
|
|
|
|
if (
|
2022-07-21 08:34:18 +00:00
|
|
|
viewport &&
|
|
|
|
(boundingBox.width > viewport.width ||
|
|
|
|
boundingBox.height > viewport.height)
|
2022-06-23 09:31:43 +00:00
|
|
|
) {
|
|
|
|
const newViewport = {
|
|
|
|
width: Math.max(viewport.width, Math.ceil(boundingBox.width)),
|
|
|
|
height: Math.max(viewport.height, Math.ceil(boundingBox.height)),
|
|
|
|
};
|
|
|
|
await this.#page.setViewport(Object.assign({}, viewport, newViewport));
|
|
|
|
|
|
|
|
needsViewportReset = true;
|
|
|
|
}
|
|
|
|
|
2023-06-19 11:26:30 +00:00
|
|
|
await this.scrollIntoViewIfNeeded();
|
2022-06-23 09:31:43 +00:00
|
|
|
|
|
|
|
boundingBox = await this.boundingBox();
|
|
|
|
assert(boundingBox, 'Node is either not visible or not an HTMLElement');
|
|
|
|
assert(boundingBox.width !== 0, 'Node has 0 width.');
|
|
|
|
assert(boundingBox.height !== 0, 'Node has 0 height.');
|
|
|
|
|
2022-08-11 09:45:35 +00:00
|
|
|
const layoutMetrics = await this.client.send('Page.getLayoutMetrics');
|
2022-06-23 09:31:43 +00:00
|
|
|
// Fallback to `layoutViewport` in case of using Firefox.
|
|
|
|
const {pageX, pageY} =
|
|
|
|
layoutMetrics.cssVisualViewport || layoutMetrics.layoutViewport;
|
|
|
|
|
|
|
|
const clip = Object.assign({}, boundingBox);
|
|
|
|
clip.x += pageX;
|
|
|
|
clip.y += pageY;
|
|
|
|
|
|
|
|
const imageData = await this.#page.screenshot(
|
|
|
|
Object.assign(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
clip,
|
|
|
|
},
|
|
|
|
options
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2022-07-21 08:34:18 +00:00
|
|
|
if (needsViewportReset && viewport) {
|
2022-06-23 09:31:43 +00:00
|
|
|
await this.#page.setViewport(viewport);
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageData;
|
|
|
|
}
|
2023-07-19 17:42:31 +00:00
|
|
|
|
2023-08-31 12:04:40 +00:00
|
|
|
@throwIfDisposed()
|
2023-07-19 17:42:31 +00:00
|
|
|
override async autofill(data: AutofillData): Promise<void> {
|
|
|
|
const nodeInfo = await this.client.send('DOM.describeNode', {
|
|
|
|
objectId: this.handle.id,
|
|
|
|
});
|
|
|
|
const fieldId = nodeInfo.node.backendNodeId;
|
|
|
|
const frameId = this.#frame._id;
|
|
|
|
await this.client.send('Autofill.trigger', {
|
|
|
|
fieldId,
|
|
|
|
frameId,
|
|
|
|
card: data.creditCard,
|
|
|
|
});
|
|
|
|
}
|
2023-08-23 16:00:34 +00:00
|
|
|
|
|
|
|
override assertElementHasWorld(): asserts this {
|
|
|
|
assert(this.executionContext()._world);
|
|
|
|
}
|
2022-06-23 09:31:43 +00:00
|
|
|
}
|