This repository was archived by the owner on Dec 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathXCUIElement+FBWebDriverAttributes.m
164 lines (142 loc) · 4.48 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
/**
* 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 <objc/runtime.h>
#import "FBElementTypeTransformer.h"
#import "FBMacros.h"
#import "XCUIElement+FBAccessibility.h"
#import "XCUIElement+FBIsVisible.h"
#import "XCUIElement+FBUID.h"
#import "XCUIElement.h"
#import "XCUIElement+FBUtilities.h"
#import "FBElementUtils.h"
@implementation XCUIElement (WebDriverAttributesForwarding)
- (id)forwardingTargetForSelector:(SEL)aSelector
{
struct objc_method_description descr = protocol_getMethodDescription(@protocol(FBElement), aSelector, YES, YES);
BOOL isWebDriverAttributesSelector = descr.name != nil;
if(!isWebDriverAttributesSelector) {
return nil;
}
if (!self.exists) {
return [XCElementSnapshot new];
}
// If lastSnapshot is still missing aplication is probably not active. Returning empty element instead of crashing.
// This will work well, if element search is requested (will not match anything) and reqesting properties values (will return nils).
return self.fb_lastSnapshot ?: [XCElementSnapshot new];
}
@end
@implementation XCElementSnapshot (WebDriverAttributes)
- (id)fb_valueForWDAttributeName:(NSString *)name
{
return [self valueForKey:[FBElementUtils wdAttributeNameForAttributeName:name]];
}
- (NSString *)wdValue
{
id value = self.value;
if (self.elementType == XCUIElementTypeStaticText) {
value = FBFirstNonEmptyValue(self.value, self.label);
}
if (self.elementType == XCUIElementTypeButton) {
value = FBFirstNonEmptyValue(self.value, (self.isSelected ? @YES : nil));
}
if (self.elementType == XCUIElementTypeSwitch) {
value = @([self.value boolValue]);
}
if (self.elementType == XCUIElementTypeTextView ||
self.elementType == XCUIElementTypeTextField ||
self.elementType == XCUIElementTypeSecureTextField) {
value = FBFirstNonEmptyValue(self.value, self.placeholderValue);
}
value = FBTransferEmptyStringToNil(value);
if (value) {
value = [NSString stringWithFormat:@"%@", value];
}
return value;
}
- (NSString *)wdName
{
return FBTransferEmptyStringToNil(FBFirstNonEmptyValue(self.identifier, self.label));
}
- (NSString *)wdLabel
{
if (self.elementType == XCUIElementTypeTextField) {
return self.label;
}
return FBTransferEmptyStringToNil(self.label);
}
- (NSString *)wdType
{
return [FBElementTypeTransformer stringWithElementType:self.elementType];
}
- (NSUInteger)wdUID
{
return self.fb_uid;
}
- (CGRect)wdFrame
{
return CGRectIntegral(self.frame);
}
- (BOOL)isWDVisible
{
return self.fb_isVisible;
}
- (BOOL)isWDAccessible
{
// 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 (self.elementType == XCUIElementTypeCell) {
if (!self.fb_isAccessibilityElement) {
XCElementSnapshot *containerView = [[self children] firstObject];
if (!containerView.fb_isAccessibilityElement) {
return NO;
}
}
} else if (self.elementType != XCUIElementTypeTextField && self.elementType != XCUIElementTypeSecureTextField) {
if (!self.fb_isAccessibilityElement) {
return NO;
}
}
XCElementSnapshot *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.fb_isAccessibilityElement && parentSnapshot.elementType != XCUIElementTypeTable) {
return NO;
}
parentSnapshot = parentSnapshot.parent;
}
return YES;
}
- (BOOL)isWDAccessibilityContainer
{
for (XCElementSnapshot *child in self.children) {
if (child.isWDAccessibilityContainer || child.fb_isAccessibilityElement) {
return YES;
}
}
return NO;
}
- (BOOL)isWDEnabled
{
return self.isEnabled;
}
- (NSDictionary *)wdRect
{
CGRect frame = self.wdFrame;
return @{
@"x": @(CGRectGetMinX(frame)),
@"y": @(CGRectGetMinY(frame)),
@"width": @(CGRectGetWidth(frame)),
@"height": @(CGRectGetHeight(frame)),
};
}
@end