-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
moveToObject.js
69 lines (56 loc) · 2.22 KB
/
moveToObject.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
69
/**
*
* Move the mouse by an offset of the specificed 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.
*
* @param {String} selector element to move to
* @param {Number} xoffset X offset to move to, relative to the top-left corner 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 top-left corner of the element. If not specified, the mouse will move to the middle of the element.
*
* @uses protocol/element, protocol/elementIdLocation
* @type action
*
*/
var staleElementRetry = require('../helpers/staleElementRetry');
module.exports = function moveToObject(selector, xoffset, yoffset) {
/**
* check for offset params
*/
var hasOffsetParams = true;
if (typeof xoffset !== 'number' && typeof yoffset !== 'number') {
hasOffsetParams = false;
}
if (typeof xoffset !== 'number') {
xoffset = 0;
}
if (typeof yoffset !== 'number') {
yoffset = 0;
}
if (this.isMobile) {
return this.element(selector).then(function(res) {
return this.elementIdSize(res.value.ELEMENT).then(function(size) {
return this.elementIdLocation(res.element.value.ELEMENT).then(function(location) {
return {
size: size,
location: location
};
});
});
}).then(function(res) {
var x = res.location.value.x + (res.size.value.width / 2),
y = res.location.value.y + (res.size.value.height / 2);
if(hasOffsetParams) {
x = res.location.value.x + xoffset;
y = res.location.value.y + yoffset;
}
return this.touchMove(x, y);
})
.catch(staleElementRetry.bind(this, 'moveToObject', arguments));
} else {
return this.element(selector).then(function(res) {
return this.moveTo(res.value.ELEMENT, xoffset, yoffset);
})
.catch(staleElementRetry.bind(this, 'moveToObject', arguments));
}
};