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');
|
2017-09-11 23:21:51 +00:00
|
|
|
const {helper} = 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
|
|
|
|
* @param {!Puppeteer.Session} client
|
2017-08-15 21:54:02 +00:00
|
|
|
* @param {!Object} remoteObject
|
2017-10-10 05:31:40 +00:00
|
|
|
* @param {!Puppeteer.Page} page
|
2017-08-15 21:54:02 +00:00
|
|
|
*/
|
2017-10-07 07:28:24 +00:00
|
|
|
constructor(context, client, remoteObject, page) {
|
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;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-10-10 06:14:09 +00:00
|
|
|
async _scrollIntoViewIfNeeded() {
|
2017-10-09 20:23:36 +00:00
|
|
|
const error = await this.executionContext().evaluate(element => {
|
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';
|
2017-08-15 21:54:02 +00:00
|
|
|
element.scrollIntoViewIfNeeded();
|
2017-10-10 06:14:09 +00:00
|
|
|
return false;
|
2017-09-12 02:20:02 +00:00
|
|
|
}, this);
|
2017-10-06 22:35:02 +00:00
|
|
|
if (error)
|
|
|
|
throw new Error(error);
|
2017-10-10 06:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Promise<{x: number, y: number}>}
|
|
|
|
*/
|
|
|
|
async _visibleCenter() {
|
|
|
|
await this._scrollIntoViewIfNeeded();
|
|
|
|
const box = await this.boundingBox();
|
2017-10-09 20:23:36 +00:00
|
|
|
return {
|
2017-10-10 06:14:09 +00:00
|
|
|
x: box.x + box.width / 2,
|
|
|
|
y: box.y + box.height / 2
|
2017-10-09 20:23:36 +00:00
|
|
|
};
|
2017-08-15 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async hover() {
|
2017-08-21 23:39:04 +00:00
|
|
|
const {x, y} = await this._visibleCenter();
|
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
|
|
|
|
*/
|
|
|
|
async click(options) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const {x, y} = await this._visibleCenter();
|
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() {
|
|
|
|
const {x, y} = await this._visibleCenter();
|
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-10-13 00:53:59 +00:00
|
|
|
* @return {!Promise<{x: number, y: number, width: number, height: number}>}
|
2017-10-10 06:14:09 +00:00
|
|
|
*/
|
|
|
|
async boundingBox() {
|
2017-10-13 00:53:59 +00:00
|
|
|
const {model} = await this._client.send('DOM.getBoxModel', {
|
2017-10-10 06:14:09 +00:00
|
|
|
objectId: this._remoteObject.objectId
|
|
|
|
});
|
2017-10-13 00:53:59 +00:00
|
|
|
if (!model)
|
|
|
|
throw new Error('Node is detached from document');
|
|
|
|
|
|
|
|
const quad = model.border;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {!Object=} options
|
|
|
|
* @returns {!Promise<Object>}
|
|
|
|
*/
|
|
|
|
async screenshot(options = {}) {
|
|
|
|
await this._scrollIntoViewIfNeeded();
|
|
|
|
const boundingBox = await this.boundingBox();
|
|
|
|
|
|
|
|
return await this._page.screenshot(Object.assign({}, {
|
|
|
|
clip: boundingBox
|
|
|
|
}, options));
|
|
|
|
}
|
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-08-15 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ElementHandle;
|
|
|
|
helper.tracePublicAPI(ElementHandle);
|