forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse.js
483 lines (424 loc) · 17.4 KB
/
mouse.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
// Copyright 2011 WebDriver committers
// Copyright 2011 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 The file contains an abstraction of a mouse for
* simulating the mouse actions.
*/
goog.provide('bot.Mouse');
goog.provide('bot.Mouse.Button');
goog.provide('bot.Mouse.State');
goog.require('bot');
goog.require('bot.Device');
goog.require('bot.Error');
goog.require('bot.ErrorCode');
goog.require('bot.dom');
goog.require('bot.events.EventType');
goog.require('bot.userAgent');
goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.math.Coordinate');
goog.require('goog.userAgent');
/**
* A mouse that provides atomic mouse actions. This mouse currently only
* supports having one button pressed at a time.
* @param {bot.Mouse.State=} opt_state The mouse's initial state.
* @param {bot.Device.ModifiersState=} opt_modifiersState State of the keyboard.
* @param {bot.Device.EventEmitter=} opt_eventEmitter An object that should be
* used to fire events.
* @constructor
* @extends {bot.Device}
*/
bot.Mouse = function(opt_state, opt_modifiersState, opt_eventEmitter) {
goog.base(this, opt_modifiersState, opt_eventEmitter);
/** @private {?bot.Mouse.Button} */
this.buttonPressed_ = null;
/** @private {Element} */
this.elementPressed_ = null;
/** @private {!goog.math.Coordinate} */
this.clientXY_ = new goog.math.Coordinate(0, 0);
/** @private {boolean} */
this.nextClickIsDoubleClick_ = false;
/**
* Whether this Mouse has ever explicitly interacted with any element.
* @private {boolean}
*/
this.hasEverInteracted_ = false;
if (opt_state) {
this.buttonPressed_ = opt_state.buttonPressed;
try {
if (bot.dom.isElement(opt_state.elementPressed)) {
this.elementPressed_ = opt_state.elementPressed;
}
} catch (ignored) {
this.buttonPressed_ = null;
}
this.clientXY_ = opt_state.clientXY;
this.nextClickIsDoubleClick_ = opt_state.nextClickIsDoubleClick;
this.hasEverInteracted_ = opt_state.hasEverInteracted;
try {
if (bot.dom.isElement(opt_state.element)) {
this.setElement(/** @type {!Element} */ (opt_state.element));
}
} catch (ignored) {
this.buttonPressed_ = null;
}
}
};
goog.inherits(bot.Mouse, bot.Device);
/**
* @typedef {{buttonPressed: ?bot.Mouse.Button,
* elementPressed: Element,
* clientXY: !goog.math.Coordinate,
* nextClickIsDoubleClick: boolean,
* hasEverInteracted: boolean,
* element: Element}}
*/
bot.Mouse.State;
/**
* Enumeration of mouse buttons that can be pressed.
*
* @enum {number}
*/
bot.Mouse.Button = {
LEFT: 0,
MIDDLE: 1,
RIGHT: 2
};
/**
* Index to indicate no button pressed in bot.Mouse.MOUSE_BUTTON_VALUE_MAP_.
* @private {number}
* @const
*/
bot.Mouse.NO_BUTTON_VALUE_INDEX_ = 3;
/**
* Maps mouse events to an array of button argument value for each mouse button.
* The array is indexed by the bot.Mouse.Button values. It encodes this table,
* where each cell contains the (left/middle/right/none) button values.
* click/ mouseup/ mouseout/ mousemove contextmenu
* dblclick mousedown mouseover
* IE_DOC_PRE9 0 0 0 X 1 4 2 X 0 0 0 0 1 4 2 0 X X 0 X
* WEBKIT/IE9 0 1 2 X 0 1 2 X 0 1 2 0 0 1 2 0 X X 2 X
* GECKO/OPERA 0 1 2 X 0 1 2 X 0 0 0 0 0 0 0 0 X X 2 X
* @private {!Object.<bot.events.EventType, !Array.<?number>>}
* @const
*/
bot.Mouse.MOUSE_BUTTON_VALUE_MAP_ = (function() {
// EventTypes can safely be used as keys without collisions in a JS Object,
// because its toString method returns a unique string (the event type name).
var buttonValueMap = {};
if (bot.userAgent.IE_DOC_PRE9) {
buttonValueMap[bot.events.EventType.CLICK] = [0, 0, 0, null];
buttonValueMap[bot.events.EventType.CONTEXTMENU] = [null, null, 0, null];
buttonValueMap[bot.events.EventType.MOUSEUP] = [1, 4, 2, null];
buttonValueMap[bot.events.EventType.MOUSEOUT] = [0, 0, 0, 0];
buttonValueMap[bot.events.EventType.MOUSEMOVE] = [1, 4, 2, 0];
} else if (goog.userAgent.WEBKIT || bot.userAgent.IE_DOC_9) {
buttonValueMap[bot.events.EventType.CLICK] = [0, 1, 2, null];
buttonValueMap[bot.events.EventType.CONTEXTMENU] = [null, null, 2, null];
buttonValueMap[bot.events.EventType.MOUSEUP] = [0, 1, 2, null];
buttonValueMap[bot.events.EventType.MOUSEOUT] = [0, 1, 2, 0];
buttonValueMap[bot.events.EventType.MOUSEMOVE] = [0, 1, 2, 0];
} else {
buttonValueMap[bot.events.EventType.CLICK] = [0, 1, 2, null];
buttonValueMap[bot.events.EventType.CONTEXTMENU] = [null, null, 2, null];
buttonValueMap[bot.events.EventType.MOUSEUP] = [0, 1, 2, null];
buttonValueMap[bot.events.EventType.MOUSEOUT] = [0, 0, 0, 0];
buttonValueMap[bot.events.EventType.MOUSEMOVE] = [0, 0, 0, 0];
}
if (bot.userAgent.IE_DOC_10) {
buttonValueMap[bot.events.EventType.MSPOINTERDOWN] =
buttonValueMap[bot.events.EventType.MOUSEUP];
buttonValueMap[bot.events.EventType.MSPOINTERUP] =
buttonValueMap[bot.events.EventType.MOUSEUP];
buttonValueMap[bot.events.EventType.MSPOINTERMOVE] = [-1, -1, -1, -1];
buttonValueMap[bot.events.EventType.MSPOINTEROUT] =
buttonValueMap[bot.events.EventType.MSPOINTERMOVE];
buttonValueMap[bot.events.EventType.MSPOINTEROVER] =
buttonValueMap[bot.events.EventType.MSPOINTERMOVE];
}
buttonValueMap[bot.events.EventType.DBLCLICK] =
buttonValueMap[bot.events.EventType.CLICK];
buttonValueMap[bot.events.EventType.MOUSEDOWN] =
buttonValueMap[bot.events.EventType.MOUSEUP];
buttonValueMap[bot.events.EventType.MOUSEOVER] =
buttonValueMap[bot.events.EventType.MOUSEOUT];
return buttonValueMap;
})();
/**
* Maps mouse events to corresponding MSPointer event.
* @private {!Object.<bot.events.EventType, bot.events.EventType>}
*/
bot.Mouse.MOUSE_EVENT_MAP_ = (function() {
var map = {};
map[bot.events.EventType.MOUSEDOWN] = bot.events.EventType.MSPOINTERDOWN;
map[bot.events.EventType.MOUSEMOVE] = bot.events.EventType.MSPOINTERMOVE;
map[bot.events.EventType.MOUSEOUT] = bot.events.EventType.MSPOINTEROUT;
map[bot.events.EventType.MOUSEOVER] = bot.events.EventType.MSPOINTEROVER;
map[bot.events.EventType.MOUSEUP] = bot.events.EventType.MSPOINTERUP;
return map;
})();
/**
* Attempts to fire a mousedown event and then returns whether or not the
* element should receive focus as a result of the mousedown.
*
* @return {boolean} Whether to focus on the element after the mousedown.
* @private
*/
bot.Mouse.prototype.fireMousedown_ = function() {
// On some browsers, a mouse down event on an OPTION or SELECT element cause
// the SELECT to open, blocking further JS execution. This is undesirable,
// and so needs to be detected. We always focus in this case.
// TODO: This is a nasty way to avoid locking the browser
var isFirefox3 = goog.userAgent.GECKO && !bot.userAgent.isProductVersion(4);
var blocksOnMousedown = (goog.userAgent.WEBKIT || isFirefox3) &&
(bot.dom.isElement(this.getElement(), goog.dom.TagName.OPTION) ||
bot.dom.isElement(this.getElement(), goog.dom.TagName.SELECT));
if (blocksOnMousedown) {
return true;
}
// On some browsers, if the mousedown event handler makes a focus() call to
// change the active element, this preempts the focus that would happen by
// default on the mousedown, so we should not explicitly focus in this case.
var beforeActiveElement;
var mousedownCanPreemptFocus = goog.userAgent.GECKO || goog.userAgent.IE;
if (mousedownCanPreemptFocus) {
beforeActiveElement = bot.dom.getActiveElement(this.getElement());
}
var performFocus = this.fireMouseEvent_(bot.events.EventType.MOUSEDOWN);
if (performFocus && mousedownCanPreemptFocus &&
beforeActiveElement != bot.dom.getActiveElement(this.getElement())) {
return false;
}
return performFocus;
};
/**
* Press a mouse button on an element that the mouse is interacting with.
*
* @param {!bot.Mouse.Button} button Button.
*/
bot.Mouse.prototype.pressButton = function(button) {
if (!goog.isNull(this.buttonPressed_)) {
throw new bot.Error(bot.ErrorCode.UNKNOWN_ERROR,
'Cannot press more then one button or an already pressed button.');
}
this.buttonPressed_ = button;
this.elementPressed_ = this.getElement();
var performFocus = this.fireMousedown_();
if (performFocus) {
if (bot.userAgent.IE_DOC_10 &&
this.buttonPressed_ == bot.Mouse.Button.LEFT &&
bot.dom.isElement(this.elementPressed_, goog.dom.TagName.OPTION)) {
this.fireMSPointerEvent(bot.events.EventType.MSGOTPOINTERCAPTURE,
this.clientXY_, 0, bot.Device.MOUSE_MS_POINTER_ID,
MSPointerEvent.MSPOINTER_TYPE_MOUSE, true);
}
this.focusOnElement();
}
};
/**
* Releases the pressed mouse button. Throws exception if no button pressed.
*
*/
bot.Mouse.prototype.releaseButton = function() {
if (goog.isNull(this.buttonPressed_)) {
throw new bot.Error(bot.ErrorCode.UNKNOWN_ERROR,
'Cannot release a button when no button is pressed.');
}
this.maybeToggleOption();
this.fireMouseEvent_(bot.events.EventType.MOUSEUP);
// TODO: Middle button can also trigger click.
if (this.buttonPressed_ == bot.Mouse.Button.LEFT &&
this.getElement() == this.elementPressed_) {
if (!(bot.userAgent.WINDOWS_PHONE &&
bot.dom.isElement(this.elementPressed_, goog.dom.TagName.OPTION))) {
this.clickElement(this.clientXY_,
this.getButtonValue_(bot.events.EventType.CLICK));
}
this.maybeDoubleClickElement_();
if (bot.userAgent.IE_DOC_10 &&
this.buttonPressed_ == bot.Mouse.Button.LEFT &&
bot.dom.isElement(this.elementPressed_, goog.dom.TagName.OPTION)) {
this.fireMSPointerEvent(bot.events.EventType.MSLOSTPOINTERCAPTURE,
new goog.math.Coordinate(0, 0), 0, bot.Device.MOUSE_MS_POINTER_ID,
MSPointerEvent.MSPOINTER_TYPE_MOUSE, false);
}
// TODO: In Linux, this fires after mousedown event.
} else if (this.buttonPressed_ == bot.Mouse.Button.RIGHT) {
this.fireMouseEvent_(bot.events.EventType.CONTEXTMENU);
}
bot.Device.clearPointerMap();
this.buttonPressed_ = null;
this.elementPressed_ = null;
};
/**
* A helper function to fire mouse double click events.
*
* @private
*/
bot.Mouse.prototype.maybeDoubleClickElement_ = function() {
// Trigger an additional double click event if it is the second click.
if (this.nextClickIsDoubleClick_) {
this.fireMouseEvent_(bot.events.EventType.DBLCLICK);
}
this.nextClickIsDoubleClick_ = !this.nextClickIsDoubleClick_;
};
/**
* Given a coordinates (x,y) related to an element, move mouse to (x,y) of the
* element. The top-left point of the element is (0,0).
*
* @param {!Element} element The destination element.
* @param {!goog.math.Coordinate} coords Mouse position related to the target.
*/
bot.Mouse.prototype.move = function(element, coords) {
// If the element is interactable at the start of the move, it receives the
// full event sequence, even if hidden by an element mid sequence.
var toElemWasInteractable = bot.dom.isInteractable(element);
var rect = bot.dom.getClientRect(element);
this.clientXY_.x = coords.x + rect.left;
this.clientXY_.y = coords.y + rect.top;
var fromElement = this.getElement();
if (element != fromElement) {
// If the window of fromElement is closed, set fromElement to null as a flag
// to skip the mouseout event and so relatedTarget of the mouseover is null.
try {
if (goog.dom.getWindow(goog.dom.getOwnerDocument(fromElement)).closed) {
fromElement = null;
}
} catch (ignore) {
// Sometimes accessing a window that no longer exists causes an error.
fromElement = null;
}
if (fromElement) {
// For the first mouse interaction on a page, if the mouse was over the
// browser window, the browser will pass null as the relatedTarget for the
// mouseover event. For subsequent interactions, it will pass the
// last-focused element. Unfortunately, we don't have anywhere to keep the
// state of which elements have been focused across Mouse instances, so we
// treat every Mouse initially positioned over the documentElement or body
// as if it's on a new page. Accordingly, for complex actions (e.g.
// drag-and-drop), a single Mouse instance should be used for the whole
// action, to ensure the correct relatedTargets are fired for any events.
var isRoot = fromElement === bot.getDocument().documentElement ||
fromElement === bot.getDocument().body;
fromElement = (!this.hasEverInteracted_ && isRoot) ? null : fromElement;
this.fireMouseEvent_(bot.events.EventType.MOUSEOUT, element);
}
this.setElement(element);
// All browsers except IE fire the mouseover before the mousemove.
if (!goog.userAgent.IE) {
this.fireMouseEvent_(bot.events.EventType.MOUSEOVER, fromElement, null,
toElemWasInteractable);
}
}
this.fireMouseEvent_(bot.events.EventType.MOUSEMOVE, null, null,
toElemWasInteractable);
// IE fires the mouseover event after the mousemove.
if (goog.userAgent.IE && element != fromElement) {
this.fireMouseEvent_(bot.events.EventType.MOUSEOVER, fromElement, null,
toElemWasInteractable);
}
this.nextClickIsDoubleClick_ = false;
};
/**
* Scrolls the wheel of the mouse by the given number of ticks, where a positive
* number indicates a downward scroll and a negative is upward scroll.
*
* @param {number} ticks Number of ticks to scroll the mouse wheel.
*/
bot.Mouse.prototype.scroll = function(ticks) {
if (ticks == 0) {
throw new bot.Error(bot.ErrorCode.UNKNOWN_ERROR,
'Must scroll a non-zero number of ticks.');
}
// The wheelDelta value for a single up-tick of the mouse wheel is 120, and
// a single down-tick is -120. The deltas in pixels (which is only relevant
// for Firefox) appears to be -57 and 57, respectively.
var wheelDelta = ticks > 0 ? -120 : 120;
var pixelDelta = ticks > 0 ? 57 : -57;
// Browsers fire a separate event (or pair of events in Gecko) for each tick.
for (var i = 0; i < Math.abs(ticks); i++) {
this.fireMouseEvent_(bot.events.EventType.MOUSEWHEEL, null, wheelDelta);
if (goog.userAgent.GECKO) {
this.fireMouseEvent_(bot.events.EventType.MOUSEPIXELSCROLL, null,
pixelDelta);
}
}
};
/**
* A helper function to fire mouse events.
*
* @param {bot.events.EventType} type Event type.
* @param {Element=} opt_related The related element of this event.
* @param {?number=} opt_wheelDelta The wheel delta value for the event.
* @param {boolean=} opt_force Whether the event should be fired even if the
* element is not interactable.
* @return {boolean} Whether the event fired successfully or was cancelled.
* @private
*/
bot.Mouse.prototype.fireMouseEvent_ = function(type, opt_related,
opt_wheelDelta, opt_force) {
this.hasEverInteracted_ = true;
if (bot.userAgent.IE_DOC_10) {
var msPointerEvent = bot.Mouse.MOUSE_EVENT_MAP_[type];
if (msPointerEvent) {
// The pointerId for mouse events is always 1 and the mouse event is never
// fired if the MSPointer event fails.
if (!this.fireMSPointerEvent(msPointerEvent, this.clientXY_,
this.getButtonValue_(msPointerEvent), bot.Device.MOUSE_MS_POINTER_ID,
MSPointerEvent.MSPOINTER_TYPE_MOUSE, /* isPrimary */ true,
opt_related, opt_force)) {
return false;
}
}
}
return this.fireMouseEvent(type, this.clientXY_,
this.getButtonValue_(type), opt_related, opt_wheelDelta, opt_force);
};
/**
* Given an event type and a mouse button, sets the mouse button value used
* for that event on the current browser. The mouse button value is 0 for any
* event not covered by bot.Mouse.MOUSE_BUTTON_VALUE_MAP_.
*
* @param {bot.events.EventType} eventType Type of mouse event.
* @return {number} The mouse button ID value to the current browser.
* @private
*/
bot.Mouse.prototype.getButtonValue_ = function(eventType) {
if (!(eventType in bot.Mouse.MOUSE_BUTTON_VALUE_MAP_)) {
return 0;
}
var buttonIndex = goog.isNull(this.buttonPressed_) ?
bot.Mouse.NO_BUTTON_VALUE_INDEX_ : this.buttonPressed_;
var buttonValue = bot.Mouse.MOUSE_BUTTON_VALUE_MAP_[eventType][buttonIndex];
if (goog.isNull(buttonValue)) {
throw new bot.Error(bot.ErrorCode.UNKNOWN_ERROR,
'Event does not permit the specified mouse button.');
}
return buttonValue;
};
/**
* Serialize the current state of the mouse.
* @return {!bot.Mouse.State} The current mouse state.
*/
bot.Mouse.prototype.getState = function() {
var state = {};
state.buttonPressed = this.buttonPressed_;
state.elementPressed = this.elementPressed_;
state.clientXY = this.clientXY_;
state.nextClickIsDoubleClick = this.nextClickIsDoubleClick_;
state.hasEverInteracted = this.hasEverInteracted_;
state.element = this.getElement();
return state;
};