forked from alexnauda/monkeytalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMTComponentTree.m
157 lines (127 loc) · 5.78 KB
/
MTComponentTree.m
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
/* MonkeyTalk - a cross-platform functional testing tool
Copyright (C) 2012 Gorilla Logic, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#import "MTComponentTree.h"
#import "MTOrdinalView.h"
#import "MonkeyTalk.h"
#import "UIView+MTReady.h"
#import "MTConvertType.h"
#import "MTCommandEvent.h"
#import "MTGetVariableCommand.h"
#import "MTWebViewController.h"
#import "MTWebJs.h"
#import "UIView+MTFinder.h"
@implementation MTComponentTree
+ (NSArray *) componentTree:(BOOL)skipWebView {
NSMutableArray *tree = [NSMutableArray array];
NSArray *all = [MTComponentTree componentTree:skipWebView withTree:tree];
return all;
}
+ (NSArray *) componentTree:(BOOL)skipWebView withTree:(NSMutableArray*)tree {
NSArray* roots=[MTComponentTree viewRoots:skipWebView];
NSMutableDictionary *componentsDictionary = [[NSMutableDictionary alloc] init];
for (UIView *root in roots) {
NSDictionary *componentDictionary = [root componentTreeDictionary:componentsDictionary];
[tree addObject:componentDictionary];
}
return tree;
}
+ (NSArray *) viewRoots:(BOOL)skipWebView {
NSArray *found = [UIView orderedViews];
//[MTOrdinalView buildFoundComponentsStartingFromView:nil havingClass:@"MTComponentTree" isOrdinalMid:NO skipWebView:skipWebView];
//NSArray *found = [[NSArray alloc] initWithArray:[MonkeyTalk sharedMonkey].foundComponents];
NSMutableArray *roots = [[NSMutableArray alloc] initWithCapacity:5];
for (NSValue *value in found) {
UIView *view = [value nonretainedObjectValue];
if (![view superview]) {
[roots addObject:view];
}
}
//[found release];
return roots;
}
+ (NSDictionary *) componentTreeForView:(UIView*)view skipWebView:(BOOL)skipWebView {
NSString *component = [MTConvertType convertedComponentFromString:[NSString stringWithFormat:@"%@",view.class] isRecording:YES];
NSString *className = [NSString stringWithFormat:@"%@",[view class]];
NSString *visible = @"true";
if (view.hidden) {
visible = @"false";
}
NSString* value = [MTComponentTree valueForView:view asComponentType:component];
NSString* mid = view.monkeyID;
if (!mid) {
mid = @"";
}
NSArray* identifiers = [view rawMonkeyIDCandidates];
NSNumber* ordinal = [MTComponentTree ordinalForView:view];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:component forKey:@"ComponentType"];
[dict setObject:mid forKey:@"monkeyId"];
[dict setObject:className forKey:@"className"];
[dict setObject:visible forKey:@"visible"];
[dict setObject:identifiers forKey:@"identifiers"];
[dict setObject:ordinal forKey:@"ordinal"];
[dict setObject:value forKey:@"value"];
NSMutableArray *childArray = [[NSMutableArray alloc] init];
[dict setObject:childArray forKey:@"children"];
if ([view isKindOfClass:[UIWebView class]] && !skipWebView) {
NSDictionary *webComponentTree = [MTComponentTree componentTreeForWebView:(UIWebView*)view];
if (webComponentTree) {
[childArray addObject:webComponentTree];
}
} else {
for (int i=0; i<[[view subviews] count]; i++) {
UIView* subview=[[view subviews] objectAtIndex:i];
[childArray addObject:[MTComponentTree componentTreeForView:subview skipWebView:skipWebView]];
}
}
return dict;
}
+ (NSDictionary *) componentTreeForWebView: (UIWebView*)view {
// assure we have MonkeyTalk support
[view stringByEvaluatingJavaScriptFromString:MTWebJsString];
// get the JSON array of components
NSString* webComponentTreeJson = [(UIWebView*)view stringByEvaluatingJavaScriptFromString:@"MonkeyTalk.getComponentTreeJson();"];
//NSLog(@"webComponentTreeJson:%@",webComponentTreeJson);
if (webComponentTreeJson.length > 0) {
NSError *error=nil;
// convert to dictionary format for eventual re-serialization to JSON
NSDictionary *webComponentTree = [NSJSONSerialization JSONObjectWithData:[webComponentTreeJson dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
if (!error) {
return webComponentTree;
} else {
NSLog(@"ERROR parsing web component tree JSON: %@", error.description);
}
}
return nil;
}
+ (NSNumber*) ordinalForView: (UIView*)view {
NSInteger ordinal = [view mtOrdinal];
if (!ordinal || ordinal<0) {
ordinal=1;
}
return [NSNumber numberWithInteger:ordinal];
}
+ (NSString*) valueForView:(UIView*)view asComponentType:(NSString*)componentType {
MTCommandEvent *event = [[MTCommandEvent alloc] init:MTCommandGet
className:componentType
monkeyID:view.monkeyID
args:[NSArray arrayWithObject:@"value"]];
NSString* value = [MTGetVariableCommand execute:event];
if (!value
|| ![value respondsToSelector:@selector(rangeOfString:)]
|| ([value rangeOfString:@"is not a valid keypath"].location != NSNotFound)) {
value = @"";
}
return value;
}
@end