forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.js
436 lines (387 loc) · 14 KB
/
window.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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you 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.
/**
* @fileoverview Atoms for simulating user actions against the browser window.
*/
goog.provide('bot.window');
goog.require('bot');
goog.require('bot.Error');
goog.require('bot.ErrorCode');
goog.require('bot.events');
goog.require('bot.userAgent');
goog.require('goog.dom');
goog.require('goog.dom.DomHelper');
goog.require('goog.math.Coordinate');
goog.require('goog.math.Size');
goog.require('goog.style');
goog.require('goog.userAgent');
goog.require('goog.userAgent.product');
/**
* Whether the value of history.length includes a newly loaded page. If not,
* after a new page load history.length is the number of pages that have loaded,
* minus 1, but becomes the total number of pages on a subsequent back() call.
* @private {boolean}
* @const
*/
bot.window.HISTORY_LENGTH_INCLUDES_NEW_PAGE_ = !goog.userAgent.IE;
/**
* Whether value of history.length includes the pages ahead of the current one
* in the history. If not, history.length equals the number of prior pages.
* Here is the WebKit bug for this behavior that was fixed by version 533:
* https://bugs.webkit.org/show_bug.cgi?id=24472
* @private {boolean}
* @const
*/
bot.window.HISTORY_LENGTH_INCLUDES_FORWARD_PAGES_ =
!goog.userAgent.WEBKIT || bot.userAgent.isEngineVersion('533');
/**
* Screen orientation values. From the draft W3C spec at:
* http://www.w3.org/TR/2012/WD-screen-orientation-20120522
*
* @enum {string}
*/
bot.window.Orientation = {
PORTRAIT: 'portrait-primary',
PORTRAIT_SECONDARY: 'portrait-secondary',
LANDSCAPE: 'landscape-primary',
LANDSCAPE_SECONDARY: 'landscape-secondary'
};
/**
* Returns the degrees corresponding to the orientation input.
*
* @param {!bot.window.Orientation} orientation The orientation.
* @return {number} The orientation degrees.
* @private
*/
bot.window.getOrientationDegrees_ = (function () {
var orientationMap;
return function (orientation) {
if (!orientationMap) {
orientationMap = {};
if (goog.userAgent.MOBILE) {
// The iPhone and Android phones do not change orientation event when
// held upside down. Hence, PORTRAIT_SECONDARY is not set.
orientationMap[bot.window.Orientation.PORTRAIT] = 0;
orientationMap[bot.window.Orientation.LANDSCAPE] = 90;
orientationMap[bot.window.Orientation.LANDSCAPE_SECONDARY] = -90;
if (goog.userAgent.product.IPAD) {
orientationMap[bot.window.Orientation.PORTRAIT_SECONDARY] = 180;
}
} else if (goog.userAgent.product.ANDROID) {
// Unlike the iPad, Android tablets treat landscape orientation as the
// default, i.e., having window.orientation = 0.
orientationMap[bot.window.Orientation.PORTRAIT] = -90;
orientationMap[bot.window.Orientation.LANDSCAPE] = 0;
orientationMap[bot.window.Orientation.PORTRAIT_SECONDARY] = 90;
orientationMap[bot.window.Orientation.LANDSCAPE_SECONDARY] = 180;
}
}
return orientationMap[orientation];
};
})();
/**
* Go back in the browser history. The number of pages to go back can
* optionally be specified and defaults to 1.
*
* @param {number=} opt_numPages Number of pages to go back.
*/
bot.window.back = function (opt_numPages) {
// Relax the upper bound by one for browsers that do not count
// newly loaded pages towards the value of window.history.length.
var maxPages = bot.window.HISTORY_LENGTH_INCLUDES_NEW_PAGE_ ?
bot.getWindow().history.length - 1 : bot.getWindow().history.length;
var numPages = bot.window.checkNumPages_(maxPages, opt_numPages);
bot.getWindow().history.go(-numPages);
};
/**
* Go forward in the browser history. The number of pages to go forward can
* optionally be specified and defaults to 1.
*
* @param {number=} opt_numPages Number of pages to go forward.
*/
bot.window.forward = function (opt_numPages) {
// Do not check the upper bound (use null for infinity) for browsers that
// do not count forward pages towards the value of window.history.length.
var maxPages = bot.window.HISTORY_LENGTH_INCLUDES_FORWARD_PAGES_ ?
bot.getWindow().history.length - 1 : null;
var numPages = bot.window.checkNumPages_(maxPages, opt_numPages);
bot.getWindow().history.go(numPages);
};
/**
* @param {?number} maxPages Upper bound on number of pages; null for infinity.
* @param {number=} opt_numPages Number of pages to move in history.
* @return {number} Correct number of pages to move in history.
* @private
*/
bot.window.checkNumPages_ = function (maxPages, opt_numPages) {
var numPages = goog.isDef(opt_numPages) ? opt_numPages : 1;
if (numPages <= 0) {
throw new bot.Error(bot.ErrorCode.UNKNOWN_ERROR,
'number of pages must be positive');
}
if (maxPages !== null && numPages > maxPages) {
throw new bot.Error(bot.ErrorCode.UNKNOWN_ERROR,
'number of pages must be less than the length of the browser history');
}
return numPages;
};
/**
* Determine the size of the window that a user could interact with. This will
* be the greatest of document.body.(width|scrollWidth), the same for
* document.documentElement or the size of the viewport.
*
* @param {!Window=} opt_win Window to determine the size of. Defaults to
* bot.getWindow().
* @return {!goog.math.Size} The calculated size.
*/
bot.window.getInteractableSize = function (opt_win) {
var win = opt_win || bot.getWindow();
var doc = win.document;
var elem = doc.documentElement;
var body = doc.body;
if (!body) {
throw new bot.Error(bot.ErrorCode.UNKNOWN_ERROR,
'No BODY element present');
}
var widths = [
elem.clientWidth, elem.scrollWidth, elem.offsetWidth,
body.scrollWidth, body.offsetWidth
];
var heights = [
elem.clientHeight, elem.scrollHeight, elem.offsetHeight,
body.scrollHeight, body.offsetHeight
];
var width = Math.max.apply(null, widths);
var height = Math.max.apply(null, heights);
return new goog.math.Size(width, height);
};
/**
* Gets the frame element.
*
* @param {!Window} win Window of the frame. Defaults to bot.getWindow().
* @return {Element} The frame element if it exists, null otherwise.
* @private
*/
bot.window.getFrame_ = function (win) {
try {
// On IE, accessing the frameElement of a popup window results in a "No
// Such interface" exception.
return win.frameElement;
} catch (e) {
return null;
}
};
/**
* Determine the outer size of the window.
*
* @param {!Window=} opt_win Window to determine the size of. Defaults to
* bot.getWindow().
* @return {!goog.math.Size} The calculated size.
*/
bot.window.getSize = function (opt_win) {
var win = opt_win || bot.getWindow();
var frame = bot.window.getFrame_(win);
if (bot.userAgent.ANDROID_PRE_ICECREAMSANDWICH) {
if (frame) {
// Early Android browsers do not account for border width.
var box = goog.style.getBorderBox(frame);
return new goog.math.Size(frame.clientWidth - box.left - box.right,
frame.clientHeight);
} else {
// A fixed popup size.
return new goog.math.Size(320, 240);
}
} else if (frame) {
return new goog.math.Size(frame.clientWidth, frame.clientHeight);
} else {
var docElem = win.document.documentElement;
var body = win.document.body;
var width = win.outerWidth || (docElem && docElem.clientWidth) ||
(body && body.clientWidth) || 0;
var height = win.outerHeight || (docElem && docElem.clientHeight) ||
(body && body.clientHeight) || 0;
return new goog.math.Size(width, height);
}
};
/**
* Set the outer size of the window.
*
* @param {!goog.math.Size} size The new window size.
* @param {!Window=} opt_win Window to determine the size of. Defaults to
* bot.getWindow().
*/
bot.window.setSize = function (size, opt_win) {
var win = opt_win || bot.getWindow();
var frame = bot.window.getFrame_(win);
if (frame) {
// minHeight and minWidth are altered because many browsers will not change
// height or width if it is less than a specified minHeight or minWidth.
frame.style.minHeight = '0px';
frame.style.minWidth = '0px';
frame.width = size.width + 'px';
frame.style.width = size.width + 'px';
frame.height = size.height + 'px';
frame.style.height = size.height + 'px';
} else {
win.resizeTo(size.width, size.height);
}
};
/**
* Determine the scroll position of the window.
*
* @param {!Window=} opt_win Window to determine the scroll position of.
* Defaults to bot.getWindow().
* @return {!goog.math.Coordinate} The scroll position.
*/
bot.window.getScroll = function (opt_win) {
var win = opt_win || bot.getWindow();
return new goog.dom.DomHelper(win.document).getDocumentScroll();
};
/**
* Set the scroll position of the window.
*
* @param {!goog.math.Coordinate} position The new scroll position.
* @param {!Window=} opt_win Window to apply position to. Defaults to
* bot.getWindow().
*/
bot.window.setScroll = function (position, opt_win) {
var win = opt_win || bot.getWindow();
win.scrollTo(position.x, position.y);
};
/**
* Get the position of the window.
*
* @param {!Window=} opt_win Window to determine the position of. Defaults to
* bot.getWindow().
* @return {!goog.math.Coordinate} The position of the window.
*/
bot.window.getPosition = function (opt_win) {
var win = opt_win || bot.getWindow();
var x, y;
if (goog.userAgent.IE) {
x = win.screenLeft;
y = win.screenTop;
} else {
x = win.screenX;
y = win.screenY;
}
return new goog.math.Coordinate(x, y);
};
/**
* Set the position of the window.
*
* @param {!goog.math.Coordinate} position The target position.
* @param {!Window=} opt_win Window to set the position of. Defaults to
* bot.getWindow().
*/
bot.window.setPosition = function (position, opt_win) {
var win = opt_win || bot.getWindow();
win.moveTo(position.x, position.y);
};
/**
* Scrolls the given position into the viewport, using the minimal amount of
* scrolling necessary to being the coordinate into view.
*
* @param {!goog.math.Coordinate} position The position to scroll into view.
* @param {!Window=} opt_win Window to apply position to. Defaults to
* bot.getWindow().
*/
bot.window.scrollIntoView = function (position, opt_win) {
var win = opt_win || bot.getWindow();
var viewport = goog.dom.getViewportSize(win);
var scroll = bot.window.getScroll(win);
// Scroll the minimal amount to bring the position into view.
var targetScroll = new goog.math.Coordinate(
newScrollDim(position.x, scroll.x, viewport.width),
newScrollDim(position.y, scroll.y, viewport.height));
if (!goog.math.Coordinate.equals(targetScroll, scroll)) {
bot.window.setScroll(targetScroll, win);
}
// It is difficult to determine the size of the web page in some browsers.
// We check if the scrolling we intended to do really happened. If not we
// assume that the target location is not on the web page.
if (!goog.math.Coordinate.equals(targetScroll, bot.window.getScroll(win))) {
throw new bot.Error(bot.ErrorCode.MOVE_TARGET_OUT_OF_BOUNDS,
'The target scroll location ' + targetScroll + ' is not on the page.');
}
function newScrollDim(positionDim, scrollDim, viewportDim) {
if (positionDim < scrollDim) {
return positionDim;
} else if (positionDim >= scrollDim + viewportDim) {
return positionDim - viewportDim + 1;
} else {
return scrollDim;
}
}
};
/**
* @return {number} The current window orientation degrees.
* window.
* @private
*/
bot.window.getCurrentOrientationDegrees_ = function () {
var win = bot.getWindow();
if (!goog.isDef(win.orientation)) {
// If window.orientation is not defined, assume a default orientation of 0.
// A value of 0 indicates a portrait orientation except for android tablets
// where 0 indicates a landscape orientation.
win.orientation = 0;
}
return win.orientation;
};
/**
* Changes window orientation.
*
* @param {!bot.window.Orientation} orientation The new orientation of the
* window.
*/
bot.window.changeOrientation = function (orientation) {
var win = bot.getWindow();
var currentOrientationDegrees = bot.window.getCurrentOrientationDegrees_();
var newOrientationDegrees = bot.window.getOrientationDegrees_(orientation);
if (currentOrientationDegrees == newOrientationDegrees ||
!goog.isDef(newOrientationDegrees)) {
return;
}
// If possible, try to override the window's orientation value.
// On some older version of Android, it's not possible to change
// the window's orientation value.
if (Object.getOwnPropertyDescriptor && Object.defineProperty) {
var descriptor = Object.getOwnPropertyDescriptor(win, 'orientation');
if (descriptor && descriptor.configurable) {
Object.defineProperty(win, 'orientation', {
configurable: true,
get: function () {
return newOrientationDegrees;
}
});
}
}
bot.events.fire(win, bot.events.EventType.ORIENTATIONCHANGE);
// Change the window size to reflect the new orientation.
if (Math.abs(currentOrientationDegrees - newOrientationDegrees) % 180 != 0) {
var size = bot.window.getSize();
var shorter = size.getShortest();
var longer = size.getLongest();
if (orientation == bot.window.Orientation.PORTRAIT ||
orientation == bot.window.Orientation.PORTRAIT_SECONDARY) {
bot.window.setSize(new goog.math.Size(shorter, longer));
} else {
bot.window.setSize(new goog.math.Size(longer, shorter));
}
}
};