2019-11-27 16:02:31 -08:00
|
|
|
// Copyright (c) Microsoft Corporation.
|
|
|
|
// Licensed under the MIT license.
|
|
|
|
|
|
|
|
import * as frames from './frames';
|
2019-11-27 16:03:51 -08:00
|
|
|
import * as input from './input';
|
|
|
|
import * as js from './javascript';
|
|
|
|
import * as types from './types';
|
2019-11-28 12:50:52 -08:00
|
|
|
import * as injectedSource from './generated/injectedSource';
|
2019-12-12 09:02:37 -08:00
|
|
|
import * as zsSelectorEngineSource from './generated/zsSelectorEngineSource';
|
2019-12-05 09:54:50 -08:00
|
|
|
import { assert, helper, debugError } from './helper';
|
2019-12-02 13:12:28 -08:00
|
|
|
import Injected from './injected/injected';
|
2019-12-12 21:11:52 -08:00
|
|
|
import { Page } from './page';
|
2019-11-27 16:02:31 -08:00
|
|
|
|
2019-12-05 16:26:09 -08:00
|
|
|
type ScopedSelector = types.Selector & { scope?: ElementHandle };
|
2019-12-14 19:13:22 -08:00
|
|
|
type ResolvedSelector = { scope?: ElementHandle, selector: string, visibility: types.Visibility, disposeScope?: boolean };
|
2019-12-02 17:33:44 -08:00
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
export class FrameExecutionContext extends js.ExecutionContext {
|
|
|
|
private readonly _frame: frames.Frame;
|
2019-11-28 12:50:52 -08:00
|
|
|
|
|
|
|
private _injectedPromise?: Promise<js.JSHandle>;
|
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
constructor(delegate: js.ExecutionContextDelegate, frame: frames.Frame) {
|
|
|
|
super(delegate);
|
|
|
|
this._frame = frame;
|
2019-11-28 12:50:52 -08:00
|
|
|
}
|
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
frame(): frames.Frame | null {
|
|
|
|
return this._frame;
|
2019-12-02 13:12:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
_createHandle(remoteObject: any): js.JSHandle | null {
|
|
|
|
if (this._frame._page._delegate.isElementHandle(remoteObject))
|
|
|
|
return new ElementHandle(this, remoteObject);
|
|
|
|
return super._createHandle(remoteObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
_injected(): Promise<js.JSHandle> {
|
2019-11-28 12:50:52 -08:00
|
|
|
if (!this._injectedPromise) {
|
2019-12-16 20:49:18 -08:00
|
|
|
const additionalEngineSources = [zsSelectorEngineSource.source];
|
2019-11-28 12:50:52 -08:00
|
|
|
const source = `
|
|
|
|
new (${injectedSource.source})([
|
2019-12-16 20:49:18 -08:00
|
|
|
${additionalEngineSources.join(',\n')},
|
2019-11-28 12:50:52 -08:00
|
|
|
])
|
|
|
|
`;
|
2019-12-12 21:11:52 -08:00
|
|
|
this._injectedPromise = this.evaluateHandle(source);
|
2019-11-28 12:50:52 -08:00
|
|
|
}
|
|
|
|
return this._injectedPromise;
|
|
|
|
}
|
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
async _adoptElementHandle<T extends Node>(handle: ElementHandle<T>): Promise<ElementHandle<T>> {
|
|
|
|
assert(handle.executionContext() !== this, 'Should not adopt to the same context');
|
|
|
|
return this._frame._page._delegate.adoptElementHandle(handle, this);
|
2019-12-02 17:33:44 -08:00
|
|
|
}
|
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
async _resolveSelector(selector: string | ScopedSelector): Promise<ResolvedSelector> {
|
2019-12-02 17:33:44 -08:00
|
|
|
if (helper.isString(selector))
|
2019-12-14 19:13:22 -08:00
|
|
|
return { selector: normalizeSelector(selector), visibility: 'any' };
|
2019-12-12 21:11:52 -08:00
|
|
|
if (selector.scope && selector.scope.executionContext() !== this) {
|
|
|
|
const scope = await this._adoptElementHandle(selector.scope);
|
2019-12-14 19:13:22 -08:00
|
|
|
return { scope, selector: normalizeSelector(selector.selector), disposeScope: true, visibility: selector.visibility || 'any' };
|
2019-12-02 17:33:44 -08:00
|
|
|
}
|
2019-12-14 19:13:22 -08:00
|
|
|
return { scope: selector.scope, selector: normalizeSelector(selector.selector), visibility: selector.visibility || 'any' };
|
2019-11-28 12:50:52 -08:00
|
|
|
}
|
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
async _$(selector: string | ScopedSelector): Promise<ElementHandle<Element> | null> {
|
|
|
|
const resolved = await this._resolveSelector(selector);
|
|
|
|
const handle = await this.evaluateHandle(
|
2019-12-14 19:13:22 -08:00
|
|
|
(injected: Injected, selector: string, visibility: types.Visibility, scope?: Node) => {
|
2019-12-04 13:11:10 -08:00
|
|
|
const element = injected.querySelector(selector, scope || document);
|
2019-12-14 19:13:22 -08:00
|
|
|
if (visibility === 'any' || !element)
|
2019-12-04 13:11:10 -08:00
|
|
|
return element;
|
2019-12-14 19:13:22 -08:00
|
|
|
return injected.isVisible(element) === (visibility === 'visible') ? element : undefined;
|
2019-12-04 13:11:10 -08:00
|
|
|
},
|
2019-12-14 19:13:22 -08:00
|
|
|
await this._injected(), resolved.selector, resolved.visibility, resolved.scope
|
2019-12-02 17:33:44 -08:00
|
|
|
);
|
2019-12-04 13:11:10 -08:00
|
|
|
if (resolved.disposeScope)
|
|
|
|
await resolved.scope.dispose();
|
2019-12-02 17:33:44 -08:00
|
|
|
if (!handle.asElement())
|
2019-11-28 12:50:52 -08:00
|
|
|
await handle.dispose();
|
2019-12-02 17:33:44 -08:00
|
|
|
return handle.asElement();
|
2019-11-28 12:50:52 -08:00
|
|
|
}
|
2019-11-27 16:02:31 -08:00
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
async _$$(selector: string | ScopedSelector): Promise<ElementHandle<Element>[]> {
|
|
|
|
const resolved = await this._resolveSelector(selector);
|
|
|
|
const arrayHandle = await this.evaluateHandle(
|
2019-12-14 19:13:22 -08:00
|
|
|
(injected: Injected, selector: string, visibility: types.Visibility, scope?: Node) => {
|
2019-12-04 13:11:10 -08:00
|
|
|
const elements = injected.querySelectorAll(selector, scope || document);
|
2019-12-14 19:13:22 -08:00
|
|
|
if (visibility !== 'any')
|
|
|
|
return elements.filter(element => injected.isVisible(element) === (visibility === 'visible'));
|
2019-12-04 13:11:10 -08:00
|
|
|
return elements;
|
|
|
|
},
|
2019-12-14 19:13:22 -08:00
|
|
|
await this._injected(), resolved.selector, resolved.visibility, resolved.scope
|
2019-12-02 17:33:44 -08:00
|
|
|
);
|
2019-12-04 13:11:10 -08:00
|
|
|
if (resolved.disposeScope)
|
|
|
|
await resolved.scope.dispose();
|
2019-12-02 17:33:44 -08:00
|
|
|
const properties = await arrayHandle.getProperties();
|
|
|
|
await arrayHandle.dispose();
|
|
|
|
const result = [];
|
|
|
|
for (const property of properties.values()) {
|
|
|
|
const elementHandle = property.asElement();
|
|
|
|
if (elementHandle)
|
|
|
|
result.push(elementHandle);
|
|
|
|
else
|
|
|
|
await property.dispose();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
_$eval: types.$Eval<string | ScopedSelector> = async (selector, pageFunction, ...args) => {
|
|
|
|
const elementHandle = await this._$(selector);
|
2019-12-02 17:33:44 -08:00
|
|
|
if (!elementHandle)
|
2019-12-04 13:11:10 -08:00
|
|
|
throw new Error(`Error: failed to find element matching selector "${types.selectorToString(selector)}"`);
|
2019-12-02 17:33:44 -08:00
|
|
|
const result = await elementHandle.evaluate(pageFunction, ...args as any);
|
|
|
|
await elementHandle.dispose();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
_$$eval: types.$$Eval<string | ScopedSelector> = async (selector, pageFunction, ...args) => {
|
|
|
|
const resolved = await this._resolveSelector(selector);
|
|
|
|
const arrayHandle = await this.evaluateHandle(
|
2019-12-14 19:13:22 -08:00
|
|
|
(injected: Injected, selector: string, visibility: types.Visibility, scope?: Node) => {
|
2019-12-04 13:11:10 -08:00
|
|
|
const elements = injected.querySelectorAll(selector, scope || document);
|
2019-12-14 19:13:22 -08:00
|
|
|
if (visibility !== 'any')
|
|
|
|
return elements.filter(element => injected.isVisible(element) === (visibility === 'visible'));
|
2019-12-04 13:11:10 -08:00
|
|
|
return elements;
|
|
|
|
},
|
2019-12-14 19:13:22 -08:00
|
|
|
await this._injected(), resolved.selector, resolved.visibility, resolved.scope
|
2019-12-02 17:33:44 -08:00
|
|
|
);
|
|
|
|
const result = await arrayHandle.evaluate(pageFunction, ...args as any);
|
|
|
|
await arrayHandle.dispose();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2019-12-02 13:12:28 -08:00
|
|
|
|
2019-12-05 16:26:09 -08:00
|
|
|
export class ElementHandle<T extends Node = Node> extends js.JSHandle<T> {
|
2019-12-12 21:11:52 -08:00
|
|
|
readonly _context: FrameExecutionContext;
|
|
|
|
readonly _page: Page;
|
2019-11-27 16:02:31 -08:00
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
constructor(context: FrameExecutionContext, remoteObject: any) {
|
2019-12-02 13:12:28 -08:00
|
|
|
super(context, remoteObject);
|
2019-12-12 21:11:52 -08:00
|
|
|
this._page = context.frame()._page;
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2019-12-05 16:26:09 -08:00
|
|
|
asElement(): ElementHandle<T> | null {
|
2019-11-27 16:02:31 -08:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
async contentFrame(): Promise<frames.Frame | null> {
|
2019-12-12 21:11:52 -08:00
|
|
|
return this._page._delegate.getContentFrame(this);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async _scrollIntoViewIfNeeded() {
|
2019-12-05 16:26:09 -08:00
|
|
|
const error = await this.evaluate(async (node: Node, pageJavascriptEnabled: boolean) => {
|
|
|
|
if (!node.isConnected)
|
2019-11-27 16:02:31 -08:00
|
|
|
return 'Node is detached from document';
|
2019-12-05 16:26:09 -08:00
|
|
|
if (node.nodeType !== Node.ELEMENT_NODE)
|
2019-11-27 16:02:31 -08:00
|
|
|
return 'Node is not of type HTMLElement';
|
2019-12-05 16:26:09 -08:00
|
|
|
const element = node as Element;
|
2019-11-27 16:02:31 -08:00
|
|
|
// force-scroll if page's javascript is disabled.
|
|
|
|
if (!pageJavascriptEnabled) {
|
2019-12-06 11:52:32 -08:00
|
|
|
// @ts-ignore because only Chromium still supports 'instant'
|
2019-11-27 16:02:31 -08:00
|
|
|
element.scrollIntoView({block: 'center', inline: 'center', behavior: 'instant'});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const visibleRatio = await new Promise(resolve => {
|
|
|
|
const observer = new IntersectionObserver(entries => {
|
|
|
|
resolve(entries[0].intersectionRatio);
|
|
|
|
observer.disconnect();
|
|
|
|
});
|
|
|
|
observer.observe(element);
|
|
|
|
// Firefox doesn't call IntersectionObserver callback unless
|
|
|
|
// there are rafs.
|
|
|
|
requestAnimationFrame(() => {});
|
|
|
|
});
|
2019-12-05 16:26:09 -08:00
|
|
|
if (visibleRatio !== 1.0) {
|
2019-12-06 11:52:32 -08:00
|
|
|
// @ts-ignore because only Chromium still supports 'instant'
|
2019-11-27 16:02:31 -08:00
|
|
|
element.scrollIntoView({block: 'center', inline: 'center', behavior: 'instant'});
|
2019-12-05 16:26:09 -08:00
|
|
|
}
|
2019-11-27 16:02:31 -08:00
|
|
|
return false;
|
2019-12-12 21:11:52 -08:00
|
|
|
}, !!this._page._state.javascriptEnabled);
|
2019-11-27 16:02:31 -08:00
|
|
|
if (error)
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
|
2019-12-05 09:54:50 -08:00
|
|
|
private async _ensurePointerActionPoint(relativePoint?: types.Point): Promise<types.Point> {
|
|
|
|
await this._scrollIntoViewIfNeeded();
|
|
|
|
if (!relativePoint)
|
|
|
|
return this._clickablePoint();
|
|
|
|
let r = await this._viewportPointAndScroll(relativePoint);
|
|
|
|
if (r.scrollX || r.scrollY) {
|
|
|
|
const error = await this.evaluate((element, scrollX, scrollY) => {
|
|
|
|
if (!element.ownerDocument || !element.ownerDocument.defaultView)
|
|
|
|
return 'Node does not have a containing window';
|
|
|
|
element.ownerDocument.defaultView.scrollBy(scrollX, scrollY);
|
|
|
|
return false;
|
|
|
|
}, r.scrollX, r.scrollY);
|
|
|
|
if (error)
|
|
|
|
throw new Error(error);
|
|
|
|
r = await this._viewportPointAndScroll(relativePoint);
|
|
|
|
if (r.scrollX || r.scrollY)
|
|
|
|
throw new Error('Failed to scroll relative point into viewport');
|
|
|
|
}
|
|
|
|
return r.point;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _clickablePoint(): Promise<types.Point> {
|
|
|
|
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),
|
|
|
|
this._page._delegate.layoutViewport(),
|
2019-12-05 09:54:50 -08:00
|
|
|
]);
|
|
|
|
if (!quads || !quads.length)
|
|
|
|
throw new Error('Node is either not visible or not an HTMLElement');
|
|
|
|
|
|
|
|
const filtered = quads.map(quad => intersectQuadWithViewport(quad)).filter(quad => computeQuadArea(quad) > 1);
|
|
|
|
if (!filtered.length)
|
|
|
|
throw new Error('Node is either not visible or not an HTMLElement');
|
|
|
|
// Return the middle point of the first quad.
|
|
|
|
const result = { x: 0, y: 0 };
|
|
|
|
for (const point of filtered[0]) {
|
|
|
|
result.x += point.x / 4;
|
|
|
|
result.y += point.y / 4;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _viewportPointAndScroll(relativePoint: types.Point): Promise<{point: types.Point, scrollX: number, scrollY: number}> {
|
|
|
|
const [box, border] = await Promise.all([
|
|
|
|
this.boundingBox(),
|
2019-12-06 11:33:24 -08:00
|
|
|
this.evaluate((node: Node) => {
|
|
|
|
if (node.nodeType !== Node.ELEMENT_NODE)
|
|
|
|
return { x: 0, y: 0 };
|
|
|
|
const style = node.ownerDocument.defaultView.getComputedStyle(node as Element);
|
2019-12-05 09:54:50 -08:00
|
|
|
return { x: parseInt(style.borderLeftWidth, 10), y: parseInt(style.borderTopWidth, 10) };
|
|
|
|
}).catch(debugError),
|
|
|
|
]);
|
|
|
|
const point = { x: relativePoint.x, y: relativePoint.y };
|
|
|
|
if (box) {
|
|
|
|
point.x += box.x;
|
|
|
|
point.y += box.y;
|
|
|
|
}
|
|
|
|
if (border) {
|
|
|
|
// Make point relative to the padding box to align with offsetX/offsetY.
|
|
|
|
point.x += border.x;
|
|
|
|
point.y += border.y;
|
|
|
|
}
|
2019-12-12 21:11:52 -08:00
|
|
|
const metrics = await this._page._delegate.layoutViewport();
|
2019-12-08 17:17:49 -08:00
|
|
|
// Give 20 extra pixels to avoid any issues on viewport edge.
|
2019-12-05 09:54:50 -08:00
|
|
|
let scrollX = 0;
|
2019-12-08 17:17:49 -08:00
|
|
|
if (point.x < 20)
|
|
|
|
scrollX = point.x - 20;
|
|
|
|
if (point.x > metrics.width - 20)
|
|
|
|
scrollX = point.x - metrics.width + 20;
|
2019-12-05 09:54:50 -08:00
|
|
|
let scrollY = 0;
|
2019-12-08 17:17:49 -08:00
|
|
|
if (point.y < 20)
|
|
|
|
scrollY = point.y - 20;
|
|
|
|
if (point.y > metrics.height - 20)
|
|
|
|
scrollY = point.y - metrics.height + 20;
|
2019-12-05 09:54:50 -08:00
|
|
|
return { point, scrollX, scrollY };
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
async _performPointerAction(action: (point: types.Point) => Promise<void>, options?: input.PointerActionOptions): Promise<void> {
|
2019-12-05 09:54:50 -08:00
|
|
|
const point = await this._ensurePointerActionPoint(options ? options.relativePoint : undefined);
|
2019-11-27 16:02:31 -08:00
|
|
|
let restoreModifiers: input.Modifier[] | undefined;
|
|
|
|
if (options && options.modifiers)
|
2019-12-12 21:11:52 -08:00
|
|
|
restoreModifiers = await this._page.keyboard._ensureModifiers(options.modifiers);
|
2019-11-27 16:02:31 -08:00
|
|
|
await action(point);
|
|
|
|
if (restoreModifiers)
|
2019-12-12 21:11:52 -08:00
|
|
|
await this._page.keyboard._ensureModifiers(restoreModifiers);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
hover(options?: input.PointerActionOptions): Promise<void> {
|
2019-12-12 21:11:52 -08:00
|
|
|
return this._performPointerAction(point => this._page.mouse.move(point.x, point.y), options);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
click(options?: input.ClickOptions): Promise<void> {
|
2019-12-12 21:11:52 -08:00
|
|
|
return this._performPointerAction(point => this._page.mouse.click(point.x, point.y, options), options);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
dblclick(options?: input.MultiClickOptions): Promise<void> {
|
2019-12-12 21:11:52 -08:00
|
|
|
return this._performPointerAction(point => this._page.mouse.dblclick(point.x, point.y, options), options);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
tripleclick(options?: input.MultiClickOptions): Promise<void> {
|
2019-12-12 21:11:52 -08:00
|
|
|
return this._performPointerAction(point => this._page.mouse.tripleclick(point.x, point.y, options), options);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async select(...values: (string | ElementHandle | input.SelectOption)[]): Promise<string[]> {
|
|
|
|
const options = values.map(value => typeof value === 'object' ? value : { value });
|
|
|
|
for (const option of options) {
|
|
|
|
if (option instanceof ElementHandle)
|
|
|
|
continue;
|
|
|
|
if (option.value !== undefined)
|
|
|
|
assert(helper.isString(option.value), 'Values must be strings. Found value "' + option.value + '" of type "' + (typeof option.value) + '"');
|
|
|
|
if (option.label !== undefined)
|
|
|
|
assert(helper.isString(option.label), 'Labels must be strings. Found label "' + option.label + '" of type "' + (typeof option.label) + '"');
|
|
|
|
if (option.index !== undefined)
|
|
|
|
assert(helper.isNumber(option.index), 'Indices must be numbers. Found index "' + option.index + '" of type "' + (typeof option.index) + '"');
|
|
|
|
}
|
|
|
|
return this.evaluate(input.selectFunction, ...options);
|
|
|
|
}
|
|
|
|
|
|
|
|
async fill(value: string): Promise<void> {
|
|
|
|
assert(helper.isString(value), 'Value must be string. Found value "' + value + '" of type "' + (typeof value) + '"');
|
|
|
|
const error = await this.evaluate(input.fillFunction);
|
|
|
|
if (error)
|
|
|
|
throw new Error(error);
|
2019-12-12 21:11:52 -08:00
|
|
|
await this._page.keyboard.sendCharacters(value);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async setInputFiles(...files: (string|input.FilePayload)[]) {
|
2019-12-05 16:26:09 -08:00
|
|
|
const multiple = await this.evaluate((node: Node) => {
|
|
|
|
if (node.nodeType !== Node.ELEMENT_NODE || (node as Element).tagName !== 'INPUT')
|
|
|
|
throw new Error('Node is not an HTMLInputElement');
|
|
|
|
const input = node as HTMLInputElement;
|
|
|
|
return input.multiple;
|
|
|
|
});
|
2019-11-27 16:02:31 -08:00
|
|
|
assert(multiple || files.length <= 1, 'Non-multiple file input can only accept single file!');
|
2019-12-12 21:11:52 -08:00
|
|
|
await this._page._delegate.setInputFiles(this, await input.loadFiles(files));
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async focus() {
|
2019-12-05 16:26:09 -08:00
|
|
|
const errorMessage = await this.evaluate((element: Node) => {
|
|
|
|
if (!element['focus'])
|
|
|
|
return 'Node is not an HTML or SVG element.';
|
|
|
|
(element as HTMLElement|SVGElement).focus();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
if (errorMessage)
|
|
|
|
throw new Error(errorMessage);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async type(text: string, options: { delay: (number | undefined); } | undefined) {
|
|
|
|
await this.focus();
|
2019-12-12 21:11:52 -08:00
|
|
|
await this._page.keyboard.type(text, options);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async press(key: string, options: { delay?: number; text?: string; } | undefined) {
|
|
|
|
await this.focus();
|
2019-12-12 21:11:52 -08:00
|
|
|
await this._page.keyboard.press(key, options);
|
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
|
|
|
}
|
|
|
|
|
2019-12-06 11:52:32 -08:00
|
|
|
async screenshot(options?: types.ElementScreenshotOptions): Promise<string | Buffer> {
|
2019-12-12 21:11:52 -08:00
|
|
|
return this._page._screenshotter.screenshotElement(this, options);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
private _scopedSelector(selector: string | types.Selector): string | ScopedSelector {
|
|
|
|
selector = types.clearSelector(selector);
|
|
|
|
if (helper.isString(selector))
|
|
|
|
selector = { selector };
|
2019-12-14 19:13:22 -08:00
|
|
|
return { scope: this, selector: selector.selector, visibility: selector.visibility };
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
$(selector: string | types.Selector): Promise<ElementHandle | null> {
|
2019-12-12 21:11:52 -08:00
|
|
|
return this._context._$(this._scopedSelector(selector));
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2019-12-05 16:26:09 -08:00
|
|
|
$$(selector: string | types.Selector): Promise<ElementHandle<Element>[]> {
|
2019-12-12 21:11:52 -08:00
|
|
|
return this._context._$$(this._scopedSelector(selector));
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
$eval: types.$Eval<string | types.Selector> = (selector, pageFunction, ...args) => {
|
2019-12-12 21:11:52 -08:00
|
|
|
return this._context._$eval(this._scopedSelector(selector), pageFunction, ...args as any);
|
2019-12-04 13:11:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
$$eval: types.$$Eval<string | types.Selector> = (selector, pageFunction, ...args) => {
|
2019-12-12 21:11:52 -08:00
|
|
|
return this._context._$$eval(this._scopedSelector(selector), pageFunction, ...args as any);
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
2019-12-05 16:26:09 -08:00
|
|
|
$x(expression: string): Promise<ElementHandle<Element>[]> {
|
2019-12-12 21:11:52 -08:00
|
|
|
return this._context._$$({ scope: this, selector: 'xpath=' + expression });
|
2019-11-27 16:02:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
isIntersectingViewport(): Promise<boolean> {
|
2019-12-05 16:26:09 -08:00
|
|
|
return this.evaluate(async (node: Node) => {
|
|
|
|
if (node.nodeType !== Node.ELEMENT_NODE)
|
|
|
|
throw new Error('Node is not of type HTMLElement');
|
|
|
|
const element = node as Element;
|
2019-11-27 16:02:31 -08:00
|
|
|
const visibleRatio = await new Promise(resolve => {
|
|
|
|
const observer = new IntersectionObserver(entries => {
|
|
|
|
resolve(entries[0].intersectionRatio);
|
|
|
|
observer.disconnect();
|
|
|
|
});
|
|
|
|
observer.observe(element);
|
|
|
|
// Firefox doesn't call IntersectionObserver callback unless
|
|
|
|
// there are rafs.
|
|
|
|
requestAnimationFrame(() => {});
|
|
|
|
});
|
|
|
|
return visibleRatio > 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-12-03 10:43:13 -08:00
|
|
|
|
|
|
|
function normalizeSelector(selector: string): string {
|
|
|
|
const eqIndex = selector.indexOf('=');
|
2019-12-16 20:49:18 -08:00
|
|
|
if (eqIndex !== -1 && selector.substring(0, eqIndex).trim().match(/^[a-zA-Z_0-9-]+$/))
|
2019-12-03 10:43:13 -08:00
|
|
|
return selector;
|
|
|
|
if (selector.startsWith('//'))
|
|
|
|
return 'xpath=' + selector;
|
2019-12-12 09:02:37 -08:00
|
|
|
if (selector.startsWith('"'))
|
|
|
|
return 'zs=' + selector;
|
2019-12-03 10:43:13 -08:00
|
|
|
return 'css=' + selector;
|
|
|
|
}
|
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
export type Task = (context: FrameExecutionContext) => Promise<js.JSHandle>;
|
2019-12-03 10:51:41 -08:00
|
|
|
|
2019-12-04 13:11:10 -08:00
|
|
|
export function waitForFunctionTask(pageFunction: Function | string, options: types.WaitForFunctionOptions, ...args: any[]) {
|
2019-12-03 10:51:41 -08:00
|
|
|
const { polling = 'raf' } = options;
|
|
|
|
if (helper.isString(polling))
|
|
|
|
assert(polling === 'raf' || polling === 'mutation', 'Unknown polling option: ' + polling);
|
|
|
|
else if (helper.isNumber(polling))
|
|
|
|
assert(polling > 0, 'Cannot poll with non-positive interval: ' + polling);
|
|
|
|
else
|
|
|
|
throw new Error('Unknown polling options: ' + polling);
|
|
|
|
const predicateBody = helper.isString(pageFunction) ? 'return (' + pageFunction + ')' : 'return (' + pageFunction + ')(...args)';
|
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
return async (context: FrameExecutionContext) => context.evaluateHandle((injected: Injected, predicateBody: string, polling: types.Polling, timeout: number, ...args) => {
|
2019-12-03 10:51:41 -08:00
|
|
|
const predicate = new Function('...args', predicateBody);
|
|
|
|
if (polling === 'raf')
|
|
|
|
return injected.pollRaf(predicate, timeout, ...args);
|
|
|
|
if (polling === 'mutation')
|
|
|
|
return injected.pollMutation(predicate, timeout, ...args);
|
|
|
|
return injected.pollInterval(polling, predicate, timeout, ...args);
|
2019-12-12 21:11:52 -08:00
|
|
|
}, await context._injected(), predicateBody, polling, options.timeout, ...args);
|
2019-12-03 10:51:41 -08:00
|
|
|
}
|
|
|
|
|
2019-12-05 08:45:36 -08:00
|
|
|
export function waitForSelectorTask(selector: string | types.Selector, timeout: number): Task {
|
2019-12-12 21:11:52 -08:00
|
|
|
return async (context: FrameExecutionContext) => {
|
|
|
|
const resolved = await context._resolveSelector(selector);
|
2019-12-14 19:13:22 -08:00
|
|
|
return context.evaluateHandle((injected: Injected, selector: string, visibility: types.Visibility, timeout: number, scope?: Node) => {
|
|
|
|
if (visibility !== 'any')
|
2019-12-04 13:11:10 -08:00
|
|
|
return injected.pollRaf(predicate, timeout);
|
|
|
|
return injected.pollMutation(predicate, timeout);
|
|
|
|
|
|
|
|
function predicate(): Element | boolean {
|
|
|
|
const element = injected.querySelector(selector, scope || document);
|
|
|
|
if (!element)
|
2019-12-14 19:13:22 -08:00
|
|
|
return visibility === 'hidden';
|
|
|
|
if (visibility === 'any')
|
2019-12-04 13:11:10 -08:00
|
|
|
return element;
|
2019-12-14 19:13:22 -08:00
|
|
|
return injected.isVisible(element) === (visibility === 'visible') ? element : false;
|
2019-12-03 10:51:41 -08:00
|
|
|
}
|
2019-12-14 19:13:22 -08:00
|
|
|
}, await context._injected(), resolved.selector, resolved.visibility, timeout, resolved.scope);
|
2019-12-04 13:11:10 -08:00
|
|
|
};
|
2019-12-03 10:43:13 -08:00
|
|
|
}
|