This repository was archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathEyesTargetLocator.js
More file actions
326 lines (294 loc) · 13.2 KB
/
Copy pathEyesTargetLocator.js
File metadata and controls
326 lines (294 loc) · 13.2 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
315
316
317
318
319
320
321
322
323
324
325
326
(function () {
'use strict';
var EyesUtils = require('eyes.utils'),
FrameChain = require('./FrameChain').FrameChain,
ScrollPositionProvider = require('./ScrollPositionProvider').ScrollPositionProvider,
EyesRemoteWebElement = require('./EyesRemoteWebElement').EyesRemoteWebElement;
var ArgumentGuard = EyesUtils.ArgumentGuard,
GeneralUtils = EyesUtils.GeneralUtils;
/**
* @enum {number}
* @readonly
*/
EyesTargetLocator.TargetType = {
FRAME: 1,
PARENT_FRAME: 2,
DEFAULT_CONTENT: 3
};
//noinspection JSUnusedLocalSymbols
/**
* A wrapper for an action to be performed before the actual switch is made.
*/
function OnWillSwitch() {
/**
* Will be called before switching into a frame.
* @param {TargetType} targetType The type of frame we're about to switch into.
* @param {WebElement} targetFrame The element about to be switched to,
* if available. Otherwise, null.
*/
this.prototype.willSwitchToFrame = function (targetType, targetFrame) {
};
/**
* Will be called before switching into a window.
* @param {string} nameOrHandle The name/handle of the window to be switched to.
*/
this.prototype.willSwitchToWindow = function (nameOrHandle) {
};
}
/**
* Initialized a new EyesTargetLocator object.
* @constructor
* @param {Logger} logger A Logger instance.
* @param {EyesWebDriver} driver The WebDriver from which the targetLocator was received.
* @param {TargetLocator} targetLocator The actual TargetLocator object.
* @param {OnWillSwitch} onWillSwitch A delegate to be called whenever a relevant switch is about to be performed.
* @param {PromiseFactory} promiseFactory
* @mixin TargetLocator
*/
function EyesTargetLocator(logger, driver, targetLocator, onWillSwitch, promiseFactory) {
ArgumentGuard.notNull(logger, "logger");
ArgumentGuard.notNull(driver, "driver");
ArgumentGuard.notNull(targetLocator, "targetLocator");
ArgumentGuard.notNull(onWillSwitch, "onWillSwitch");
this._logger = logger;
this._driver = driver;
this._targetLocator = targetLocator;
this._onWillSwitch = onWillSwitch;
this._promiseFactory = promiseFactory;
this._scrollPosition = new ScrollPositionProvider(this._logger, this._driver, this._promiseFactory);
GeneralUtils.mixin(this, targetLocator);
}
/**
* @param {int|string|EyesRemoteWebElement} obj
* @return {Promise<void>}
*/
EyesTargetLocator.prototype.frame = function (obj) {
var that = this, frames;
if (typeof obj === 'string' || obj instanceof String) {
this._logger.verbose("EyesTargetLocator.frame('", obj, "')");
// Finding the target element so we can report it.
// We use find elements(plural) to avoid exception when the element
// is not found.
this._logger.verbose("Getting frames by name...");
return this._driver.findElementsByName(obj).then(function(elements) {
if (elements.length === 0) {
that._logger.verbose("No frames Found! Trying by id...");
// If there are no frames by that name, we'll try the id
return that._driver.findElementsById(obj);
}
return elements;
}).then(function(elements) {
if (elements.length === 0) {
// No such frame, bummer
throw new Error("No frame with name or id '" + obj + "' exists!");
}
return elements;
}).then(function (frames) {
if (frames.length === 0) {
that._logger.verbose("No frames Found! Trying by id...");
// If there are no frames by that name, we'll try the id
frames = that._driver.findElementsById(obj);
if (frames.length === 0) {
// No such frame, bummer
throw new Error("No frame with name or id '" + obj + "' exists!");
}
}
that._logger.verbose("Done! Making preparations..");
return that._onWillSwitch.willSwitchToFrame(EyesTargetLocator.TargetType.FRAME, frames[0]);
}).then(function () {
that._logger.verbose("Done! Switching to frame...");
return that._targetLocator.frame(obj)
}).then(function () {
that._logger.verbose("Done!");
});
} else if (obj instanceof EyesRemoteWebElement) {
that._logger.verbose("EyesTargetLocator.frame(element)");
that._logger.verbose("Making preparations..");
return that._onWillSwitch.willSwitchToFrame(EyesTargetLocator.TargetType.FRAME, obj).then(function () {
that._logger.verbose("Done! Switching to frame...");
return that._targetLocator.frame(obj.getRemoteWebElement())
}).then(function () {
that._logger.verbose("Done!");
});
} else {
that._logger.verbose("EyesTargetLocator.frame(", typeof obj, ")");
// Finding the target element so and reporting it using onWillSwitch.
that._logger.verbose("Getting frames list...");
return that._driver.findElementsByCssSelector("frame, iframe").then(function (elements) {
if (obj > elements.length) {
throw new Error("Frame index [" + obj + "] is invalid!");
}
return elements;
}).then(function (frames) {
that._logger.verbose("Done! getting the specific frame...");
var targetFrame = frames[obj];
that._logger.verbose("Done! Making preparations...");
return that._onWillSwitch.willSwitchToFrame(EyesTargetLocator.TargetType.FRAME, targetFrame);
}).then(function () {
that._logger.verbose("Done! Switching to frame...");
return that._targetLocator.frame(obj)
}).then(function () {
that._logger.verbose("Done!");
});
}
};
/**
* @return {Promise<void>}
*/
EyesTargetLocator.prototype.parentFrame = function () {
var that = this;
this._logger.verbose("EyesTargetLocator.parentFrame()");
return this._driver.getPromiseFactory().makePromise(function (resolve) {
if (that._driver.getFrameChain().size() !== 0) {
that._logger.verbose("Making preparations..");
return that._onWillSwitch.willSwitchToFrame(EyesTargetLocator.TargetType.PARENT_FRAME, null).then(function () {
return that._targetLocator.defaultContent();
}).then(function () {
that._logger.verbose("Done! Switching to parent frame..");
return that.frames(that._driver.getFrameChain());
}).then(function () {
that._logger.verbose("Done!");
resolve();
});
}
resolve();
});
};
/**
* Switches into every frame in the frame chain. This is used as way to
* switch into nested frames (while considering scroll) in a single call.
* @param {FrameChain|string[]} obj The path to the frame to switch to.
* Or the path to the frame to check. This is a list of frame names/IDs
* (where each frame is nested in the previous frame).
* @return {Promise<void>} The WebDriver with the switched context.
*/
EyesTargetLocator.prototype.frames = function (obj) {
var that = this;
return this._driver.getPromiseFactory().makePromise(function (resolve) {
if (obj instanceof FrameChain) {
that._logger.verbose("EyesTargetLocator.frames(frameChain)");
if (obj.size() > 0) {
return _framesSetPosition(that, obj, obj.size() - 1, that._driver.getPromiseFactory()).then(function () {
that._logger.verbose("Done switching into nested frames!");
resolve();
});
}
resolve();
} else if (Array.isArray(obj)) {
if (obj.length > 0) {
return _framesSetPositionFromArray(that, obj, obj.length - 1, that._driver.getPromiseFactory()).then(function () {
that._logger.verbose("Done switching into nested frames!");
resolve();
});
}
resolve();
}
});
};
function _framesSetPosition(targetLocator, obj, retries, promiseFactory) {
return promiseFactory.makePromise(function (resolve) {
// TODO: check order, if it make sense
var frame = obj.getFrames()[retries];
targetLocator._scrollPosition.setPosition(frame.getParentScrollPosition()).then(function () {
targetLocator._logger.verbose("Done! Switching to frame...");
return targetLocator._driver.switchTo().frame(frame.getReference());
}).then(function () {
targetLocator._logger.verbose("Done!");
if (retries === 0) {
resolve();
return;
}
targetLocator.controlFlow().then(function () {
_framesSetPosition(targetLocator, obj, retries - 1, promiseFactory).then(function () {
resolve();
});
});
});
});
}
function _framesSetPositionFromArray(targetLocator, obj, retries, promiseFactory) {
return promiseFactory.makePromise(function (resolve) {
targetLocator._logger.verbose("Switching to frame...");
targetLocator._driver.switchTo().frame(obj[retries]).then(function () {
targetLocator._logger.verbose("Done!");
if (retries === 0) {
resolve();
return;
}
targetLocator.controlFlow().then(function () {
_framesSetPositionFromArray(targetLocator, obj, retries - 1, promiseFactory).then(function () {
resolve();
});
});
});
});
}
/**
* @param {string} nameOrHandle
* @return {Promise<void>}
*/
EyesTargetLocator.prototype.window = function (nameOrHandle) {
var that = this;
this._logger.verbose("EyesTargetLocator.window()");
return this._driver.getPromiseFactory().makePromise(function (resolve) {
that._logger.verbose("Making preparations..");
that._onWillSwitch.willSwitchToWindow(nameOrHandle).then(function () {
that._logger.verbose("Done! Switching to window..");
return that._targetLocator.window(nameOrHandle);
}).then(function () {
that._logger.verbose("Done!");
resolve();
});
});
};
/**
* @return {Promise<void>}
*/
EyesTargetLocator.prototype.defaultContent = function () {
var that = this;
return this._driver.getPromiseFactory().makePromise(function (resolve) {
that._logger.verbose("EyesTargetLocator.defaultContent()");
if (that._driver.getFrameChain().size() !== 0) {
that._logger.verbose("Making preparations..");
that._onWillSwitch.willSwitchToFrame(EyesTargetLocator.TargetType.DEFAULT_CONTENT, null).then(function () {
that._logger.verbose("Done! Switching to default content..");
return that._targetLocator.defaultContent();
}).then(function () {
that._logger.verbose("Done!");
resolve();
});
}
resolve();
});
};
/**
* @return {Promise<EyesRemoteWebElement>}
*/
EyesTargetLocator.prototype.activeElement = function () {
var that = this;
this._logger.verbose("EyesTargetLocator.activeElement()");
return this._driver.getPromiseFactory().makePromise(function (resolve) {
that._logger.verbose("Switching to element..");
that._targetLocator.activeElement().then(function (element) {
var result = new EyesRemoteWebElement(element, that._driver, that._logger);
that._logger.verbose("Done!");
resolve(result);
})
});
};
/**
* @return {Promise<Alert>}
*/
EyesTargetLocator.prototype.alert = function () {
var that = this;
this._logger.verbose("EyesTargetLocator.alert()");
return this._driver.getPromiseFactory().makePromise(function (resolve) {
that._logger.verbose("Switching to alert..");
that._targetLocator.alert().then(function (alert) {
that._logger.verbose("Done!");
resolve(alert);
});
});
};
exports.EyesTargetLocator = EyesTargetLocator;
}());