-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathactionsequence.js
356 lines (317 loc) · 13.2 KB
/
actionsequence.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
// 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.
goog.provide('webdriver.ActionSequence');
goog.require('goog.array');
goog.require('webdriver.Button');
goog.require('webdriver.Command');
goog.require('webdriver.CommandName');
goog.require('webdriver.Key');
/**
* Class for defining sequences of complex user interactions. Each sequence
* will not be executed until {@link #perform} is called.
*
* Example:
*
* new webdriver.ActionSequence(driver).
* keyDown(webdriver.Key.SHIFT).
* click(element1).
* click(element2).
* dragAndDrop(element3, element4).
* keyUp(webdriver.Key.SHIFT).
* perform();
*
* @param {!webdriver.WebDriver} driver The driver instance to use.
* @constructor
*/
webdriver.ActionSequence = function(driver) {
/** @private {!webdriver.WebDriver} */
this.driver_ = driver;
/** @private {!Array.<{description: string, command: !webdriver.Command}>} */
this.actions_ = [];
};
/**
* Schedules an action to be executed each time {@link #perform} is called on
* this instance.
* @param {string} description A description of the command.
* @param {!webdriver.Command} command The command.
* @private
*/
webdriver.ActionSequence.prototype.schedule_ = function(description, command) {
this.actions_.push({
description: description,
command: command
});
};
/**
* Executes this action sequence.
* @return {!webdriver.promise.Promise} A promise that will be resolved once
* this sequence has completed.
*/
webdriver.ActionSequence.prototype.perform = function() {
// Make a protected copy of the scheduled actions. This will protect against
// users defining additional commands before this sequence is actually
// executed.
var actions = goog.array.clone(this.actions_);
var driver = this.driver_;
return driver.controlFlow().execute(function() {
goog.array.forEach(actions, function(action) {
driver.schedule(action.command, action.description);
});
}, 'ActionSequence.perform');
};
/**
* Moves the mouse. The location to move to may be specified in terms of the
* mouse's current location, an offset relative to the top-left corner of an
* element, or an element (in which case the middle of the element is used).
* @param {(!webdriver.WebElement|{x: number, y: number})} location The
* location to drag to, as either another WebElement or an offset in pixels.
* @param {{x: number, y: number}=} opt_offset If the target {@code location}
* is defined as a {@link webdriver.WebElement}, this parameter defines an
* offset within that element. The offset should be specified in pixels
* relative to the top-left corner of the element's bounding box. If
* omitted, the element's center will be used as the target offset.
* @return {!webdriver.ActionSequence} A self reference.
*/
webdriver.ActionSequence.prototype.mouseMove = function(location, opt_offset) {
var command = new webdriver.Command(webdriver.CommandName.MOVE_TO);
if (goog.isNumber(location.x)) {
setOffset(/** @type {{x: number, y: number}} */(location));
} else {
command.setParameter('element', location.getRawId());
if (opt_offset) {
setOffset(opt_offset);
}
}
this.schedule_('mouseMove', command);
return this;
/** @param {{x: number, y: number}} offset The offset to use. */
function setOffset(offset) {
command.setParameter('xoffset', offset.x || 0);
command.setParameter('yoffset', offset.y || 0);
}
};
/**
* Schedules a mouse action.
* @param {string} description A simple descriptive label for the scheduled
* action.
* @param {!webdriver.CommandName} commandName The name of the command.
* @param {(webdriver.WebElement|webdriver.Button)=} opt_elementOrButton Either
* the element to interact with or the button to click with.
* Defaults to {@link webdriver.Button.LEFT} if neither an element nor
* button is specified.
* @param {webdriver.Button=} opt_button The button to use. Defaults to
* {@link webdriver.Button.LEFT}. Ignored if the previous argument is
* provided as a button.
* @return {!webdriver.ActionSequence} A self reference.
* @private
*/
webdriver.ActionSequence.prototype.scheduleMouseAction_ = function(
description, commandName, opt_elementOrButton, opt_button) {
var button;
if (goog.isNumber(opt_elementOrButton)) {
button = opt_elementOrButton;
} else {
if (opt_elementOrButton) {
this.mouseMove(
/** @type {!webdriver.WebElement} */ (opt_elementOrButton));
}
button = goog.isDef(opt_button) ? opt_button : webdriver.Button.LEFT;
}
var command = new webdriver.Command(commandName).
setParameter('button', button);
this.schedule_(description, command);
return this;
};
/**
* Presses a mouse button. The mouse button will not be released until
* {@link #mouseUp} is called, regardless of whether that call is made in this
* sequence or another. The behavior for out-of-order events (e.g. mouseDown,
* click) is undefined.
*
* If an element is provided, the mouse will first be moved to the center
* of that element. This is equivalent to:
*
* sequence.mouseMove(element).mouseDown()
*
* Warning: this method currently only supports the left mouse button. See
* [issue 4047](http://code.google.com/p/selenium/issues/detail?id=4047).
*
* @param {(webdriver.WebElement|webdriver.Button)=} opt_elementOrButton Either
* the element to interact with or the button to click with.
* Defaults to {@link webdriver.Button.LEFT} if neither an element nor
* button is specified.
* @param {webdriver.Button=} opt_button The button to use. Defaults to
* {@link webdriver.Button.LEFT}. Ignored if a button is provided as the
* first argument.
* @return {!webdriver.ActionSequence} A self reference.
*/
webdriver.ActionSequence.prototype.mouseDown = function(opt_elementOrButton,
opt_button) {
return this.scheduleMouseAction_('mouseDown',
webdriver.CommandName.MOUSE_DOWN, opt_elementOrButton, opt_button);
};
/**
* Releases a mouse button. Behavior is undefined for calling this function
* without a previous call to {@link #mouseDown}.
*
* If an element is provided, the mouse will first be moved to the center
* of that element. This is equivalent to:
*
* sequence.mouseMove(element).mouseUp()
*
* Warning: this method currently only supports the left mouse button. See
* [issue 4047](http://code.google.com/p/selenium/issues/detail?id=4047).
*
* @param {(webdriver.WebElement|webdriver.Button)=} opt_elementOrButton Either
* the element to interact with or the button to click with.
* Defaults to {@link webdriver.Button.LEFT} if neither an element nor
* button is specified.
* @param {webdriver.Button=} opt_button The button to use. Defaults to
* {@link webdriver.Button.LEFT}. Ignored if a button is provided as the
* first argument.
* @return {!webdriver.ActionSequence} A self reference.
*/
webdriver.ActionSequence.prototype.mouseUp = function(opt_elementOrButton,
opt_button) {
return this.scheduleMouseAction_('mouseUp',
webdriver.CommandName.MOUSE_UP, opt_elementOrButton, opt_button);
};
/**
* Convenience function for performing a "drag and drop" manuever. The target
* element may be moved to the location of another element, or by an offset (in
* pixels).
* @param {!webdriver.WebElement} element The element to drag.
* @param {(!webdriver.WebElement|{x: number, y: number})} location The
* location to drag to, either as another WebElement or an offset in pixels.
* @return {!webdriver.ActionSequence} A self reference.
*/
webdriver.ActionSequence.prototype.dragAndDrop = function(element, location) {
return this.mouseDown(element).mouseMove(location).mouseUp();
};
/**
* Clicks a mouse button.
*
* If an element is provided, the mouse will first be moved to the center
* of that element. This is equivalent to:
*
* sequence.mouseMove(element).click()
*
* @param {(webdriver.WebElement|webdriver.Button)=} opt_elementOrButton Either
* the element to interact with or the button to click with.
* Defaults to {@link webdriver.Button.LEFT} if neither an element nor
* button is specified.
* @param {webdriver.Button=} opt_button The button to use. Defaults to
* {@link webdriver.Button.LEFT}. Ignored if a button is provided as the
* first argument.
* @return {!webdriver.ActionSequence} A self reference.
*/
webdriver.ActionSequence.prototype.click = function(opt_elementOrButton,
opt_button) {
return this.scheduleMouseAction_('click',
webdriver.CommandName.CLICK, opt_elementOrButton, opt_button);
};
/**
* Double-clicks a mouse button.
*
* If an element is provided, the mouse will first be moved to the center of
* that element. This is equivalent to:
*
* sequence.mouseMove(element).doubleClick()
*
* Warning: this method currently only supports the left mouse button. See
* [issue 4047](http://code.google.com/p/selenium/issues/detail?id=4047).
*
* @param {(webdriver.WebElement|webdriver.Button)=} opt_elementOrButton Either
* the element to interact with or the button to click with.
* Defaults to {@link webdriver.Button.LEFT} if neither an element nor
* button is specified.
* @param {webdriver.Button=} opt_button The button to use. Defaults to
* {@link webdriver.Button.LEFT}. Ignored if a button is provided as the
* first argument.
* @return {!webdriver.ActionSequence} A self reference.
*/
webdriver.ActionSequence.prototype.doubleClick = function(opt_elementOrButton,
opt_button) {
return this.scheduleMouseAction_('doubleClick',
webdriver.CommandName.DOUBLE_CLICK, opt_elementOrButton, opt_button);
};
/**
* Schedules a keyboard action.
* @param {string} description A simple descriptive label for the scheduled
* action.
* @param {!Array.<(string|!webdriver.Key)>} keys The keys to send.
* @return {!webdriver.ActionSequence} A self reference.
* @private
*/
webdriver.ActionSequence.prototype.scheduleKeyboardAction_ = function(
description, keys) {
var command =
new webdriver.Command(webdriver.CommandName.SEND_KEYS_TO_ACTIVE_ELEMENT).
setParameter('value', keys);
this.schedule_(description, command);
return this;
};
/**
* Checks that a key is a modifier key.
* @param {!webdriver.Key} key The key to check.
* @throws {Error} If the key is not a modifier key.
* @private
*/
webdriver.ActionSequence.checkModifierKey_ = function(key) {
if (key !== webdriver.Key.ALT && key !== webdriver.Key.CONTROL &&
key !== webdriver.Key.SHIFT && key !== webdriver.Key.COMMAND) {
throw Error('Not a modifier key');
}
};
/**
* Performs a modifier key press. The modifier key is <em>not released</em>
* until {@link #keyUp} or {@link #sendKeys} is called. The key press will be
* targetted at the currently focused element.
* @param {!webdriver.Key} key The modifier key to push. Must be one of
* {ALT, CONTROL, SHIFT, COMMAND, META}.
* @return {!webdriver.ActionSequence} A self reference.
* @throws {Error} If the key is not a valid modifier key.
*/
webdriver.ActionSequence.prototype.keyDown = function(key) {
webdriver.ActionSequence.checkModifierKey_(key);
return this.scheduleKeyboardAction_('keyDown', [key]);
};
/**
* Performs a modifier key release. The release is targetted at the currently
* focused element.
* @param {!webdriver.Key} key The modifier key to release. Must be one of
* {ALT, CONTROL, SHIFT, COMMAND, META}.
* @return {!webdriver.ActionSequence} A self reference.
* @throws {Error} If the key is not a valid modifier key.
*/
webdriver.ActionSequence.prototype.keyUp = function(key) {
webdriver.ActionSequence.checkModifierKey_(key);
return this.scheduleKeyboardAction_('keyUp', [key]);
};
/**
* Simulates typing multiple keys. Each modifier key encountered in the
* sequence will not be released until it is encountered again. All key events
* will be targetted at the currently focused element.
* @param {...(string|!webdriver.Key|!Array.<(string|!webdriver.Key)>)} var_args
* The keys to type.
* @return {!webdriver.ActionSequence} A self reference.
* @throws {Error} If the key is not a valid modifier key.
*/
webdriver.ActionSequence.prototype.sendKeys = function(var_args) {
var keys = goog.array.flatten(goog.array.slice(arguments, 0));
return this.scheduleKeyboardAction_('sendKeys', keys);
};