forked from vkolgi/tuneup_js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uiautomation-ext.js
256 lines (232 loc) · 7.88 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
#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)
{
timeoutInSeconds = timeoutInSeconds == null ? 5 : timeoutInSeconds;
var element = this;
var delay = 0.25;
retry(function() {
if(!element.isVisible()) {
throw("Element (" + element + ") didn't become visible within " + timeoutInSeconds + " seconds.");
}
}, timeoutInSeconds/delay, delay);
},
/*
Wait until element becomes invisible
*/
waitUntilInvisible: function (timeoutInSeconds)
{
timeoutInSeconds = timeoutInSeconds == null ? 5 : timeoutInSeconds;
var element = this;
var delay = 0.25;
retry(function() {
if(element.isVisible()) {
throw("Element (" + element + ") didn't become invisible within " + timeoutInSeconds + " seconds.");
}
}, 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() {
return this.model().match(/^iPad/) !== null;
},
/**
* A convenience method for detecting that you're running on an
* iPhone or iPod touch
*/
isDeviceiPhone: function() {
return this.model().match(/^iPhone/) !== null;
}
});
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) - 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
});