forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.js
701 lines (628 loc) · 25.8 KB
/
action.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
// Copyright 2010 WebDriver committers
// Copyright 2010 Google Inc.
//
// Licensed 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 DOM.
* The bot.action namespace is required since these atoms would otherwise form a
* circular dependency between bot.dom and bot.events.
*
*/
goog.provide('bot.action');
goog.require('bot');
goog.require('bot.Device');
goog.require('bot.Error');
goog.require('bot.ErrorCode');
goog.require('bot.Keyboard');
goog.require('bot.Mouse');
goog.require('bot.Touchscreen');
goog.require('bot.dom');
goog.require('bot.events');
goog.require('bot.events.EventType');
goog.require('goog.array');
goog.require('goog.math.Coordinate');
goog.require('goog.math.Vec2');
goog.require('goog.style');
/**
* Throws an exception if an element is not shown to the user, ignoring its
* opacity.
*
* @param {!Element} element The element to check.
* @see bot.dom.isShown.
* @private
*/
bot.action.checkShown_ = function(element) {
if (!bot.dom.isShown(element, /*ignoreOpacity=*/true)) {
throw new bot.Error(bot.ErrorCode.ELEMENT_NOT_VISIBLE,
'Element is not currently visible and may not be manipulated');
}
};
/**
* Throws an exception if the given element cannot be interacted with.
*
* @param {!Element} element The element to check.
* @throws {bot.Error} If the element cannot be interacted with.
* @see bot.dom.isInteractable.
* @private
*/
bot.action.checkInteractable_ = function(element) {
if (!bot.dom.isInteractable(element)) {
throw new bot.Error(bot.ErrorCode.INVALID_ELEMENT_STATE,
'Element is not currently interactable and may not be manipulated');
}
};
/**
* Clears the given {@code element} if it is a editable text field.
*
* @param {!Element} element The element to clear.
* @throws {bot.Error} If the element is not an editable text field.
*/
bot.action.clear = function(element) {
bot.action.checkInteractable_(element);
if (!bot.dom.isEditable(element)) {
throw new bot.Error(bot.ErrorCode.INVALID_ELEMENT_STATE,
'Element must be user-editable in order to clear it.');
}
bot.action.LegacyDevice_.focusOnElement(element);
if (element.value) {
element.value = '';
bot.events.fire(element, bot.events.EventType.CHANGE);
}
if (bot.dom.isContentEditable(element)) {
// A single space is required, if you put empty string here you'll not be
// able to interact with this element anymore in Firefox.
element.innerHTML = ' ';
// contentEditable does not generate onchange event.
}
};
/**
* Focuses on the given element if it is not already the active element.
*
* @param {!Element} element The element to focus on.
*/
bot.action.focusOnElement = function(element) {
bot.action.checkInteractable_(element);
bot.action.LegacyDevice_.focusOnElement(element);
};
/**
* Types keys on the given {@code element} with a virtual keyboard.
*
* <p>Callers can pass in a string, a key in bot.Keyboard.Key, or an array
* of strings or keys. If a modifier key is provided, it is pressed but not
* released, until it is either is listed again or the function ends.
*
* <p>Example:
* bot.keys.type(element, ['ab', bot.Keyboard.Key.LEFT,
* bot.Keyboard.Key.SHIFT, 'cd']);
*
* @param {!Element} element The element receiving the event.
* @param {(string|!bot.Keyboard.Key|!Array.<(string|!bot.Keyboard.Key)>)}
* values Value or values to type on the element.
* @param {bot.Keyboard=} opt_keyboard Keyboard to use; if not provided,
* constructs one.
* @param {boolean=} opt_persistModifiers Whether modifier keys should remain
* pressed when this function ends.
* @throws {bot.Error} If the element cannot be interacted with.
*/
bot.action.type = function(
element, values, opt_keyboard, opt_persistModifiers) {
// If the element has already been brought into focus somewhow, typing is
// always allowed to proceed. Otherwise, we require the element be in an
// "interactable" state. For example, an element that is hidden by overflow
// can be typed on, so long as the user first tabs to it or the app calls
// focus() on the element first.
if (element != bot.dom.getActiveElement(element)) {
bot.action.checkInteractable_(element);
bot.action.scrollIntoView(element);
}
var keyboard = opt_keyboard || new bot.Keyboard();
keyboard.moveCursor(element);
function typeValue(value) {
if (goog.isString(value)) {
goog.array.forEach(value.split(''), function(ch) {
var keyShiftPair = bot.Keyboard.Key.fromChar(ch);
var shiftIsPressed = keyboard.isPressed(bot.Keyboard.Keys.SHIFT);
if (keyShiftPair.shift && !shiftIsPressed) {
keyboard.pressKey(bot.Keyboard.Keys.SHIFT);
}
keyboard.pressKey(keyShiftPair.key);
keyboard.releaseKey(keyShiftPair.key);
if (keyShiftPair.shift && !shiftIsPressed) {
keyboard.releaseKey(bot.Keyboard.Keys.SHIFT);
}
});
} else if (goog.array.contains(bot.Keyboard.MODIFIERS, value)) {
if (keyboard.isPressed(value)) {
keyboard.releaseKey(value);
} else {
keyboard.pressKey(value);
}
} else {
keyboard.pressKey(value);
keyboard.releaseKey(value);
}
}
// mobile safari (iPhone / iPad). one cannot 'type' in a date field
// chrome implements this, but desktop Safari doesn't, what's webkit again?
if ((!(goog.userAgent.product.SAFARI && !goog.userAgent.MOBILE)) &&
goog.userAgent.WEBKIT && element.type == 'date') {
var val = goog.isArray(values)? values = values.join("") : values;
var datePattern = /\d{4}-\d{2}-\d{2}/;
if (val.match(datePattern)) {
// The following events get fired on iOS first
if (goog.userAgent.MOBILE && goog.userAgent.product.SAFARI) {
bot.events.fire(element, bot.events.EventType.TOUCHSTART);
bot.events.fire(element, bot.events.EventType.TOUCHEND);
}
bot.events.fire(element, bot.events.EventType.FOCUS);
element.value = val.match(datePattern)[0];
bot.events.fire(element, bot.events.EventType.CHANGE);
bot.events.fire(element, bot.events.EventType.BLUR);
return;
}
}
if (goog.isArray(values)) {
goog.array.forEach(values, typeValue);
} else {
typeValue(values);
}
if (!opt_persistModifiers) {
// Release all the modifier keys.
goog.array.forEach(bot.Keyboard.MODIFIERS, function(key) {
if (keyboard.isPressed(key)) {
keyboard.releaseKey(key);
}
});
}
};
/**
* Submits the form containing the given {@code element}.
*
* <p>Note this function submits the form, but does not simulate user input
* (a click or key press).
*
* @param {!Element} element The element to submit.
* @deprecated Click on a submit button or type ENTER in a text box instead.
*/
bot.action.submit = function(element) {
var form = bot.action.LegacyDevice_.findAncestorForm(element);
if (!form) {
throw new bot.Error(bot.ErrorCode.NO_SUCH_ELEMENT,
'Element was not in a form, so could not submit.');
}
bot.action.LegacyDevice_.submitForm(element, form);
};
/**
* Moves the mouse over the given {@code element} with a virtual mouse.
*
* @param {!Element} element The element to click.
* @param {goog.math.Coordinate=} opt_coords Mouse position relative to the
* element.
* @param {bot.Mouse=} opt_mouse Mouse to use; if not provided, constructs one.
* @throws {bot.Error} If the element cannot be interacted with.
*/
bot.action.moveMouse = function(element, opt_coords, opt_mouse) {
var coords = bot.action.prepareToInteractWith_(element, opt_coords);
var mouse = opt_mouse || new bot.Mouse();
mouse.move(element, coords);
};
/**
* Clicks on the given {@code element} with a virtual mouse.
*
* @param {!Element} element The element to click.
* @param {goog.math.Coordinate=} opt_coords Mouse position relative to the
* element.
* @param {bot.Mouse=} opt_mouse Mouse to use; if not provided, constructs one.
* @throws {bot.Error} If the element cannot be interacted with.
*/
bot.action.click = function(element, opt_coords, opt_mouse) {
var coords = bot.action.prepareToInteractWith_(element, opt_coords);
var mouse = opt_mouse || new bot.Mouse();
mouse.move(element, coords);
mouse.pressButton(bot.Mouse.Button.LEFT);
mouse.releaseButton();
};
/**
* Right-clicks on the given {@code element} with a virtual mouse.
*
* @param {!Element} element The element to click.
* @param {goog.math.Coordinate=} opt_coords Mouse position relative to the
* element.
* @param {bot.Mouse=} opt_mouse Mouse to use; if not provided, constructs one.
* @throws {bot.Error} If the element cannot be interacted with.
*/
bot.action.rightClick = function(element, opt_coords, opt_mouse) {
var coords = bot.action.prepareToInteractWith_(element, opt_coords);
var mouse = opt_mouse || new bot.Mouse();
mouse.move(element, coords);
mouse.pressButton(bot.Mouse.Button.RIGHT);
mouse.releaseButton();
};
/**
* Double-clicks on the given {@code element} with a virtual mouse.
*
* @param {!Element} element The element to click.
* @param {goog.math.Coordinate=} opt_coords Mouse position relative to the
* element.
* @param {bot.Mouse=} opt_mouse Mouse to use; if not provided, constructs one.
* @throws {bot.Error} If the element cannot be interacted with.
*/
bot.action.doubleClick = function(element, opt_coords, opt_mouse) {
var coords = bot.action.prepareToInteractWith_(element, opt_coords);
var mouse = opt_mouse || new bot.Mouse();
mouse.move(element, coords);
mouse.pressButton(bot.Mouse.Button.LEFT);
mouse.releaseButton();
mouse.pressButton(bot.Mouse.Button.LEFT);
mouse.releaseButton();
};
/**
* Scrolls the mouse wheel on the given {@code element} with a virtual mouse.
*
* @param {!Element} element The element to scroll the mouse wheel on.
* @param {number} ticks Number of ticks to scroll the mouse wheel; a positive
* number scrolls down and a negative scrolls up.
* @param {goog.math.Coordinate=} opt_coords Mouse position relative to the
* element.
* @param {bot.Mouse=} opt_mouse Mouse to use; if not provided, constructs one.
* @throws {bot.Error} If the element cannot be interacted with.
*/
bot.action.scrollMouse = function(element, ticks, opt_coords, opt_mouse) {
var coords = bot.action.prepareToInteractWith_(element, opt_coords);
var mouse = opt_mouse || new bot.Mouse();
mouse.move(element, coords);
mouse.scroll(ticks);
};
/**
* Drags the given {@code element} by (dx, dy) with a virtual mouse.
*
* @param {!Element} element The element to drag.
* @param {number} dx Increment in x coordinate.
* @param {number} dy Increment in y coordinate.
* @param {number=} opt_steps The number of steps that should occur as part of
* the drag, default is 2.
* @param {goog.math.Coordinate=} opt_coords Drag start position relative to the
* element.
* @param {bot.Mouse=} opt_mouse Mouse to use; if not provided, constructs one.
* @throws {bot.Error} If the element cannot be interacted with.
*/
bot.action.drag = function(element, dx, dy, opt_steps, opt_coords, opt_mouse) {
var coords = bot.action.prepareToInteractWith_(element, opt_coords);
var initRect = bot.dom.getClientRect(element);
var mouse = opt_mouse || new bot.Mouse();
mouse.move(element, coords);
mouse.pressButton(bot.Mouse.Button.LEFT);
var steps = goog.isDef(opt_steps) ? opt_steps : 2;
if (steps < 1) {
throw new bot.Error(bot.ErrorCode.UNKNOWN_ERROR,
'There must be at least one step as part of a drag.');
}
for (var i = 1; i <= steps; i++) {
moveTo(Math.floor(i * dx / steps), Math.floor(i * dy / steps));
}
mouse.releaseButton();
function moveTo(x, y) {
var currRect = bot.dom.getClientRect(element);
var newPos = new goog.math.Coordinate(
coords.x + initRect.left + x - currRect.left,
coords.y + initRect.top + y - currRect.top);
mouse.move(element, newPos);
}
};
/**
* Taps on the given {@code element} with a virtual touch screen.
*
* @param {!Element} element The element to tap.
* @param {goog.math.Coordinate=} opt_coords Finger position relative to the
* target.
* @param {bot.Touchscreen=} opt_touchscreen Touchscreen to use; if not
* provided, constructs one.
* @throws {bot.Error} If the element cannot be interacted with.
*/
bot.action.tap = function(element, opt_coords, opt_touchscreen) {
var coords = bot.action.prepareToInteractWith_(element, opt_coords);
var touchscreen = opt_touchscreen || new bot.Touchscreen();
touchscreen.move(element, coords);
touchscreen.press();
touchscreen.release();
};
/**
* Swipes the given {@code element} by (dx, dy) with a virtual touch screen.
*
* @param {!Element} element The element to swipe.
* @param {number} dx Increment in x coordinate.
* @param {number} dy Increment in y coordinate.
* @param {number=} opt_steps The number of steps that should occurs as part of
* the swipe, default is 2.
* @param {goog.math.Coordinate=} opt_coords Swipe start position relative to
* the element.
* @param {bot.Touchscreen=} opt_touchscreen Touchscreen to use; if not
* provided, constructs one.
* @throws {bot.Error} If the element cannot be interacted with.
*/
bot.action.swipe = function(element, dx, dy, opt_steps, opt_coords,
opt_touchscreen) {
var coords = bot.action.prepareToInteractWith_(element, opt_coords);
var touchscreen = opt_touchscreen || new bot.Touchscreen();
var initRect = bot.dom.getClientRect(element);
touchscreen.move(element, coords);
touchscreen.press();
var steps = goog.isDef(opt_steps) ? opt_steps : 2;
if (steps < 1) {
throw new bot.Error(bot.ErrorCode.UNKNOWN_ERROR,
'There must be at least one step as part of a swipe.');
}
for (var i = 1; i <= steps; i++) {
moveTo(Math.floor(i * dx / steps), Math.floor(i * dy / steps));
}
touchscreen.release();
function moveTo(x, y) {
var currRect = bot.dom.getClientRect(element);
var newPos = new goog.math.Coordinate(
coords.x + initRect.left + x - currRect.left,
coords.y + initRect.top + y - currRect.top);
touchscreen.move(element, newPos);
}
};
/**
* Pinches the given {@code element} by the given distance with a virtual touch
* screen. A positive distance moves two fingers inward toward each and a
* negative distances spreds them outward. The optional coordinate is the point
* the fingers move towards (for positive distances) or away from (for negative
* distances); and if not provided, defaults to the center of the element.
*
* @param {!Element} element The element to pinch.
* @param {number} distance The distance by which to pinch the element.
* @param {goog.math.Coordinate=} opt_coords Position relative to the element
* at the center of the pinch.
* @param {bot.Touchscreen=} opt_touchscreen Touchscreen to use; if not
* provided, constructs one.
* @throws {bot.Error} If the element cannot be interacted with.
*/
bot.action.pinch = function(element, distance, opt_coords, opt_touchscreen) {
if (distance == 0) {
throw new bot.Error(bot.ErrorCode.UNKNOWN_ERROR,
'Cannot pinch by a distance of zero.');
}
function startSoThatEndsAtMax(offsetVec) {
if (distance < 0) {
var magnitude = offsetVec.magnitude();
offsetVec.scale(magnitude ? (magnitude + distance) / magnitude : 0);
}
}
var halfDistance = distance / 2;
function scaleByHalfDistance(offsetVec) {
var magnitude = offsetVec.magnitude();
offsetVec.scale(magnitude ? (magnitude - halfDistance) / magnitude : 0);
}
bot.action.multiTouchAction_(element,
startSoThatEndsAtMax,
scaleByHalfDistance,
opt_coords,
opt_touchscreen);
};
/**
* Rotates the given {@code element} by the given angle with a virtual touch
* screen. A positive angle moves two fingers clockwise and a negative angle
* moves them counter-clockwise. The optional coordinate is the point to
* rotate around; and if not provided, defaults to the center of the element.
*
* @param {!Element} element The element to rotate.
* @param {number} angle The angle by which to rotate the element.
* @param {goog.math.Coordinate=} opt_coords Position relative to the element
* at the center of the rotation.
* @param {bot.Touchscreen=} opt_touchscreen Touchscreen to use; if not
* provided, constructs one.
* @throws {bot.Error} If the element cannot be interacted with.
*/
bot.action.rotate = function(element, angle, opt_coords, opt_touchscreen) {
if (angle == 0) {
throw new bot.Error(bot.ErrorCode.UNKNOWN_ERROR,
'Cannot rotate by an angle of zero.');
}
function startHalfwayToMax(offsetVec) {
offsetVec.scale(0.5);
}
var halfRadians = Math.PI * (angle / 180) / 2;
function rotateByHalfAngle(offsetVec) {
offsetVec.rotate(halfRadians);
}
bot.action.multiTouchAction_(element,
startHalfwayToMax,
rotateByHalfAngle,
opt_coords,
opt_touchscreen);
};
/**
* Performs a multi-touch action with two fingers on the given element. This
* helper function works by manipulating an "offsetVector", which is the vector
* away from the center of the interaction at which the fingers are positioned.
* It computes the maximum offset vector and passes it to transformStart to
* find the starting position of the fingers; it then passes it to transformHalf
* twice to find the midpoint and final position of the fingers.
*
* @param {!Element} element Element to interact with.
* @param {function(goog.math.Vec2)} transformStart Function to transform the
* maximum offset vector to the starting offset vector.
* @param {function(goog.math.Vec2)} transformHalf Function to transform the
* offset vector halfway to its destination.
* @param {goog.math.Coordinate=} opt_coords Position relative to the element
* at the center of the pinch.
* @param {bot.Touchscreen=} opt_touchscreen Touchscreen to use; if not
* provided, constructs one.
* @private
*/
bot.action.multiTouchAction_ = function(element, transformStart, transformHalf,
opt_coords, opt_touchscreen) {
var center = bot.action.prepareToInteractWith_(element, opt_coords);
var size = bot.action.getInteractableSize(element);
var offsetVec = new goog.math.Vec2(
Math.min(center.x, size.width - center.x),
Math.min(center.y, size.height - center.y));
var touchScreen = opt_touchscreen || new bot.Touchscreen();
transformStart(offsetVec);
var start1 = goog.math.Vec2.sum(center, offsetVec);
var start2 = goog.math.Vec2.difference(center, offsetVec);
touchScreen.move(element, start1, start2);
touchScreen.press(/*Two Finger Press*/ true);
var initRect = bot.dom.getClientRect(element);
transformHalf(offsetVec);
var mid1 = goog.math.Vec2.sum(center, offsetVec);
var mid2 = goog.math.Vec2.difference(center, offsetVec);
touchScreen.move(element, mid1, mid2);
var midRect = bot.dom.getClientRect(element);
var movedVec = goog.math.Vec2.difference(
new goog.math.Vec2(midRect.left, midRect.top),
new goog.math.Vec2(initRect.left, initRect.top));
transformHalf(offsetVec);
var end1 = goog.math.Vec2.sum(center, offsetVec).subtract(movedVec);
var end2 = goog.math.Vec2.difference(center, offsetVec).subtract(movedVec);
touchScreen.move(element, end1, end2);
touchScreen.release();
};
/**
* Prepares to interact with the given {@code element}. It checks if the the
* element is shown, scrolls the element into view, and returns the coordinates
* of the interaction, which if not provided, is the center of the element.
*
* @param {!Element} element The element to be interacted with.
* @param {goog.math.Coordinate=} opt_coords Position relative to the target.
* @return {!goog.math.Vec2} Coordinates at the center of the interaction.
* @throws {bot.Error} If the element cannot be interacted with.
* @private
*/
bot.action.prepareToInteractWith_ = function(element, opt_coords) {
bot.action.checkShown_(element);
bot.action.scrollIntoView(element, opt_coords || undefined);
// NOTE: Ideally, we would check that any provided coordinates fall
// within the bounds of the element, but this has proven difficult, because:
// (1) Browsers sometimes lie about the true size of elements, e.g. when text
// overflows the bounding box of an element, browsers report the size of the
// box even though the true area that can be interacted with is larger; and
// (2) Elements with children styled as position:absolute will often not have
// a bounding box that surrounds all of their children, but it is useful for
// the user to be able to interact with this parent element as if it does.
if (opt_coords) {
return goog.math.Vec2.fromCoordinate(opt_coords);
} else {
var size = bot.action.getInteractableSize(element);
return new goog.math.Vec2(size.width / 2, size.height / 2);
}
};
/**
* Returns the interactable size of an element.
*
* @param {!Element} elem Element.
* @return {!goog.math.Size} size Size of the element.
*/
bot.action.getInteractableSize = function(elem) {
var size = goog.style.getSize(elem);
return ((size.width > 0 && size.height > 0) || !elem.offsetParent) ? size :
bot.action.getInteractableSize(elem.offsetParent);
};
/**
* A Device that is intended to allows access to protected members of the
* Device superclass. A singleton.
*
* @constructor
* @extends {bot.Device}
* @private
*/
bot.action.LegacyDevice_ = function() {
goog.base(this);
};
goog.inherits(bot.action.LegacyDevice_, bot.Device);
goog.addSingletonGetter(bot.action.LegacyDevice_);
/**
* Focuses on the given element. See {@link bot.device.focusOnElement}.
* @param {!Element} element The element to focus on.
* @return {boolean} True if element.focus() was called on the element.
*/
bot.action.LegacyDevice_.focusOnElement = function(element) {
var instance = bot.action.LegacyDevice_.getInstance();
instance.setElement(element);
return instance.focusOnElement();
};
/**
* Submit the form for the element. See {@link bot.device.submit}.
* @param {!Element} element The element to submit a form on.
* @param {!Element} form The form to submit.
*/
bot.action.LegacyDevice_.submitForm = function(element, form) {
var instance = bot.action.LegacyDevice_.getInstance();
instance.setElement(element);
instance.submitForm(form);
};
/**
* Find FORM element that is an ancestor of the passed in element. See
* {@link bot.device.findAncestorForm}.
* @param {!Element} element The element to find an ancestor form.
* @return {Element} form The ancestor form, or null if none.
*/
bot.action.LegacyDevice_.findAncestorForm = function(element) {
return bot.Device.findAncestorForm(element);
};
/**
* Scrolls the given {@code element} in to the current viewport. Aims to do the
* minimum scrolling necessary, but prefers too much scrolling to too little.
*
* If an optional coordinate or rectangle region is provided, scrolls that
* region relative to the element into view. A coordinate is treated as a 1x1
* region whose top-left corner is positioned at that coordinate.
*
* @param {!Element} element The element to scroll in to view.
* @param {!(goog.math.Coordinate|goog.math.Rect)=} opt_region
* Region relative to the top-left corner of the element.
* @return {boolean} Whether the element is in view after scrolling.
*/
bot.action.scrollIntoView = function(element, opt_region) {
// If the element is already in view, return true; if hidden, return false.
var overflow = bot.dom.getOverflowState(element, opt_region);
if (overflow != bot.dom.OverflowState.SCROLL) {
return overflow == bot.dom.OverflowState.NONE;
}
// Some elements may not have a scrollIntoView function - for example,
// elements under an SVG element. Call those only if they exist.
if (element.scrollIntoView) {
element.scrollIntoView();
if (bot.dom.OverflowState.NONE ==
bot.dom.getOverflowState(element, opt_region)) {
return true;
}
}
// There may have not been a scrollIntoView function, or the specified
// coordinate may not be in view, so scroll "manually".
var region = bot.dom.getClientRegion(element, opt_region);
for (var container = bot.dom.getParentElement(element);
container;
container = bot.dom.getParentElement(container)) {
scrollClientRegionIntoContainerView(container);
}
return bot.dom.OverflowState.NONE ==
bot.dom.getOverflowState(element, opt_region);
function scrollClientRegionIntoContainerView(container) {
// Based largely from goog.style.scrollIntoContainerView.
var containerRect = bot.dom.getClientRect(container);
var containerBorder = goog.style.getBorderBox(container);
// Relative position of the region to the container's content box.
var relX = region.left - containerRect.left - containerBorder.left;
var relY = region.top - containerRect.top - containerBorder.top;
// How much the region can move in the container. Use the container's
// clientWidth/Height, not containerRect, to account for the scrollbar.
var spaceX = container.clientWidth + region.left - region.right;
var spaceY = container.clientHeight + region.top - region.bottom;
// Scroll the element into view of the container.
container.scrollLeft += Math.min(relX, Math.max(relX - spaceX, 0));
container.scrollTop += Math.min(relY, Math.max(relY - spaceY, 0));
}
};