forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.js
More file actions
244 lines (208 loc) · 7.51 KB
/
Copy pathwindow.js
File metadata and controls
244 lines (208 loc) · 7.51 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
// 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 A utility class for working with test windows.
*/
goog.provide('webdriver.testing.Window');
goog.require('goog.string');
goog.require('webdriver.promise');
/**
* Class for managing a window.
*
* This class is implemented as a promise so consumers may register
* callbacks on it to handle situations where the window fails to open.
*
* For example:
*
* var testWindow = webdriver.testing.Window.create(driver);
* // Throw a custom error when the window fails to open.
* testWindow.thenCatch(function(e) {
* throw Error('Failed to open test window: ' + e);
* });
*
* @param {!webdriver.WebDriver} driver The driver to use.
* @param {(string|!webdriver.promise.Promise)} handle Either the managed
* window's handle, or a promise that will resolve to it.
* @param {(Window|webdriver.promise.Promise)=} opt_window The raw window
* object, if available.
* @constructor
* @extends {webdriver.promise.Promise}
*/
webdriver.testing.Window = function(driver, handle, opt_window) {
webdriver.promise.Promise.call(this, function(fulfill, reject) {
handle.then(fulfill, reject);
});
/** @private {!webdriver.WebDriver} */
this.driver_ = driver;
/** @private {!webdriver.promise.Promise} */
this.handle_ = webdriver.promise.when(handle);
/** @private {!webdriver.promise.Promise} */
this.window_ = webdriver.promise.when(opt_window);
};
goog.inherits(webdriver.testing.Window, webdriver.promise.Promise);
/**
* Default amount of time, in milliseconds, to wait for a new window to open.
* @type {number}
* @const
*/
webdriver.testing.Window.DEFAULT_OPEN_TIMEOUT = 2000;
/**
* Window running this script. Lazily initialized the first time it is requested
* from {@link webdriver.testing.Window.findWindow}.
* @private {webdriver.testing.Window}
*/
webdriver.testing.Window.currentWindow_ = null;
/**
* Opens and focuses on a new window.
*
* @param {!webdriver.WebDriver} driver The driver to use.
* @param {?{width: number, height: number}=} opt_size The desired size for
* the new window.
* @param {number=} opt_timeout How long, in milliseconds, to wait for the new
* window to open. Defaults to
* {@link webdriver.testing.Window.DEFAULT_OPEN_TIMEOUT}.
* @return {!webdriver.testing.Window} The new window.
*/
webdriver.testing.Window.create = function(driver, opt_size, opt_timeout) {
var windowPromise = webdriver.promise.defer();
var handle = driver.call(function() {
var features = [
'location=yes',
'titlebar=yes'
];
if (opt_size) {
features.push(
'width=' + opt_size.width,
'height=' + opt_size.height);
}
var name = goog.string.getRandomString();
windowPromise.fulfill(window.open('', name, features.join(',')));
driver.wait(function() {
return driver.switchTo().window(name).then(
function() { return true; },
function() { return false; });
}, opt_timeout || webdriver.testing.Window.DEFAULT_OPEN_TIMEOUT);
return driver.getWindowHandle();
});
return new webdriver.testing.Window(driver, handle, windowPromise);
};
/**
* Changes focus to the topmost window for the provided DOM window.
*
* @param {!webdriver.WebDriver} driver The driver to use.
* @param {Window=} opt_window The window to search for. Defaults to the window
* running this script.
* @return {!webdriver.testing.Window} The located window.
*/
webdriver.testing.Window.focusOnWindow = function(driver, opt_window) {
var name = goog.string.getRandomString();
var win = opt_window ? opt_window.top : window;
var ret;
if (win === window && webdriver.testing.Window.currentWindow_) {
ret = webdriver.testing.Window.currentWindow_;
webdriver.testing.Window.currentWindow_.focus();
} else {
win.name = name;
ret = new webdriver.testing.Window(driver,
driver.switchTo().window(name).
then(goog.bind(driver.getWindowHandle, driver)),
win);
if (win === window) {
webdriver.testing.Window.currentWindow_ = ret;
}
}
return ret;
};
/** @override */
webdriver.testing.Window.prototype.cancel = function() {
return this.handle_.cancel();
};
/**
* Focuses the wrapped driver on the window managed by this class.
* @return {!webdriver.promise.Promise} A promise that will be resolved
* when this command has completed.
*/
webdriver.testing.Window.prototype.focus = function() {
return this.driver_.switchTo().window(this.handle_);
};
/**
* Focuses on and closes the managed window. The driver <em>must</em> be
* focused on another window before issuing any further commands.
* @return {!webdriver.promise.Promise} A promise that will be resolved
* when this command has completed.
*/
webdriver.testing.Window.prototype.close = function() {
var self = this;
return this.window_.then(function(win) {
if (win) {
win.close();
} else {
return self.focus().then(goog.bind(self.driver_.close, self.driver_));
}
});
};
/**
* Retrieves the current size of this window.
* @return {!webdriver.promise.Promise} A promise that resolves to the size of
* this window as a {width:number, height:number} object.
*/
webdriver.testing.Window.prototype.getSize = function() {
var driver = this.driver_;
return this.focus().then(function() {
return driver.manage().window().getSize();
});
};
/**
* Sets the size of this window.
* @param {number} width The desired width, in pixels.
* @param {number} height The desired height, in pixels.
* @return {!webdriver.promise.Promise} A promise that resolves when the
* command has completed.
*/
webdriver.testing.Window.prototype.setSize = function(width, height) {
var driver = this.driver_;
return this.focus().then(function() {
return driver.manage().window().setSize(width, height);
});
};
/**
* Retrieves the current position of this window, in pixels relative to the
* upper left corner of the screen.
* @return {!webdriver.promise.Promise} A promise that resolves to the
* position of this window as a {x:number, y:number} object.
*/
webdriver.testing.Window.prototype.getPosition = function() {
var driver = this.driver_;
return this.focus().then(function() {
return driver.manage().window().getPosition();
});
};
/**
* Repositions this window.
* @param {number} x The desired horizontal position, in pixels from the left
* side of the screen.
* @param {number} y The desired vertical position, in pixels from the top
* of the screen.
* @return {!webdriver.promise.Promise} A promise that resolves when the
* command has completed.
*/
webdriver.testing.Window.prototype.setPosition = function(x, y) {
var driver = this.driver_;
return this.focus().then(function() {
return driver.manage().window().setPosition(x, y);
});
};