forked from nightwatchjs/nightwatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moveTo.js
68 lines (59 loc) · 2.68 KB
/
moveTo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const {Origin} = require('selenium-webdriver');
const Utils = require('../../utils/');
const ProtocolAction = require('./_base-action.js');
const {isNumber} = Utils;
/**
* Move the mouse by an offset of the specified [Web Element ID](https://www.w3.org/TR/webdriver1/#dfn-web-elements) or relative to the current mouse cursor, if no element is specified. If an element is provided but no offset, the mouse will be moved to the center of the element.
*
* If an element is provided but no offset, the mouse will be moved to the center of the element. If the element is not visible, it will be scrolled into view.
*
* @example
* this.demoTest = function (browser) {
* browser.moveTo(null, 110, 100);
* };
*
* @syntax .moveTo([webElementId], xoffset, yoffset, [callback])
* @syntax .moveTo(null, xoffset, yoffset, [callback])
* @editline L1335
* @param {string} [webElementId] The [Web Element ID](https://www.w3.org/TR/webdriver1/#dfn-web-elements) assigned to the element to move to. If not specified or is null, the offset is relative to current position of the mouse.
* @param {number} xoffset X offset to move to, relative to the center of the element. If not specified, the mouse will move to the middle of the element.
* @param {number} yoffset Y offset to move to, relative to the center of the element. If not specified, the mouse will move to the middle of the element.
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @api protocol.useractions
*/
module.exports = class Command extends ProtocolAction {
static get isTraceable() {
return true;
}
command(...args) {
let webElementId;
let xoffset = 0;
let yoffset = 0;
let callback = function() {};
const lastItem = args[args.length - 1];
if (Utils.isFunction(lastItem)) {
args.pop();
callback = lastItem;
}
if (typeof args[0] === 'string' || arguments[0] === null) {
webElementId = args.shift();
if (webElementId === null) {
webElementId = Origin.POINTER;
} else {
ProtocolAction.validateElementId(webElementId);
}
}
if (args.length === 1 && isNumber(args[0]) && !isNaN(args[0])) {
xoffset = args[0];
} else if (args.length === 2 && isNumber(args[0]) && !isNaN(args[0]) && isNumber(args[1]) && !isNaN(args[1])) {
xoffset = args[0];
yoffset = args[1];
} else if (args.length > 2 || (args.length > 0 && (args[0] !== null || args[1] !== null))) {
throw new Error('Invalid Parameters for moveTo Command');
}
if (!webElementId) {
webElementId = Origin.POINTER;
}
return this.transportActions.moveTo(webElementId, xoffset, yoffset, callback);
}
};