This repository was archived by the owner on Oct 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathScriptRunner.m
More file actions
451 lines (384 loc) · 11.9 KB
/
ScriptRunner.m
File metadata and controls
451 lines (384 loc) · 11.9 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
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
//
// ScriptRunner.m
// SelfTesting
//
// Created by Matt Gallagher on 9/10/08.
// Copyright 2008 Matt Gallagher. All rights reserved.
//
#import "ScriptRunner.h"
#import "XMLDescription.h"
#import "XPathQuery.h"
#import "TouchSynthesis.h"
#import "NSObject+ClassName.h"
#import "Foundation/Foundation.h"
const float SCRIPT_RUNNER_INTER_COMMAND_DELAY = 0.0;
const float MAX_WAIT_ATTEMPTS = 60;
const float WAIT_ATTEMPT_DELAY = 0.25;
const float BACKBUTTON_WAIT_DELAY = 0.75;
@implementation ScriptRunner
//
// performTouchInView:
//
// Synthesize a touch begin/end in the center of the specified view. Since there
// is no API to do this, it's a dirty hack of a job.
//
- (void)performTouchInView:(UIView *)view hitTest:(BOOL)hitTest
{
UITouch *touch = [[UITouch alloc] initInView:view hitTest:hitTest];
UIEvent *event = [[UIEvent alloc] initWithTouch:touch];
NSSet *touches = [[NSMutableSet alloc] initWithObjects:&touch count:1];
[touch.view touchesBegan:touches withEvent:event];
[touch setPhase:UITouchPhaseEnded];
[touch.view touchesEnded:touches withEvent:event];
[event release];
[touches release];
[touch release];
}
//
// performLeftSwipeInView:
//
// swipe to the LEFT
//
// Synthesize a short rightward drag in the center of the specified view. Since there
// is no API to do this, it's a dirty hack of a job.
//
- (void)performLeftSwipeInView:(UIView *)view
{
UITouch *touch = [[UITouch alloc] initInView:view];
UIEvent *event = [[UIEvent alloc] initWithTouch:touch];
NSSet *touches = [[NSMutableSet alloc] initWithObjects:&touch count:1];
[touch.view touchesBegan:touches withEvent:event];
[touch setPhase:UITouchPhaseMoved];
//swipe left
[touch moveLocationInWindowLeft];
[event moveLocation];
[touch.view touchesMoved:touches withEvent:event];
[touch setPhase:UITouchPhaseEnded];
[touch.view touchesEnded:touches withEvent:event];
[event release];
[touches release];
[touch release];
}
//
// performSwipeInView:
//
// swipe to the RIGHT
//
// Synthesize a short rightward drag in the center of the specified view. Since there
// is no API to do this, it's a dirty hack of a job.
//
- (void)performSwipeInView:(UIView *)view
{
UITouch *touch = [[UITouch alloc] initInView:view];
UIEvent *event = [[UIEvent alloc] initWithTouch:touch];
NSSet *touches = [[NSMutableSet alloc] initWithObjects:&touch count:1];
[touch.view touchesBegan:touches withEvent:event];
[touch setPhase:UITouchPhaseMoved];
// swipe right
[touch moveLocationInWindow];
[event moveLocation];
[touch.view touchesMoved:touches withEvent:event];
[touch setPhase:UITouchPhaseEnded];
[touch.view touchesEnded:touches withEvent:event];
[event release];
[touches release];
[touch release];
}
//
// highlightView
//
// shows a yellow rect over the provided view
//
- (void)highlightView:(UIView *)view {
UIView *highlightView = [[UIView alloc] init];
CGRect frame = view.frame;
frame.origin.x = 0;
frame.origin.y = 0;
highlightView.frame = frame;
highlightView.backgroundColor = [UIColor yellowColor];
highlightView.alpha = 0.5;
[view addSubview:highlightView];
[UIView beginAnimations:@"highlight" context:nil];
[UIView setAnimationDuration:1.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
highlightView.alpha = 0;
[UIView commitAnimations];
[highlightView release];
}
//
// viewsForXPath:
//
// Generates an XML document from the current view tree and runs the specified
// XPath query on the document. If the resulting nodes contain "address" values
// then these values are interrogated to determine if they are UIViews. All
// UIViews found in this way are returned in the array.
//
- (NSArray *)viewsForXPath:(NSString *)xpath
{
//printf("XML \n%s\n\n", [[[[UIApplication sharedApplication] keyWindow] xmlDescription] cStringUsingEncoding:NSUTF8StringEncoding]);
NSData *resultData =
[[[UIApplication sharedApplication] xmlDescription] dataUsingEncoding: NSUTF8StringEncoding];
NSArray *queryResults = PerformXMLXPathQuery(resultData, xpath);
//printf("%s", [queryResults.description UTF8String]);
NSMutableArray *views =
[NSMutableArray arrayWithCapacity:[queryResults count]];
for (NSDictionary *result in queryResults)
{
int i;
int count = [[result objectForKey:@"nodeChildArray"] count];
for (i = 0; i < count; i++)
{
NSDictionary *childNode = [[result objectForKey:@"nodeChildArray"] objectAtIndex:i];
if ([[childNode objectForKey:@"nodeName"] isEqualToString:@"address"] )
{
UIView *view =
(UIView *)[[childNode objectForKey:@"nodeContent"] integerValue];
NSAssert([view isKindOfClass:[UIView class]],
@"XPath selected memory address did not contain a UIView");
[views addObject:view];
[self highlightView:view];
break;
}
}
}
return views;
}
#pragma mark -
#pragma mark Available test commands
//
// outputView
//
// This command outputs the current view hierarchy, starting with the
// keyWindow, to a file or stdout.
//
// Required parameter:
//
- (NSString*) outputView: (NSDictionary *) command {
printf("=== outputView\n");
return [[UIApplication sharedApplication] xmlDescription];
}
//
// simulateTouch
//
// Performs a synthesized touch down and touch up in a single view selected
// by a given XPath query.
//
// Required parameters:
// viewXPath (search for a view matching this XPath)
//
// Optional parameters:
// hitTest (use hit-testing to find the target view; default 1)
//
- (NSString*) simulateTouch: (NSDictionary *) command {
NSString *viewXPath = [command objectForKey:@"viewXPath"];
if (viewXPath == nil)
{
fprintf(stderr, "### Command 'simulateTouch' requires 'viewXPath' parameter.\n");
return @"fail";
}
NSNumber *sectionIndex = [command objectForKey:@"hitTest"];
BOOL hitTest = sectionIndex ? [sectionIndex boolValue] : YES;
printf("=== simulateTouch\n viewXPath:\n %s\n",
[viewXPath cStringUsingEncoding:NSUTF8StringEncoding]);
NSArray *views = [self viewsForXPath:viewXPath];
if([views count] != 1)
{
fprintf(
stderr,
"### 'viewXPath' for command 'simulateTouch' selected %d nodes, where exactly 1 is required.\n",
[views count]);
return @"fail";
}
UIView *view = [views objectAtIndex:0];
[self performTouchInView:view hitTest:hitTest];
return @"pass";
}
//
// simulateLeftSwipe
//
// swipe to the LEFT
//
- (NSString*) simulateLeftSwipe: (NSDictionary *) command {
NSString *viewXPath = [command objectForKey:@"viewXPath"];
if (viewXPath == nil)
{
fprintf(stderr, "### Command 'simulateSwipe' requires 'viewXPath' parameter.\n");
return @"fail";
}
printf("=== simulateSwipe\n viewXPath:\n %s\n",
[viewXPath cStringUsingEncoding:NSUTF8StringEncoding]);
NSArray *views = [self viewsForXPath:viewXPath];
if([views count] != 1)
{
fprintf(
stderr,
"### 'viewXPath' for command 'simulateSwipe' selected %d nodes, where exactly 1 is required.\n",
[views count]);
return @"fail";
}
UIView *view = [views objectAtIndex:0];
[self performLeftSwipeInView:view];
return @"pass";
}
//
// simulateSwipe
//
// swipe to the RIGHT
//
// Performs a synthesized rightward swipe in a single view selected
// by a given XPath query.
//
// Required parameters:
// viewXPath (search for a view matching this XPath)
//
- (NSString*) simulateSwipe: (NSDictionary *) command {
NSString *viewXPath = [command objectForKey:@"viewXPath"];
if (viewXPath == nil)
{
fprintf(stderr, "### Command 'simulateSwipe' requires 'viewXPath' parameter.\n");
return @"fail";
}
printf("=== simulateSwipe\n viewXPath:\n %s\n",
[viewXPath cStringUsingEncoding:NSUTF8StringEncoding]);
NSArray *views = [self viewsForXPath:viewXPath];
if([views count] != 1)
{
fprintf(
stderr,
"### 'viewXPath' for command 'simulateSwipe' selected %d nodes, where exactly 1 is required.\n",
[views count]);
return @"fail";
}
UIView *view = [views objectAtIndex:0];
[self performSwipeInView:view];
return @"pass";
}
//
// scrollToRow
//
// Scrolls a UITableView selected by an XPath query to the specified
// rowIndex (and optionally sectionIndex).
//
// Required parameters:
// viewXPath (search for a table view matching this XPath)
// rowIndex (scroll the table view to this row)
//
// Optional parameter:
// sectionIndex (scroll the table view to the rowIndex in this section)
//
- (NSString*) scrollToRow: (NSDictionary *) command {
NSString *viewXPath = [command objectForKey:@"viewXPath"];
NSAssert(viewXPath != nil, @"Command 'scrollToRow' requires 'viewXPath' parameter");
NSNumber *rowIndex = [command objectForKey:@"rowIndex"];
NSAssert(viewXPath != nil, @"Command 'scrollToRow' requires 'rowIndex' parameter");
NSNumber *sectionIndex = [command objectForKey:@"sectionIndex"];
// sectionIndex is allowed to be nil -- will select section 0
NSIndexPath *indexPath =
[NSIndexPath
indexPathForRow:[rowIndex integerValue]
inSection:[sectionIndex integerValue]];
printf("=== scrollToRow\n viewXPath:\n %s\n indexPath: (section: %d, row: %d)\n",
[viewXPath cStringUsingEncoding:NSUTF8StringEncoding],
[indexPath section],
[indexPath row]);
NSArray *views = [self viewsForXPath:viewXPath];
if([views count] != 1) {
fprintf(
stderr,
"### 'viewXPath' for command 'scrollToRow' selected %d nodes, where exactly 1 is required.\n",
[views count]);
return @"fail";
}
else if(![[views objectAtIndex:0] isKindOfClass:[UITableView class]]) {
fprintf(
stderr,
"### 'viewXPath' for command 'scrollToRow' selected a node but it wasn't a UITableView as required.\n",
[views count]);
return @"fail";
}
else {
UITableView *view = [views objectAtIndex:0];
[view
scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionNone
animated:NO];
return @"pass";
}
}
//
// setText
//
// This command sets the text of an UITextField or any component with text attribute
// found with the given XPath query.
//
// Required parameters:
// viewXPath (search for views matching this XPath)
// text (the text to be set)
//
- (NSString*) setText: (NSDictionary *) command {
NSString *viewXPath = [command objectForKey:@"viewXPath"];
if (viewXPath == nil)
{
fprintf(stderr, "### Command 'setText' requires 'viewXPath' parameter.\n");
return @"fail";
}
NSString *text = [command objectForKey:@"text"];
if (text == nil)
{
fprintf(stderr, "### Command 'setText' requires 'text' parameter.\n");
return @"fail";
}
printf("=== setText\n viewXPath:\n %s\n text: %s\n",
[viewXPath cStringUsingEncoding:NSUTF8StringEncoding],
[text cStringUsingEncoding:NSUTF8StringEncoding]);
NSArray *views = [self viewsForXPath:viewXPath];
if([views count] != 1)
{
fprintf(
stderr,
"### 'viewXPath' for command 'setText' selected %d nodes, where exactly 1 is required.\n",
[views count]);
return @"fail";
}
UIView *viewForText = (UIView *)[views objectAtIndex:0];
if([viewForText respondsToSelector:@selector(setText:)]) {
[viewForText performSelector:@selector(setText:) withObject:text];
return @"pass";
}
else {
fprintf(
stderr,
"### %s doesn't suport 'setText' method.\n",
[viewForText.className cStringUsingEncoding:NSUTF8StringEncoding],
[views count]);
return @"fail";
}
}
#pragma mark -
//
// runCommandStep
//
// Runs the specified command.
//
- (NSString*)runCommandStep:(NSData*)command
{
NSDictionary* parsed =
[NSPropertyListSerialization
propertyListFromData:command
mutabilityOption:NSPropertyListMutableContainers
format:nil
errorDescription:nil];
NSString *commandName = [[parsed objectForKey:@"command"] stringByAppendingString:@":"];
NSLog(commandName);
id appDelegate = [[UIApplication sharedApplication] delegate];
SEL selector = NSSelectorFromString(commandName);
id performer = ([self respondsToSelector:selector] ?
self :
([appDelegate respondsToSelector:selector] ?
appDelegate :
nil));
if (!performer)
return @"";
return [performer performSelector:selector withObject:parsed];
}
@end