forked from facebookarchive/WebDriverAgent
-
Notifications
You must be signed in to change notification settings - Fork 401
/
Copy pathXCUIElement+FBWebDriverAttributes.m
234 lines (205 loc) · 6.88 KB
/
XCUIElement+FBWebDriverAttributes.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
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
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "XCUIElement+FBWebDriverAttributes.h"
#import "FBElementTypeTransformer.h"
#import "FBLogger.h"
#import "FBMacros.h"
#import "FBXCElementSnapshotWrapper.h"
#import "XCUIElement+FBAccessibility.h"
#import "XCUIElement+FBIsVisible.h"
#import "XCUIElement+FBUID.h"
#import "XCUIElement.h"
#import "XCUIElement+FBUtilities.h"
#import "FBElementUtils.h"
#import "XCTestPrivateSymbols.h"
#import "XCUIHitPointResult.h"
#define BROKEN_RECT CGRectMake(-1, -1, 0, 0)
@implementation XCUIElement (WebDriverAttributesForwarding)
- (id<FBXCElementSnapshot>)fb_snapshotForAttributeName:(NSString *)name
{
BOOL inDepth = [name isEqualToString:FBStringify(XCUIElement, isWDAccessible)]
|| [name isEqualToString:FBStringify(XCUIElement, isWDAccessibilityContainer)]
|| [name isEqualToString:FBStringify(XCUIElement, wdIndex)];
return [self fb_takeSnapshot:inDepth];
}
- (id)fb_valueForWDAttributeName:(NSString *)name
{
NSString *wdAttributeName = [FBElementUtils wdAttributeNameForAttributeName:name];
id<FBXCElementSnapshot> snapshot = [self fb_snapshotForAttributeName:wdAttributeName];
return [[FBXCElementSnapshotWrapper ensureWrapped:snapshot] fb_valueForWDAttributeName:name];
}
- (id)forwardingTargetForSelector:(SEL)aSelector
{
static dispatch_once_t onceToken;
static NSSet<NSString *> *fbElementAttributeNames;
dispatch_once(&onceToken, ^{
fbElementAttributeNames = [FBElementUtils selectorNamesWithProtocol:@protocol(FBElement)];
});
NSString* attributeName = NSStringFromSelector(aSelector);
return [fbElementAttributeNames containsObject:attributeName]
? [FBXCElementSnapshotWrapper ensureWrapped:[self fb_snapshotForAttributeName:attributeName]]
: nil;
}
@end
@implementation FBXCElementSnapshotWrapper (WebDriverAttributes)
- (id)fb_valueForWDAttributeName:(NSString *)name
{
return [self valueForKey:[FBElementUtils wdAttributeNameForAttributeName:name]];
}
- (NSString *)wdValue
{
id value = self.value;
XCUIElementType elementType = self.elementType;
if (elementType == XCUIElementTypeStaticText) {
NSString *label = self.label;
value = FBFirstNonEmptyValue(value, label);
} else if (elementType == XCUIElementTypeButton) {
NSNumber *isSelected = self.isSelected ? @YES : nil;
value = FBFirstNonEmptyValue(value, isSelected);
} else if (elementType == XCUIElementTypeSwitch) {
value = @([value boolValue]);
} else if (elementType == XCUIElementTypeTextView ||
elementType == XCUIElementTypeTextField ||
elementType == XCUIElementTypeSearchField ||
elementType == XCUIElementTypeSecureTextField) {
NSString *placeholderValue = self.placeholderValue;
value = FBFirstNonEmptyValue(value, placeholderValue);
}
value = FBTransferEmptyStringToNil(value);
if (value) {
value = [NSString stringWithFormat:@"%@", value];
}
return value;
}
+ (NSString *)wdNameWithSnapshot:(id<FBXCElementSnapshot>)snapshot
{
NSString *identifier = snapshot.identifier;
if (nil != identifier && identifier.length != 0) {
return identifier;
}
NSString *label = snapshot.label;
return FBTransferEmptyStringToNil(label);
}
- (NSString *)wdName
{
return [self.class wdNameWithSnapshot:self.snapshot];
}
- (NSString *)wdLabel
{
NSString *label = self.label;
XCUIElementType elementType = self.elementType;
if (elementType == XCUIElementTypeTextField || elementType == XCUIElementTypeSecureTextField ) {
return label;
}
return FBTransferEmptyStringToNil(label);
}
- (NSString *)wdType
{
return [FBElementTypeTransformer stringWithElementType:self.elementType];
}
- (NSString *)wdUID
{
return self.fb_uid;
}
- (CGRect)wdFrame
{
CGRect frame = self.frame;
// It is mandatory to replace all Infinity values with numbers to avoid JSON parsing
// exceptions like https://github.com/facebook/WebDriverAgent/issues/639#issuecomment-314421206
// caused by broken element dimensions returned by XCTest
return (isinf(frame.size.width) || isinf(frame.size.height)
|| isinf(frame.origin.x) || isinf(frame.origin.y))
? CGRectIntegral(BROKEN_RECT)
: CGRectIntegral(frame);
}
- (BOOL)isWDVisible
{
return self.fb_isVisible;
}
- (BOOL)isWDFocused
{
return self.hasFocus;
}
- (BOOL)isWDAccessible
{
XCUIElementType elementType = self.elementType;
// Special cases:
// Table view cell: we consider it accessible if it's container is accessible
// Text fields: actual accessible element isn't text field itself, but nested element
if (elementType == XCUIElementTypeCell) {
if (!self.fb_isAccessibilityElement) {
id<FBXCElementSnapshot> containerView = [[self children] firstObject];
if (![FBXCElementSnapshotWrapper ensureWrapped:containerView].fb_isAccessibilityElement) {
return NO;
}
}
} else if (elementType != XCUIElementTypeTextField && elementType != XCUIElementTypeSecureTextField) {
if (!self.fb_isAccessibilityElement) {
return NO;
}
}
id<FBXCElementSnapshot> parentSnapshot = self.parent;
while (parentSnapshot) {
// In the scenario when table provides Search results controller, table could be marked as accessible element, even though it isn't
// As it is highly unlikely that table view should ever be an accessibility element itself,
// for now we work around that by skipping Table View in container checks
if (parentSnapshot.elementType != XCUIElementTypeTable
&& [FBXCElementSnapshotWrapper ensureWrapped:parentSnapshot].fb_isAccessibilityElement) {
return NO;
}
parentSnapshot = parentSnapshot.parent;
}
return YES;
}
- (BOOL)isWDAccessibilityContainer
{
NSArray<id<FBXCElementSnapshot>> *children = self.children;
for (id<FBXCElementSnapshot> child in children) {
FBXCElementSnapshotWrapper *wrappedChild = [FBXCElementSnapshotWrapper ensureWrapped:child];
if (wrappedChild.isWDAccessibilityContainer || wrappedChild.fb_isAccessibilityElement) {
return YES;
}
}
return NO;
}
- (BOOL)isWDEnabled
{
return self.isEnabled;
}
- (BOOL)isWDSelected
{
return self.isSelected;
}
- (NSUInteger)wdIndex
{
if (nil != self.parent) {
for (NSUInteger index = 0; index < self.parent.children.count; ++index) {
if ([self.parent.children objectAtIndex:index] == self.snapshot) {
return index;
}
}
}
return 0;
}
- (BOOL)isWDHittable
{
XCUIHitPointResult *result = [self hitPoint:nil];
return nil == result ? NO : result.hittable;
}
- (NSDictionary *)wdRect
{
CGRect frame = self.wdFrame;
return @{
@"x": @(CGRectGetMinX(frame)),
@"y": @(CGRectGetMinY(frame)),
@"width": @(CGRectGetWidth(frame)),
@"height": @(CGRectGetHeight(frame)),
};
}
@end