forked from applitools/Eyes.Selenium.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEyesRemoteWebElement.js
More file actions
314 lines (271 loc) · 11.5 KB
/
Copy pathEyesRemoteWebElement.js
File metadata and controls
314 lines (271 loc) · 11.5 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
(function () {
'use strict';
var EyesSDK = require('eyes.sdk'),
EyesUtils = require('eyes.utils');
var MouseAction = EyesSDK.Triggers.MouseAction,
GeneralUtils = EyesUtils.GeneralUtils,
GeometryUtils = EyesUtils.GeometryUtils;
var JS_GET_SCROLL_LEFT = "return arguments[0].scrollLeft;";
var JS_GET_SCROLL_TOP = "return arguments[0].scrollTop;";
var JS_GET_SCROLL_WIDTH = "return arguments[0].scrollWidth;";
var JS_GET_SCROLL_HEIGHT = "return arguments[0].scrollHeight;";
var JS_GET_OVERFLOW = "return arguments[0].style.overflow;";
var JS_GET_LOCATION = "var rect = arguments[0].getBoundingClientRect(); return [rect.left, rect.top]";
/**
* @param {string} styleProp
* @return {string}
*/
var JS_GET_COMPUTED_STYLE_FORMATTED_STR = function (styleProp) {
return "var elem = arguments[0], styleProp = '"+ styleProp +"'; " +
"if (window.getComputedStyle) { " +
" return window.getComputedStyle(elem, null).getPropertyValue(styleProp);" +
"} else if (elem.currentStyle) { " +
" return elem.currentStyle[styleProp];" +
"} else { " +
" return null;" +
"}";
};
/**
* @param {number} scrollLeft
* @param {number} scrollTop
* @return {string}
*/
var JS_SCROLL_TO_FORMATTED_STR = function (scrollLeft, scrollTop) {
return "arguments[0].scrollLeft = " + scrollLeft + "; " +
"arguments[0].scrollTop = " + scrollTop + ";";
};
/**
* @param {string} overflow
* @return {string}
*/
var JS_SET_OVERFLOW_FORMATTED_STR = function (overflow) {
return "arguments[0].style.overflow = '" + overflow + "'";
};
/**
* Wraps a Remote Web Element.
*
* @constructor
* @param {WebElement} webElement
* @param {EyesWebDriver} eyesDriver
* @param {Logger} logger
* @mixin WebElement
**/
function EyesRemoteWebElement(webElement, eyesDriver, logger) {
this._element = webElement;
this._logger = logger;
this._eyesDriver = eyesDriver;
GeneralUtils.mixin(this, webElement);
}
function _getRectangle(location, size) {
size = size || {height: 0, width: 0};
location = location || {x: 0, y: 0};
var left = location.x,
top = location.y,
width = size.width,
height = size.height;
if (left < 0) {
width = Math.max(0, width + left);
left = 0;
}
if (top < 0) {
height = Math.max(0, height + top);
top = 0;
}
return {
top: top,
left: left,
width: width,
height: height
};
}
function _getBounds (element) {
return element.getLocation().then(function (location) {
return element.getSize().then(function (size) {
return _getRectangle(location, size);
}, function () {
return _getRectangle(location);
});
}, function () {
return _getRectangle();
});
}
EyesRemoteWebElement.registerSendKeys = function (element, eyesDriver, logger, args) {
var text = args.join('');
logger.verbose("registerSendKeys: text is", text);
return _getBounds(element).then(function (rect) {
eyesDriver.getEyes().addKeyboardTrigger(rect, text);
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Schedules a command to type a sequence on the DOM element represented by this instance.
* @param {...(number|string|!IThenable<(number|string)>)} var_args The sequence of keys to type. Number keys may
* be referenced numerically or by string (1 or '1'). All arguments will be joined into a single sequence.
* @return {!Promise<void>} A promise that will be resolved when all keys have been typed.
*/
EyesRemoteWebElement.prototype.sendKeys = function (var_args) {
var that = this, args = Array.prototype.slice.call(arguments, 0);
return EyesRemoteWebElement.registerSendKeys(that._element, that._eyesDriver, that._logger, args).then(function () {
var element = that.getRemoteWebElement();
return element.sendKeys.apply(element, args);
});
};
EyesRemoteWebElement.registerClick = function (element, eyesDriver, logger) {
logger.verbose("apply click on element");
return _getBounds(element).then(function (rect) {
var offset = {x: rect.width / 2, y: rect.height / 2};
eyesDriver.getEyes().addMouseTrigger(MouseAction.Click, rect, offset);
});
};
// noinspection JSUnusedGlobalSymbols
/**
* Schedules a command to click on this element.
* @return {!Promise<void>} A promise that will be resolved when the click command has completed.
*/
EyesRemoteWebElement.prototype.click = function () {
var that = this;
that._logger.verbose("click on element");
return EyesRemoteWebElement.registerClick(that._element, that._eyesDriver, that._logger).then(function () {
var element = that.getRemoteWebElement();
return element.click.apply(element);
});
};
EyesRemoteWebElement.prototype.findElement = function (locator) {
return new EyesRemoteWebElement(this.getRemoteWebElement().findElement(locator), this._eyesDriver, this._logger);
};
EyesRemoteWebElement.prototype.findElements = function (locator) {
var that = this;
return this.getRemoteWebElement().findElements(locator).then(function (elements) {
return elements.map(function (element) {
return new EyesRemoteWebElement(element, that._eyesDriver, that._logger);
});
});
};
/**
* Returns the computed value of the style property for the current element.
* @param {string} propStyle The style property which value we would like to extract.
* @return {promise.Promise<string>} The value of the style property of the element, or {@code null}.
*/
EyesRemoteWebElement.prototype.getComputedStyle = function (propStyle) {
return this._eyesDriver.executeScript(JS_GET_COMPUTED_STYLE_FORMATTED_STR(propStyle), this.getRemoteWebElement());
};
/**
* @return {promise.Promise<number>} The integer value of a computed style.
*/
EyesRemoteWebElement.prototype.getComputedStyleInteger = function (propStyle) {
return this.getComputedStyle(propStyle).then(function (value) {
return Math.round(parseFloat(value.trim().replace("px", "")));
});
};
/**
* @return {promise.Promise<number>} The value of the scrollLeft property of the element.
*/
EyesRemoteWebElement.prototype.getScrollLeft = function () {
return this._eyesDriver.executeScript(JS_GET_SCROLL_LEFT, this.getRemoteWebElement()).then(function (value) {
return parseInt(value, 10);
});
};
/**
* @return {promise.Promise<number>} The value of the scrollTop property of the element.
*/
EyesRemoteWebElement.prototype.getScrollTop = function () {
return this._eyesDriver.executeScript(JS_GET_SCROLL_TOP, this.getRemoteWebElement()).then(function (value) {
return parseInt(value, 10);
});
};
/**
* @return {promise.Promise<number>} The value of the scrollWidth property of the element.
*/
EyesRemoteWebElement.prototype.getScrollWidth = function () {
return this._eyesDriver.executeScript(JS_GET_SCROLL_WIDTH, this.getRemoteWebElement()).then(function (value) {
return parseInt(value, 10);
});
};
/**
* @return {promise.Promise<number>} The value of the scrollHeight property of the element.
*/
EyesRemoteWebElement.prototype.getScrollHeight = function () {
return this._eyesDriver.executeScript(JS_GET_SCROLL_HEIGHT, this.getRemoteWebElement()).then(function (value) {
return parseInt(value, 10);
});
};
/**
* @return {promise.Promise<number>} The width of the left border.
*/
EyesRemoteWebElement.prototype.getBorderLeftWidth = function () {
return this.getComputedStyleInteger("border-left-width");
};
/**
* @return {promise.Promise<number>} The width of the right border.
*/
EyesRemoteWebElement.prototype.getBorderRightWidth = function () {
return this.getComputedStyleInteger("border-right-width");
};
/**
* @return {promise.Promise<number>} The width of the top border.
*/
EyesRemoteWebElement.prototype.getBorderTopWidth = function () {
return this.getComputedStyleInteger("border-top-width");
};
/**
* @return {promise.Promise<number>} The width of the bottom border.
*/
EyesRemoteWebElement.prototype.getBorderBottomWidth = function () {
return this.getComputedStyleInteger("border-bottom-width");
};
/**
* @return {!promise.Thenable<{width: number, height: number}>} element's size
*/
EyesRemoteWebElement.prototype.getSize = function () {
return this.getRemoteWebElement().getSize().then(function (value) {
return GeometryUtils.createSize(value.width, value.height);
});
};
/**
* @return {!promise.Thenable<{x: number, y: number}>} element's location
*/
EyesRemoteWebElement.prototype.getLocation = function () {
// The workaround is similar to Java one,
// https://github.com/applitools/eyes.sdk.java3/blob/master/eyes.selenium.java/src/main/java/com/applitools/eyes/selenium/EyesRemoteWebElement.java#L453
// but we can't get raw data (including decimal values) from remote Selenium webdriver
// and therefore we should use our own client-side script for retrieving exact values and rounding up them
// this.getRemoteWebElement().getLocation()
return this._eyesDriver.executeScript(JS_GET_LOCATION, this.getRemoteWebElement()).then(function (value) {
var x = Math.ceil(value[0]) || 0;
var y = Math.ceil(value[1]) || 0;
return GeometryUtils.createLocation(x, y);
});
};
/**
* Scrolls to the specified location inside the element.
* @param {{x: number, y: number}} location The location to scroll to.
* @return {promise.Promise<void>}
*/
EyesRemoteWebElement.prototype.scrollTo = function (location) {
return this._eyesDriver.executeScript(JS_SCROLL_TO_FORMATTED_STR(location.x, location.y), this.getRemoteWebElement());
};
/**
* @return {promise.Promise<string>} The overflow of the element.
*/
EyesRemoteWebElement.prototype.getOverflow = function () {
return this._eyesDriver.executeScript(JS_GET_OVERFLOW, this.getRemoteWebElement());
};
/**
* @param {string} overflow The overflow to set
* @return {promise.Promise<void>} The overflow of the element.
*/
EyesRemoteWebElement.prototype.setOverflow = function (overflow) {
return this._eyesDriver.executeScript(JS_SET_OVERFLOW_FORMATTED_STR(overflow), this.getRemoteWebElement());
};
/**
* @return {WebElement} The original element object
*/
EyesRemoteWebElement.prototype.getRemoteWebElement = function () {
if (this._element.getRemoteWebElement) {
this._logger.log('EyesRemoteWebElement.getRemoteWebElement - remote element contains another remote');
return this._element.getRemoteWebElement();
}
return this._element;
};
exports.EyesRemoteWebElement = EyesRemoteWebElement;
}());