forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtouchsequence.js
More file actions
248 lines (206 loc) · 7.46 KB
/
Copy pathtouchsequence.js
File metadata and controls
248 lines (206 loc) · 7.46 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
245
246
247
// 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.TouchSequence');
goog.require('goog.array');
goog.require('webdriver.Command');
goog.require('webdriver.CommandName');
/**
* Class for defining sequences of user touch interactions. Each sequence
* will not be executed until {@link #perform} is called.
*
* Example:
*
* new webdriver.TouchSequence(driver).
* tapAndHold({x: 0, y: 0}).
* move({x: 3, y: 4}).
* release({x: 10, y: 10}).
* perform();
*
* @param {!webdriver.WebDriver} driver The driver instance to use.
* @constructor
*/
webdriver.TouchSequence = function(driver) {
/** @private {!webdriver.WebDriver} */
this.driver_ = driver;
/** @private {!Array<{description: string, command: !webdriver.Command}>} */
this.touchActions_ = [];
};
/**
* 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.TouchSequence.prototype.schedule_ = function(description, command) {
this.touchActions_.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.TouchSequence.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.touchActions_);
var driver = this.driver_;
return driver.controlFlow().execute(function() {
goog.array.forEach(actions, function(action) {
driver.schedule(action.command, action.description);
});
}, 'TouchSequence.perform');
};
/**
* Taps an element.
*
* @param {!webdriver.WebElement} elem The element to tap.
* @return {!webdriver.TouchSequence} A self reference.
*/
webdriver.TouchSequence.prototype.tap = function(elem) {
var command = new webdriver.Command(webdriver.CommandName.TOUCH_SINGLE_TAP).
setParameter('element', elem.getRawId());
this.schedule_('tap', command);
return this;
};
/**
* Double taps an element.
*
* @param {!webdriver.WebElement} elem The element to double tap.
* @return {!webdriver.TouchSequence} A self reference.
*/
webdriver.TouchSequence.prototype.doubleTap = function(elem) {
var command = new webdriver.Command(webdriver.CommandName.TOUCH_DOUBLE_TAP).
setParameter('element', elem.getRawId());
this.schedule_('doubleTap', command);
return this;
};
/**
* Long press on an element.
*
* @param {!webdriver.WebElement} elem The element to long press.
* @return {!webdriver.TouchSequence} A self reference.
*/
webdriver.TouchSequence.prototype.longPress = function(elem) {
var command = new webdriver.Command(webdriver.CommandName.TOUCH_LONG_PRESS).
setParameter('element', elem.getRawId());
this.schedule_('longPress', command);
return this;
};
/**
* Touch down at the given location.
*
* @param {{x: number, y: number}} location The location to touch down at.
* @return {!webdriver.TouchSequence} A self reference.
*/
webdriver.TouchSequence.prototype.tapAndHold = function(location) {
var command = new webdriver.Command(webdriver.CommandName.TOUCH_DOWN).
setParameter('x', location.x).
setParameter('y', location.y);
this.schedule_('tapAndHold', command);
return this;
};
/**
* Move a held {@linkplain #tapAndHold touch} to the specified location.
*
* @param {{x: number, y: number}} location The location to move to.
* @return {!webdriver.TouchSequence} A self reference.
*/
webdriver.TouchSequence.prototype.move = function(location) {
var command = new webdriver.Command(webdriver.CommandName.TOUCH_MOVE).
setParameter('x', location.x).
setParameter('y', location.y);
this.schedule_('move', command);
return this;
};
/**
* Release a held {@linkplain #tapAndHold touch} at the specified location.
*
* @param {{x: number, y: number}} location The location to release at.
* @return {!webdriver.TouchSequence} A self reference.
*/
webdriver.TouchSequence.prototype.release = function(location) {
var command = new webdriver.Command(webdriver.CommandName.TOUCH_UP).
setParameter('x', location.x).
setParameter('y', location.y);
this.schedule_('release', command);
return this;
};
/**
* Scrolls the touch screen by the given offset.
*
* @param {{x: number, y: number}} offset The offset to scroll to.
* @return {!webdriver.TouchSequence} A self reference.
*/
webdriver.TouchSequence.prototype.scroll = function(offset) {
var command = new webdriver.Command(webdriver.CommandName.TOUCH_SCROLL).
setParameter('xoffset', offset.x).
setParameter('yoffset', offset.y);
this.schedule_('scroll', command);
return this;
};
/**
* Scrolls the touch screen, starting on `elem` and moving by the specified
* offset.
*
* @param {!webdriver.WebElement} elem The element where scroll starts.
* @param {{x: number, y: number}} offset The offset to scroll to.
* @return {!webdriver.TouchSequence} A self reference.
*/
webdriver.TouchSequence.prototype.scrollFromElement = function(elem, offset) {
var command = new webdriver.Command(webdriver.CommandName.TOUCH_SCROLL).
setParameter('element', elem.getRawId()).
setParameter('xoffset', offset.x).
setParameter('yoffset', offset.y);
this.schedule_('scrollFromElement', command);
return this;
};
/**
* Flick, starting anywhere on the screen, at speed xspeed and yspeed.
*
* @param {{xspeed: number, yspeed: number}} speed The speed to flick in each
direction, in pixels per second.
* @return {!webdriver.TouchSequence} A self reference.
*/
webdriver.TouchSequence.prototype.flick = function(speed) {
var command = new webdriver.Command(webdriver.CommandName.TOUCH_FLICK).
setParameter('xspeed', speed.xspeed).
setParameter('yspeed', speed.yspeed);
this.schedule_('flick', command);
return this;
};
/**
* Flick starting at elem and moving by x and y at specified speed.
*
* @param {!webdriver.WebElement} elem The element where flick starts.
* @param {{x: number, y: number}} offset The offset to flick to.
* @param {number} speed The speed to flick at in pixels per second.
* @return {!webdriver.TouchSequence} A self reference.
*/
webdriver.TouchSequence.prototype.flickElement = function(elem, offset, speed) {
var command = new webdriver.Command(webdriver.CommandName.TOUCH_FLICK).
setParameter('element', elem.getRawId()).
setParameter('xoffset', offset.x).
setParameter('yoffset', offset.y).
setParameter('speed', speed);
this.schedule_('flickElement', command);
return this;
};