forked from vkolgi/tuneup_js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uiautomation-ext.js
369 lines (325 loc) · 11.6 KB
/
uiautomation-ext.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
#import "assertions.js"
#import "lang-ext.js"
extend(UIATableView.prototype, {
/**
* A shortcut for:
* this.cells().firstWithName(name)
*/
cellNamed: function(name) {
return this.cells().firstWithName(name);
},
/**
* Asserts that this table has a cell with the name (accessibility label)
* matching the given +name+ argument.
*/
assertCellNamed: function(name) {
assertNotNull(this.cellNamed(name), "No table cell found named '" + name + "'");
}
});
extend(UIAElement.prototype, {
/**
* Poll till the item becomes visible, up to a specified timeout
*/
waitUntilVisible: function (timeoutInSeconds) {
this.waitUntil(function(element) {
return element;
}, function(element) {
return element.isVisible();
}, timeoutInSeconds, "to become visible");
},
/**
* Wait until element becomes invisible
*/
waitUntilInvisible: function (timeoutInSeconds) {
this.waitUntil(function(element) {
return element;
}, function(element) {
return !element.isVisible();
}, timeoutInSeconds, "to become invisible");
},
/**
* Wait until child element with name is added
*/
waitUntilFoundByName: function (name, timeoutInSeconds) {
this.waitUntil(function(element) {
return element.elements().firstWithName(name);
}, function(element) {
return element.isValid();
}, timeoutInSeconds, "to become valid");
},
/**
* Wait until child element with name is removed
*/
waitUntilNotFoundByName: function (name, timeoutInSeconds) {
this.waitUntil(function(element) {
return element.elements().firstWithName(name);
}, function(element) {
return !element.isValid();
}, timeoutInSeconds, "to become invalid");
},
/**
* Wait until element fulfills condition
*/
waitUntil: function (filterFunction, conditionFunction, timeoutInSeconds, description) {
timeoutInSeconds = timeoutInSeconds == null ? 5 : timeoutInSeconds;
var element = this;
var delay = 0.25;
retry(function() {
var filteredElement = filterFunction(element);
if(!conditionFunction(filteredElement)) {
throw(["Element", filteredElement, "failed", description, "within", timeoutInSeconds, "seconds."].join(" "));
}
}, Math.max(1, timeoutInSeconds/delay), delay);
},
/**
* A shortcut for waiting an element to become visible and tap.
*/
vtap: function() {
this.waitUntilVisible(10);
this.tap();
},
/**
* A shortcut for touching an element and waiting for it to disappear.
*/
tapAndWaitForInvalid: function() {
this.tap();
this.waitForInvalid();
}
});
extend(UIAApplication.prototype, {
/**
* A shortcut for getting the current view controller's title from the
* navigation bar. If there is no navigation bar, this method returns null
*/
navigationTitle: function() {
navBar = this.mainWindow().navigationBar();
if (navBar) {
return navBar.name();
}
return null;
},
/**
* A shortcut for checking that the interface orientation in either
* portrait mode
*/
isPortraitOrientation: function() {
var orientation = this.interfaceOrientation();
return orientation == UIA_DEVICE_ORIENTATION_PORTRAIT ||
orientation == UIA_DEVICE_ORIENTATION_PORTRAIT_UPSIDEDOWN;
},
/**
* A shortcut for checking that the interface orientation in one of the
* landscape orientations.
*/
isLandscapeOrientation: function() {
var orientation = this.interfaceOrientation();
return orientation == UIA_DEVICE_ORIENTATION_LANDSCAPELEFT ||
orientation == UIA_DEVICE_ORIENTATION_LANDSCAPERIGHT;
}
});
extend(UIANavigationBar.prototype, {
/**
* Asserts that the left button's name matches the given +name+ argument
*/
assertLeftButtonNamed: function(name) {
assertEquals(name, this.leftButton().name());
},
/**
* Asserts that the right button's name matches the given +name+ argument
*/
assertRightButtonNamed: function(name) {
assertEquals(name, this.rightButton().name());
}
});
extend(UIATarget.prototype, {
/**
* A shortcut for checking that the interface orientation in either
* portrait mode
*/
isPortraitOrientation: function() {
var orientation = this.deviceOrientation();
return orientation == UIA_DEVICE_ORIENTATION_PORTRAIT ||
orientation == UIA_DEVICE_ORIENTATION_PORTRAIT_UPSIDEDOWN;
},
/**
* A shortcut for checking that the interface orientation in one of the
* landscape orientations.
*/
isLandscapeOrientation: function() {
var orientation = this.deviceOrientation();
return orientation == UIA_DEVICE_ORIENTATION_LANDSCAPELEFT ||
orientation == UIA_DEVICE_ORIENTATION_LANDSCAPERIGHT;
},
/**
* A convenience method for detecting that you're running on an iPad
*/
isDeviceiPad: function() {
//model is iPhone Simulator, even when running in iPad mode
return this.model().match(/^iPad/) !== null ||
this.name().match(/iPad Simulator/) !== null;
},
/**
* A convenience method for detecting that you're running on an
* iPhone or iPod touch
*/
isDeviceiPhone: function() {
return this.model().match(/^iPad/) === null &&
this.name().match(/^iPad Simulator$/) === null;
},
/**
* A shortcut for checking if target device is iPhone 5 (or iPod Touch
* 5th generation)
*/
isDeviceiPhone5: function() {
var isIphone = this.isDeviceiPhone();
var deviceScreen = this.rect();
return isIphone && deviceScreen.size.height == 568;
},
/**
* A convenience method for producing screenshots without status bar
*/
captureAppScreenWithName: function(imageName) {
var appRect = this.rect();
appRect.origin.y += 20.0;
appRect.size.height -= 20.0;
return this.captureRectWithName(appRect, imageName);
}
});
extend(UIAKeyboard.prototype,{
KEYBOARD_TYPE_UNKNOWN :-1,
KEYBOARD_TYPE_ALPHA : 0,
KEYBOARD_TYPE_ALPHA_CAPS : 1,
KEYBOARD_TYPE_NUMBER_AND_PUNCTUATION:2,
KEYBOARD_TYPE_NUMBER:3,
keyboardType : function() {
if (this.keys().length < 12){
return this.KEYBOARD_TYPE_NUMBER;
} else if (this.keys().firstWithName("a").toString() != "[object UIAElementNil]") {
return this.KEYBOARD_TYPE_ALPHA;
} else if (this.keys().firstWithName("A").toString() != "[object UIAElementNil]") {
return this.KEYBOARD_TYPE_ALPHA_CAPS;
} else if (this.keys().firstWithName("1").toString() != "[object UIAElementNil]") {
return this.KEYBOARD_TYPE_NUMBER_AND_PUNCTUATION;
} else {
return this.KEYBOARD_TYPE_UNKNOWN;
}
}
});
/*
TODO: Character keyboard is super slow.
*/
var typeString = function(pstrString, pbClear) {
pstrString += ''; // convert number to string
if (!this.hasKeyboardFocus()){
this.tap();
}
UIATarget.localTarget().delay(0.5);
if (pbClear || pstrString.length === 0) {
this.clear();
}
if (pstrString.length > 0) {
var app = UIATarget.localTarget().frontMostApp();
var keyboard = app.keyboard();
var keys = app.keyboard().keys();
var buttons = app.keyboard().buttons();
for (i = 0; i < pstrString.length; i++) {
var intKeyboardType = keyboard.keyboardType();
var bIsAllCaps = (intKeyboardType == keyboard.KEYBOARD_TYPE_ALPHA_CAPS); //Handles autocapitalizationType = UITextAutocapitalizationTypeAllCharacters
var intNewKeyboardType = intKeyboardType;
var strChar = pstrString.charAt(i);
if ((/[a-z]/.test(strChar)) && intKeyboardType == keyboard.KEYBOARD_TYPE_ALPHA_CAPS && !bIsAllCaps) {
buttons.firstWithName("shift").tap();
intKeyboardType = keyboard.KEYBOARD_TYPE_ALPHA;
} else if ((/[A-Z]/.test(strChar)) && intKeyboardType == keyboard.KEYBOARD_TYPE_ALPHA) {
buttons.firstWithName("shift").tap();
intKeyboardType = keyboard.KEYBOARD_TYPE_ALPHA_CAPS;
} else if ((/[A-z]/.test(strChar)) && intKeyboardType == keyboard.KEYBOARD_TYPE_NUMBER_AND_PUNCTUATION) {
buttons.firstWithName("more, letters").tap();
intKeyboardType = keyboard.KEYBOARD_TYPE_ALPHA;
} else if ((/[0-9.]/.test(strChar)) && intKeyboardType != keyboard.KEYBOARD_TYPE_NUMBER_AND_PUNCTUATION && intKeyboardType != keyboard.KEYBOARD_TYPE_NUMBER) {
buttons.firstWithName("more, numbers").tap();
intKeyboardType = keyboard.KEYBOARD_TYPE_NUMBER_AND_PUNCTUATION;
}
if ((/[a-z]/.test(strChar)) && intKeyboardType == keyboard.KEYBOARD_TYPE_ALPHA_CAPS) {
strChar = strChar.toUpperCase();
}
if (strChar == " ") {
keys["space"].tap();
} else if (/[0-9]/.test(strChar)) { // Need to change strChar to the index key of the number because strChar = "0" will tap "1" and strChar = "1" will tap "2"
if (strChar == "0") {
if (intKeyboardType == keyboard.KEYBOARD_TYPE_NUMBER_AND_PUNCTUATION) {
strChar = "9";
} else {
strChar = "10";
}
} else {
strChar = (parseInt(strChar, 10) - 1).toString();
}
keys[strChar].tap();
} else {
keys[strChar].tap(); // TODO: this line is super slow when there are many keys
}
UIATarget.localTarget().delay(0.5);
}
}
};
extend(UIATextField.prototype,{
typeString: typeString
});
extend(UIATextView.prototype,{
typeString: typeString
});
extend(UIAPickerWheel.prototype, {
/*
* Better implementation than UIAPickerWheel.selectValue
* Works also for texts
* Poorly works not for UIDatePickers -> because .values() which get all values of wheel does not work :(
* I think this is a bug in UIAutomation!
*/
scrollToValue: function (valueToSelect) {
var element = this;
var values = this.values();
var pickerValue = element.value();
// convert to string
valueToSelect = valueToSelect + "";
// some wheels return for .value() "17. 128 of 267" ?? don't know why
// throw away all after "." but be careful lastIndexOf is used because the value can
// also have "." in it!! e.g.: "1.2. 13 of 27"
if (pickerValue.lastIndexOf(".") != -1) {
var currentValue = pickerValue.substr(0, pickerValue.lastIndexOf("."));
} else {
var currentValue = element.value();
}
var currentValueIndex = values.indexOf(currentValue);
var valueToSelectIndex = values.indexOf(valueToSelect);
if (valueToSelectIndex == -1) {
fail("value: " + valueToSelect + " not found in Wheel!");
}
var elementsToScroll = valueToSelectIndex - currentValueIndex;
UIALogger.logDebug("number of elements to scroll: " + elementsToScroll);
if (elementsToScroll > 0) {
for (i=0; i<elementsToScroll; i++) {
element.tapWithOptions({tapOffset:{x:0.35, y:0.67}});
target.delay(0.7);
}
} else {
for (i=0; i>elementsToScroll; i--) {
element.tapWithOptions({tapOffset:{x:0.35, y:0.31}});
target.delay(0.7);
}
}
},
/*
* Wheels filled with values return for .value() "17. 128 of 267" ?? don't know why -> for comparisons this is unuseful!!
* If you want to check a value of a wheel this function is very helpful
*/
realValue: function() {
// current value of wheel
var pickerValue = this.value();
// throw away all after "." but be careful lastIndexOf is used because the value can
if (pickerValue.lastIndexOf(".") != -1) {
return pickerValue.substr(0, pickerValue.lastIndexOf("."));
}
return this.value();
}
});