2017-08-15 21:54:02 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 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.
|
|
|
|
*/
|
2017-08-17 21:53:37 +00:00
|
|
|
const path = require('path');
|
2017-10-06 22:35:02 +00:00
|
|
|
const {JSHandle} = require('./ExecutionContext');
|
2018-06-29 19:03:02 +00:00
|
|
|
const {helper, assert, debugError} = require('./helper');
|
2017-08-15 21:54:02 +00:00
|
|
|
|
2017-10-06 22:35:02 +00:00
|
|
|
class ElementHandle extends JSHandle {
|
2017-08-15 21:54:02 +00:00
|
|
|
/**
|
2017-10-10 05:31:40 +00:00
|
|
|
* @param {!Puppeteer.ExecutionContext} context
|
2018-01-11 03:33:22 +00:00
|
|
|
* @param {!Puppeteer.CDPSession} client
|
2018-04-07 01:20:48 +00:00
|
|
|
* @param {!Protocol.Runtime.RemoteObject} remoteObject
|
2017-10-10 05:31:40 +00:00
|
|
|
* @param {!Puppeteer.Page} page
|
2018-02-24 05:22:53 +00:00
|
|
|
* @param {!Puppeteer.FrameManager} frameManager
|
2017-08-15 21:54:02 +00:00
|
|
|
*/
|
2018-02-24 05:22:53 +00:00
|
|
|
constructor(context, client, remoteObject, page, frameManager) {
|
2017-10-06 22:35:02 +00:00
|
|
|
super(context, client, remoteObject);
|
2017-10-07 07:28:24 +00:00
|
|
|
this._client = client;
|
|
|
|
this._remoteObject = remoteObject;
|
|
|
|
this._page = page;
|
2018-02-24 05:22:53 +00:00
|
|
|
this._frameManager = frameManager;
|
2017-10-07 07:28:24 +00:00
|
|
|
this._disposed = false;
|
2017-08-15 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 02:20:02 +00:00
|
|
|
/**
|
2017-10-06 22:35:02 +00:00
|
|
|
* @override
|
|
|
|
* @return {?ElementHandle}
|
2017-09-12 02:20:02 +00:00
|
|
|
*/
|
2017-10-06 22:35:02 +00:00
|
|
|
asElement() {
|
|
|
|
return this;
|
2017-08-15 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 05:22:53 +00:00
|
|
|
/**
|
|
|
|
* @return {!Promise<?Puppeteer.Frame>}
|
|
|
|
*/
|
|
|
|
async contentFrame() {
|
|
|
|
const nodeInfo = await this._client.send('DOM.describeNode', {
|
|
|
|
objectId: this._remoteObject.objectId
|
|
|
|
});
|
|
|
|
if (typeof nodeInfo.node.frameId !== 'string')
|
|
|
|
return null;
|
|
|
|
return this._frameManager.frame(nodeInfo.node.frameId);
|
|
|
|
}
|
|
|
|
|
2017-10-10 06:14:09 +00:00
|
|
|
async _scrollIntoViewIfNeeded() {
|
2018-07-19 01:51:18 +00:00
|
|
|
const error = await this.executionContext().evaluate(async(element, pageJavascriptEnabled) => {
|
2017-10-31 19:02:16 +00:00
|
|
|
if (!element.isConnected)
|
2017-10-09 20:23:36 +00:00
|
|
|
return 'Node is detached from document';
|
2017-10-10 05:31:40 +00:00
|
|
|
if (element.nodeType !== Node.ELEMENT_NODE)
|
2017-10-09 20:23:36 +00:00
|
|
|
return 'Node is not of type HTMLElement';
|
2018-07-19 01:51:18 +00:00
|
|
|
// force-scroll if page's javascript is disabled.
|
|
|
|
if (!pageJavascriptEnabled) {
|
|
|
|
element.scrollIntoView({block: 'center', inline: 'center', behavior: 'instant'});
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-27 01:00:55 +00:00
|
|
|
const visibleRatio = await new Promise(resolve => {
|
|
|
|
const observer = new IntersectionObserver(entries => {
|
|
|
|
resolve(entries[0].intersectionRatio);
|
|
|
|
observer.disconnect();
|
|
|
|
});
|
|
|
|
observer.observe(element);
|
|
|
|
});
|
|
|
|
if (visibleRatio !== 1.0)
|
|
|
|
element.scrollIntoView({block: 'center', inline: 'center', behavior: 'instant'});
|
2017-10-10 06:14:09 +00:00
|
|
|
return false;
|
2018-07-19 01:51:18 +00:00
|
|
|
}, this, this._page._javascriptEnabled);
|
2017-10-06 22:35:02 +00:00
|
|
|
if (error)
|
|
|
|
throw new Error(error);
|
2017-10-10 06:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-29 19:03:02 +00:00
|
|
|
* @return {!Promise<!{x: number, y: number}>}
|
2017-10-10 06:14:09 +00:00
|
|
|
*/
|
2018-06-29 19:03:02 +00:00
|
|
|
async _clickablePoint() {
|
|
|
|
const result = await this._client.send('DOM.getContentQuads', {
|
|
|
|
objectId: this._remoteObject.objectId
|
|
|
|
}).catch(debugError);
|
|
|
|
if (!result || !result.quads.length)
|
|
|
|
throw new Error('Node is either not visible or not an HTMLElement');
|
|
|
|
// Filter out quads that have too small area to click into.
|
|
|
|
const quads = result.quads.map(quad => this._fromProtocolQuad(quad)).filter(quad => computeQuadArea(quad) > 1);
|
|
|
|
if (!quads.length)
|
|
|
|
throw new Error('Node is either not visible or not an HTMLElement');
|
|
|
|
// Return the middle point of the first quad.
|
|
|
|
const quad = quads[0];
|
|
|
|
let x = 0;
|
|
|
|
let y = 0;
|
|
|
|
for (const point of quad) {
|
|
|
|
x += point.x;
|
|
|
|
y += point.y;
|
|
|
|
}
|
2017-10-09 20:23:36 +00:00
|
|
|
return {
|
2018-06-29 19:03:02 +00:00
|
|
|
x: x / 4,
|
|
|
|
y: y / 4
|
2017-10-09 20:23:36 +00:00
|
|
|
};
|
2017-08-15 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 20:52:28 +00:00
|
|
|
/**
|
2018-04-08 00:58:52 +00:00
|
|
|
* @return {!Promise<void|Protocol.DOM.getBoxModelReturnValue>}
|
2018-03-29 20:52:28 +00:00
|
|
|
*/
|
|
|
|
_getBoxModel() {
|
|
|
|
return this._client.send('DOM.getBoxModel', {
|
|
|
|
objectId: this._remoteObject.objectId
|
|
|
|
}).catch(error => debugError(error));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Array<number>} quad
|
|
|
|
* @return {!Array<object>}
|
|
|
|
*/
|
|
|
|
_fromProtocolQuad(quad) {
|
|
|
|
return [
|
|
|
|
{x: quad[0], y: quad[1]},
|
|
|
|
{x: quad[2], y: quad[3]},
|
|
|
|
{x: quad[4], y: quad[5]},
|
|
|
|
{x: quad[6], y: quad[7]}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:54:02 +00:00
|
|
|
async hover() {
|
2018-06-27 01:00:55 +00:00
|
|
|
await this._scrollIntoViewIfNeeded();
|
2018-06-29 19:03:02 +00:00
|
|
|
const {x, y} = await this._clickablePoint();
|
2017-10-07 07:28:24 +00:00
|
|
|
await this._page.mouse.move(x, y);
|
2017-08-15 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Object=} options
|
|
|
|
*/
|
2017-11-08 04:38:22 +00:00
|
|
|
async click(options = {}) {
|
2018-06-27 01:00:55 +00:00
|
|
|
await this._scrollIntoViewIfNeeded();
|
2018-06-29 19:03:02 +00:00
|
|
|
const {x, y} = await this._clickablePoint();
|
2017-10-07 07:28:24 +00:00
|
|
|
await this._page.mouse.click(x, y, options);
|
2017-08-15 21:54:02 +00:00
|
|
|
}
|
2017-08-17 21:53:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Array<string>} filePaths
|
|
|
|
* @return {!Promise}
|
|
|
|
*/
|
|
|
|
async uploadFile(...filePaths) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const files = filePaths.map(filePath => path.resolve(filePath));
|
2017-08-17 21:53:37 +00:00
|
|
|
const objectId = this._remoteObject.objectId;
|
|
|
|
return this._client.send('DOM.setFileInputFiles', { objectId, files });
|
|
|
|
}
|
2017-09-02 02:03:51 +00:00
|
|
|
|
|
|
|
async tap() {
|
2018-06-27 01:00:55 +00:00
|
|
|
await this._scrollIntoViewIfNeeded();
|
2018-06-29 19:03:02 +00:00
|
|
|
const {x, y} = await this._clickablePoint();
|
2017-10-07 07:28:24 +00:00
|
|
|
await this._page.touchscreen.tap(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
async focus() {
|
|
|
|
await this.executionContext().evaluate(element => element.focus(), this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} text
|
|
|
|
* @param {{delay: (number|undefined)}=} options
|
|
|
|
*/
|
|
|
|
async type(text, options) {
|
|
|
|
await this.focus();
|
|
|
|
await this._page.keyboard.type(text, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} key
|
|
|
|
* @param {!Object=} options
|
|
|
|
*/
|
|
|
|
async press(key, options) {
|
|
|
|
await this.focus();
|
|
|
|
await this._page.keyboard.press(key, options);
|
2017-09-02 02:03:51 +00:00
|
|
|
}
|
2017-10-10 06:14:09 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-07 21:54:40 +00:00
|
|
|
* @return {!Promise<?{x: number, y: number, width: number, height: number}>}
|
2017-10-10 06:14:09 +00:00
|
|
|
*/
|
|
|
|
async boundingBox() {
|
2018-03-29 20:52:28 +00:00
|
|
|
const result = await this._getBoxModel();
|
2017-10-13 00:53:59 +00:00
|
|
|
|
2017-11-07 21:54:40 +00:00
|
|
|
if (!result)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
const quad = result.model.border;
|
2017-10-13 00:53:59 +00:00
|
|
|
const x = Math.min(quad[0], quad[2], quad[4], quad[6]);
|
|
|
|
const y = Math.min(quad[1], quad[3], quad[5], quad[7]);
|
|
|
|
const width = Math.max(quad[0], quad[2], quad[4], quad[6]) - x;
|
|
|
|
const height = Math.max(quad[1], quad[3], quad[5], quad[7]) - y;
|
|
|
|
|
|
|
|
return {x, y, width, height};
|
2017-10-10 06:14:09 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 20:52:28 +00:00
|
|
|
/**
|
|
|
|
* @return {!Promise<?object>}
|
|
|
|
*/
|
|
|
|
async boxModel() {
|
|
|
|
const result = await this._getBoxModel();
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
const {content, padding, border, margin, width, height} = result.model;
|
|
|
|
return {
|
|
|
|
content: this._fromProtocolQuad(content),
|
|
|
|
padding: this._fromProtocolQuad(padding),
|
|
|
|
border: this._fromProtocolQuad(border),
|
|
|
|
margin: this._fromProtocolQuad(margin),
|
|
|
|
width,
|
|
|
|
height
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-10-10 06:14:09 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {!Object=} options
|
|
|
|
* @returns {!Promise<Object>}
|
|
|
|
*/
|
|
|
|
async screenshot(options = {}) {
|
2018-02-23 22:13:08 +00:00
|
|
|
let needsViewportReset = false;
|
|
|
|
|
2018-06-29 19:03:02 +00:00
|
|
|
let boundingBox = await this.boundingBox();
|
|
|
|
assert(boundingBox, 'Node is either not visible or not an HTMLElement');
|
2018-02-23 22:13:08 +00:00
|
|
|
|
|
|
|
const viewport = this._page.viewport();
|
|
|
|
|
|
|
|
if (boundingBox.width > viewport.width || boundingBox.height > viewport.height) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-06-27 01:00:55 +00:00
|
|
|
await this._scrollIntoViewIfNeeded();
|
2018-02-23 22:13:08 +00:00
|
|
|
|
2018-06-29 19:03:02 +00:00
|
|
|
boundingBox = await this.boundingBox();
|
|
|
|
assert(boundingBox, 'Node is either not visible or not an HTMLElement');
|
2018-02-23 22:13:08 +00:00
|
|
|
|
2017-11-11 00:02:52 +00:00
|
|
|
const { layoutViewport: { pageX, pageY } } = await this._client.send('Page.getLayoutMetrics');
|
|
|
|
|
|
|
|
const clip = Object.assign({}, boundingBox);
|
|
|
|
clip.x += pageX;
|
|
|
|
clip.y += pageY;
|
2018-02-23 22:13:08 +00:00
|
|
|
|
|
|
|
const imageData = await this._page.screenshot(Object.assign({}, {
|
2017-11-11 00:02:52 +00:00
|
|
|
clip
|
2017-10-10 06:14:09 +00:00
|
|
|
}, options));
|
2018-02-23 22:13:08 +00:00
|
|
|
|
|
|
|
if (needsViewportReset)
|
|
|
|
await this._page.setViewport(viewport);
|
|
|
|
|
|
|
|
return imageData;
|
2017-10-10 06:14:09 +00:00
|
|
|
}
|
2017-10-27 09:08:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} selector
|
|
|
|
* @return {!Promise<?ElementHandle>}
|
|
|
|
*/
|
|
|
|
async $(selector) {
|
|
|
|
const handle = await this.executionContext().evaluateHandle(
|
|
|
|
(element, selector) => element.querySelector(selector),
|
|
|
|
this, selector
|
|
|
|
);
|
|
|
|
const element = handle.asElement();
|
|
|
|
if (element)
|
|
|
|
return element;
|
|
|
|
await handle.dispose();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} selector
|
|
|
|
* @return {!Promise<!Array<!ElementHandle>>}
|
|
|
|
*/
|
|
|
|
async $$(selector) {
|
|
|
|
const arrayHandle = await this.executionContext().evaluateHandle(
|
|
|
|
(element, selector) => element.querySelectorAll(selector),
|
|
|
|
this, selector
|
|
|
|
);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2017-12-20 00:23:45 +00:00
|
|
|
|
2018-05-09 01:17:59 +00:00
|
|
|
/**
|
|
|
|
* @param {string} selector
|
|
|
|
* @param {Function|String} pageFunction
|
|
|
|
* @param {!Array<*>} args
|
|
|
|
* @return {!Promise<(!Object|undefined)>}
|
|
|
|
*/
|
|
|
|
async $eval(selector, pageFunction, ...args) {
|
|
|
|
const elementHandle = await this.$(selector);
|
|
|
|
if (!elementHandle)
|
|
|
|
throw new Error(`Error: failed to find element matching selector "${selector}"`);
|
|
|
|
const result = await this.executionContext().evaluate(pageFunction, elementHandle, ...args);
|
|
|
|
await elementHandle.dispose();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-25 23:56:51 +00:00
|
|
|
/**
|
|
|
|
* @param {string} selector
|
|
|
|
* @param {Function|String} pageFunction
|
|
|
|
* @param {!Array<*>} args
|
|
|
|
* @return {!Promise<(!Object|undefined)>}
|
|
|
|
*/
|
|
|
|
async $$eval(selector, pageFunction, ...args) {
|
|
|
|
const arrayHandle = await this.executionContext().evaluateHandle(
|
|
|
|
(element, selector) => Array.from(element.querySelectorAll(selector)),
|
|
|
|
this, selector
|
|
|
|
);
|
|
|
|
|
|
|
|
const result = await this.executionContext().evaluate(pageFunction, arrayHandle, ...args);
|
|
|
|
await arrayHandle.dispose();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-12-20 00:23:45 +00:00
|
|
|
/**
|
|
|
|
* @param {string} expression
|
2018-01-03 23:37:08 +00:00
|
|
|
* @return {!Promise<!Array<!ElementHandle>>}
|
2017-12-20 00:23:45 +00:00
|
|
|
*/
|
2018-01-03 23:37:08 +00:00
|
|
|
async $x(expression) {
|
|
|
|
const arrayHandle = await this.executionContext().evaluateHandle(
|
2017-12-20 00:23:45 +00:00
|
|
|
(element, expression) => {
|
|
|
|
const document = element.ownerDocument || element;
|
2018-01-03 23:37:08 +00:00
|
|
|
const iterator = document.evaluate(expression, element, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE);
|
|
|
|
const array = [];
|
|
|
|
let item;
|
|
|
|
while ((item = iterator.iterateNext()))
|
|
|
|
array.push(item);
|
|
|
|
return array;
|
2017-12-20 00:23:45 +00:00
|
|
|
},
|
|
|
|
this, expression
|
|
|
|
);
|
2018-01-03 23:37:08 +00: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);
|
|
|
|
}
|
|
|
|
return result;
|
2017-12-20 00:23:45 +00:00
|
|
|
}
|
2018-07-12 00:51:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {!Promise<boolean>}
|
|
|
|
*/
|
|
|
|
isIntersectingViewport() {
|
2018-07-12 04:04:36 +00:00
|
|
|
return this.executionContext().evaluate(async element => {
|
|
|
|
const visibleRatio = await new Promise(resolve => {
|
|
|
|
const observer = new IntersectionObserver(entries => {
|
|
|
|
resolve(entries[0].intersectionRatio);
|
|
|
|
observer.disconnect();
|
|
|
|
});
|
|
|
|
observer.observe(element);
|
|
|
|
});
|
|
|
|
return visibleRatio > 0;
|
|
|
|
}, this);
|
2018-07-12 00:51:04 +00:00
|
|
|
}
|
2017-08-15 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
2018-06-29 19:03:02 +00:00
|
|
|
function computeQuadArea(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 area;
|
|
|
|
}
|
|
|
|
|
2018-08-06 18:31:33 +00:00
|
|
|
module.exports = {ElementHandle};
|
2017-08-15 21:54:02 +00:00
|
|
|
helper.tracePublicAPI(ElementHandle);
|