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-06 22:35:02 +00:00
|
|
|
* @param {!ExecutionContext} context
|
|
|
|
* @param {!Session} client
|
2017-08-15 21:54:02 +00:00
|
|
|
* @param {!Object} remoteObject
|
|
|
|
* @param {!Mouse} mouse
|
2017-09-02 02:03:51 +00:00
|
|
|
* @param {!Touchscreen} touchscreen;
|
2017-08-15 21:54:02 +00:00
|
|
|
*/
|
2017-10-06 22:35:02 +00:00
|
|
|
constructor(context, client, remoteObject, mouse, touchscreen) {
|
|
|
|
super(context, client, remoteObject);
|
2017-08-15 21:54:02 +00:00
|
|
|
this._mouse = mouse;
|
2017-09-02 02:03:51 +00:00
|
|
|
this._touchscreen = touchscreen;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Promise<{x: number, y: number}>}
|
|
|
|
*/
|
|
|
|
async _visibleCenter() {
|
2017-10-06 22:35:02 +00:00
|
|
|
const {center, error} = await this.executionContext().evaluate(element => {
|
2017-08-15 21:54:02 +00:00
|
|
|
if (!element.ownerDocument.contains(element))
|
2017-10-06 22:35:02 +00:00
|
|
|
return {center: null, error: 'Node is detached from document'};
|
|
|
|
if (element.nodeType !== HTMLElement.ELEMENT_NODE)
|
|
|
|
return {center: null, error: 'Node is not of type HTMLElement'};
|
2017-08-15 21:54:02 +00:00
|
|
|
element.scrollIntoViewIfNeeded();
|
2017-08-21 23:39:04 +00:00
|
|
|
const rect = element.getBoundingClientRect();
|
2017-10-06 22:35:02 +00:00
|
|
|
const center = {
|
2017-08-15 21:54:02 +00:00
|
|
|
x: (Math.max(rect.left, 0) + Math.min(rect.right, window.innerWidth)) / 2,
|
|
|
|
y: (Math.max(rect.top, 0) + Math.min(rect.bottom, window.innerHeight)) / 2
|
|
|
|
};
|
2017-10-06 22:35:02 +00:00
|
|
|
return {center, error: null};
|
2017-09-12 02:20:02 +00:00
|
|
|
}, this);
|
2017-10-06 22:35:02 +00:00
|
|
|
if (error)
|
|
|
|
throw new Error(error);
|
2017-08-15 21:54:02 +00:00
|
|
|
return center;
|
|
|
|
}
|
|
|
|
|
|
|
|
async hover() {
|
2017-08-21 23:39:04 +00:00
|
|
|
const {x, y} = await this._visibleCenter();
|
2017-08-15 21:54:02 +00:00
|
|
|
await this._mouse.move(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Object=} options
|
|
|
|
*/
|
|
|
|
async click(options) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const {x, y} = await this._visibleCenter();
|
2017-08-15 21:54:02 +00:00
|
|
|
await this._mouse.click(x, y, options);
|
|
|
|
}
|
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();
|
|
|
|
await this._touchscreen.tap(x, y);
|
|
|
|
}
|
2017-08-15 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ElementHandle;
|
|
|
|
helper.tracePublicAPI(ElementHandle);
|