Skip to content

Commit f62afc3

Browse files
chore: Exclude element visibility and accessibility info from the accessibility audit details (#968)
1 parent a1b5af6 commit f62afc3

File tree

1 file changed

+50
-14
lines changed

1 file changed

+50
-14
lines changed

WebDriverAgentLib/Categories/XCUIApplication+FBHelpers.m

+50-14
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@
4040

4141
static NSString* const FBUnknownBundleId = @"unknown";
4242

43+
static NSString* const FBExclusionAttributeFrame = @"frame";
44+
static NSString* const FBExclusionAttributeEnabled = @"enabled";
45+
static NSString* const FBExclusionAttributeVisible = @"visible";
46+
static NSString* const FBExclusionAttributeAccessible = @"accessible";
47+
static NSString* const FBExclusionAttributeFocused = @"focused";
48+
49+
4350
_Nullable id extractIssueProperty(id issue, NSString *propertyName) {
4451
SEL selector = NSSelectorFromString(propertyName);
4552
NSMethodSignature *methodSignature = [issue methodSignatureForSelector:selector];
@@ -88,6 +95,17 @@ _Nullable id extractIssueProperty(id issue, NSString *propertyName) {
8895
return result;
8996
}
9097

98+
NSDictionary<NSString *, NSString *> *customExclusionAttributesMap(void) {
99+
static dispatch_once_t onceToken;
100+
static NSDictionary *result;
101+
dispatch_once(&onceToken, ^{
102+
result = @{
103+
FBExclusionAttributeVisible: FB_XCAXAIsVisibleAttributeName,
104+
FBExclusionAttributeAccessible: FB_XCAXAIsElementAttributeName,
105+
};
106+
});
107+
return result;
108+
}
91109

92110
@implementation XCUIApplication (FBHelpers)
93111

@@ -156,12 +174,26 @@ - (NSDictionary *)fb_tree
156174
return [self fb_tree:nil];
157175
}
158176

159-
- (NSDictionary *)fb_tree:(nullable NSSet<NSString *> *) excludedAttributes
177+
- (NSDictionary *)fb_tree:(nullable NSSet<NSString *> *)excludedAttributes
160178
{
161-
id<FBXCElementSnapshot> snapshot = self.fb_isResolvedFromCache.boolValue
162-
? self.lastSnapshot
163-
: [self fb_snapshotWithAllAttributesAndMaxDepth:nil];
164-
return [self.class dictionaryForElement:snapshot recursive:YES excludedAttributes:excludedAttributes];
179+
// This set includes XCTest-specific internal attribute names,
180+
// while the `excludedAttributes` arg contains human-readable ones
181+
NSMutableSet* includedAttributeNames = [NSMutableSet setWithArray:FBCustomAttributeNames()];
182+
[includedAttributeNames addObjectsFromArray:FBStandardAttributeNames()];
183+
if (nil != excludedAttributes) {
184+
for (NSString *attr in excludedAttributes) {
185+
NSString *mappedName = [customExclusionAttributesMap() objectForKey:attr];
186+
if (nil != mappedName) {
187+
[includedAttributeNames removeObject:attr];
188+
}
189+
}
190+
}
191+
id<FBXCElementSnapshot> snapshot = nil == excludedAttributes
192+
? [self fb_snapshotWithAllAttributesAndMaxDepth:nil]
193+
: [self fb_snapshotWithAttributes:[includedAttributeNames allObjects] maxDepth:nil];
194+
return [self.class dictionaryForElement:snapshot
195+
recursive:YES
196+
excludedAttributes:excludedAttributes];
165197
}
166198

167199
- (NSDictionary *)fb_accessibilityTree
@@ -174,7 +206,7 @@ - (NSDictionary *)fb_accessibilityTree
174206

175207
+ (NSDictionary *)dictionaryForElement:(id<FBXCElementSnapshot>)snapshot
176208
recursive:(BOOL)recursive
177-
excludedAttributes:(nullable NSSet<NSString *> *) excludedAttributes
209+
excludedAttributes:(nullable NSSet<NSString *> *)excludedAttributes
178210
{
179211
NSMutableDictionary *info = [[NSMutableDictionary alloc] init];
180212
info[@"type"] = [FBElementTypeTransformer shortStringWithElementType:snapshot.elementType];
@@ -186,27 +218,27 @@ + (NSDictionary *)dictionaryForElement:(id<FBXCElementSnapshot>)snapshot
186218
info[@"rect"] = wrappedSnapshot.wdRect;
187219

188220
NSDictionary<NSString *, NSString * (^)(void)> *attributeBlocks = @{
189-
@"frame": ^{
221+
FBExclusionAttributeFrame: ^{
190222
return NSStringFromCGRect(wrappedSnapshot.wdFrame);
191223
},
192-
@"enabled": ^{
224+
FBExclusionAttributeEnabled: ^{
193225
return [@([wrappedSnapshot isWDEnabled]) stringValue];
194226
},
195-
@"visible": ^{
227+
FBExclusionAttributeVisible: ^{
196228
return [@([wrappedSnapshot isWDVisible]) stringValue];
197229
},
198-
@"accessible": ^{
230+
FBExclusionAttributeAccessible: ^{
199231
return [@([wrappedSnapshot isWDAccessible]) stringValue];
200232
},
201-
@"focused": ^{
233+
FBExclusionAttributeFocused: ^{
202234
return [@([wrappedSnapshot isWDFocused]) stringValue];
203235
}
204236
};
205237

206238
for (NSString *key in attributeBlocks) {
207239
if (excludedAttributes == nil || ![excludedAttributes containsObject:key]) {
208240
NSString *value = ((NSString * (^)(void))attributeBlocks[key])();
209-
if ([key isEqualToString:@"frame"]) {
241+
if ([key isEqualToString:FBExclusionAttributeFrame]) {
210242
info[key] = value;
211243
} else {
212244
info[[NSString stringWithFormat:@"is%@", [key capitalizedString]]] = value;
@@ -396,6 +428,8 @@ - (BOOL)fb_dismissKeyboardWithKeyNames:(nullable NSArray<NSString *> *)keyNames
396428
return nil;
397429
}
398430

431+
// These custom attributes could take too long to fetch, thus excluded
432+
NSSet *customAttributesToExclude = [NSSet setWithArray:[customExclusionAttributesMap() allKeys]];
399433
NSMutableArray<NSDictionary *> *resultArray = [NSMutableArray array];
400434
NSMethodSignature *methodSignature = [self methodSignatureForSelector:selector];
401435
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
@@ -411,9 +445,11 @@ - (BOOL)fb_dismissKeyboardWithKeyNames:(nullable NSArray<NSString *> *)keyNames
411445

412446
id extractedElement = extractIssueProperty(issue, @"element");
413447

414-
id<FBXCElementSnapshot> elementSnapshot = [extractedElement fb_takeSnapshot];
448+
id<FBXCElementSnapshot> elementSnapshot = [extractedElement fb_cachedSnapshot] ?: [extractedElement fb_takeSnapshot];
415449
NSDictionary *elementAttributes = elementSnapshot
416-
? [self.class dictionaryForElement:elementSnapshot recursive:NO excludedAttributes:nil]
450+
? [self.class dictionaryForElement:elementSnapshot
451+
recursive:NO
452+
excludedAttributes:customAttributesToExclude]
417453
: @{};
418454

419455
[resultArray addObject:@{

0 commit comments

Comments
 (0)