This repository has been 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
/
XCElementSnapshot+FBHelpers.m
175 lines (151 loc) · 6.33 KB
/
XCElementSnapshot+FBHelpers.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
/**
* 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 "XCElementSnapshot+FBHelpers.h"
#import <KissXML/DDXML.h>
#import "FBFindElementCommands.h"
#import "FBRunLoopSpinner.h"
#import "FBLogger.h"
#import "FBXPathCreator.h"
#import "XCAXClient_iOS.h"
#import "XCTestDriver.h"
#import "XCTestPrivateSymbols.h"
#import "XCUIElement.h"
#import "XCUIElement+FBWebDriverAttributes.h"
static NSString *const kXMLIndexPathKey = @"private_indexPath";
inline static BOOL valuesAreEqual(id value1, id value2);
inline static BOOL isSnapshotTypeAmongstGivenTypes(XCElementSnapshot* snapshot, NSArray<NSNumber *> *types);
@implementation XCElementSnapshot (FBHelpers)
- (NSArray<XCElementSnapshot *> *)fb_descendantsMatchingType:(XCUIElementType)type
{
NSString *xpathQuery = [FBXPathCreator xpathWithSubelementsOfType:type];
return [self fb_descendantsMatchingXPathQuery:xpathQuery];
}
- (NSArray<XCElementSnapshot *> *)fb_descendantsMatchingXPathQuery:(NSString *)xpathQuery
{
NSMutableDictionary *elementStore = [NSMutableDictionary dictionary];
DDXMLElement *xmlElement = [self XMLElementFromElement:self indexPath:@"top" elementStore:elementStore];
NSError *error;
NSArray *xpathNodes = [xmlElement nodesForXPath:xpathQuery error:&error];
if (![xpathNodes count]) {
return nil;
}
NSMutableArray *matchingSnapshots = [NSMutableArray array];
for (DDXMLElement *childXMLElement in xpathNodes) {
XCElementSnapshot *element = [elementStore objectForKey:[[childXMLElement attributeForName:kXMLIndexPathKey] stringValue]];
if (element) {
[matchingSnapshots addObject:element];
}
}
return matchingSnapshots;
}
- (DDXMLElement *)XMLElementFromElement:(XCElementSnapshot *)snapshot indexPath:(NSString *)indexPath elementStore:(NSMutableDictionary *)elementStore
{
DDXMLElement *xmlElement = [[DDXMLElement alloc] initWithName:snapshot.wdType];
[xmlElement addAttribute:[DDXMLNode attributeWithName:@"type" stringValue:snapshot.wdType]];
if (snapshot.wdValue) {
id value = snapshot.wdValue;
NSString *stringValue;
if ([value isKindOfClass:[NSValue class]]) {
stringValue = [value stringValue];
} else if ([value isKindOfClass:[NSString class]]) {
stringValue = value;
} else {
stringValue = [value description];
}
[xmlElement addAttribute:[DDXMLNode attributeWithName:@"value" stringValue:stringValue]];
}
if (snapshot.wdName) {
[xmlElement addAttribute:[DDXMLNode attributeWithName:@"name" stringValue:snapshot.wdName]];
}
if (snapshot.wdLabel) {
[xmlElement addAttribute:[DDXMLNode attributeWithName:@"label" stringValue:snapshot.wdLabel]];
}
[xmlElement addAttribute:[DDXMLNode attributeWithName:kXMLIndexPathKey stringValue:indexPath]];
NSArray *children = snapshot.children;
for (NSUInteger i = 0; i < [children count]; i++) {
XCElementSnapshot *childSnapshot = children[i];
NSString *newIndexPath = [indexPath stringByAppendingFormat:@",%lu", (unsigned long)i];
elementStore[newIndexPath] = childSnapshot;
[xmlElement addChild:[self XMLElementFromElement:childSnapshot indexPath:newIndexPath elementStore:elementStore]];
}
return xmlElement;
}
- (XCElementSnapshot *)fb_parentMatchingType:(XCUIElementType)type
{
NSArray *acceptedParents = @[@(type)];
return [self fb_parentMatchingOneOfTypes:acceptedParents];
}
- (XCElementSnapshot *)fb_parentMatchingOneOfTypes:(NSArray<NSNumber *> *)types
{
return [self fb_parentMatchingOneOfTypes:types filter:^(XCElementSnapshot *snapshot) {
return YES;
}];
}
- (XCElementSnapshot *)fb_parentMatchingOneOfTypes:(NSArray<NSNumber *> *)types filter:(BOOL(^)(XCElementSnapshot *snapshot))filter
{
XCElementSnapshot *snapshot = self.parent;
while (snapshot && !(isSnapshotTypeAmongstGivenTypes(snapshot, types) && filter(snapshot))) {
snapshot = snapshot.parent;
}
return snapshot;
}
- (id)fb_attributeValue:(NSNumber *)attribute
{
NSDictionary *attributesResult = [[XCAXClient_iOS sharedClient] attributesForElementSnapshot:self attributeList:@[attribute]];
return (id __nonnull)attributesResult[attribute];
}
- (BOOL)fb_framelessFuzzyMatchesElement:(XCElementSnapshot *)snapshot
{
return self.elementType == snapshot.elementType &&
valuesAreEqual(self.identifier, snapshot.identifier) &&
valuesAreEqual(self.title, snapshot.title) &&
valuesAreEqual(self.label, snapshot.label) &&
valuesAreEqual(self.value, snapshot.value) &&
valuesAreEqual(self.placeholderValue, snapshot.placeholderValue);
}
- (NSArray<XCElementSnapshot *> *)fb_descendantsCellSnapshots
{
NSArray<XCElementSnapshot *> *cellSnapshots = [self fb_descendantsMatchingType:XCUIElementTypeCell];
if (cellSnapshots.count == 0) {
// For the home screen, cells are actually of type XCUIElementTypeIcon
cellSnapshots = [self fb_descendantsMatchingType:XCUIElementTypeIcon];
}
if (cellSnapshots.count == 0) {
// In some cases XCTest will not report Cell Views. In that case grab all descendants and try to figure out scroll directon from them.
cellSnapshots = self._allDescendants;
}
return cellSnapshots;
}
- (XCElementSnapshot *)fb_parentCellSnapshot
{
XCElementSnapshot *targetCellSnapshot = self;
// XCUIElementTypeIcon is the cell type for homescreen icons
NSArray<NSNumber *> *acceptableElementTypes = @[
@(XCUIElementTypeCell),
@(XCUIElementTypeIcon),
];
if (self.elementType != XCUIElementTypeCell && self.elementType != XCUIElementTypeIcon) {
targetCellSnapshot = [self fb_parentMatchingOneOfTypes:acceptableElementTypes];
}
return targetCellSnapshot;
}
@end
inline static BOOL valuesAreEqual(id value1, id value2)
{
return value1 == value2 || [value1 isEqual:value2];
}
inline static BOOL isSnapshotTypeAmongstGivenTypes(XCElementSnapshot* snapshot, NSArray<NSNumber *> *types)
{
for (NSUInteger i = 0; i < types.count; i++) {
if([@(snapshot.elementType) isEqual: types[i]] || [types[i] isEqual: @(XCUIElementTypeAny)]){
return YES;
}
}
return NO;
}