XCUIElement+FBFind.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. #import "XCUIElement+FBFind.h"
  10. #import "FBMacros.h"
  11. #import "FBElementTypeTransformer.h"
  12. #import "FBConfiguration.h"
  13. #import "NSPredicate+FBFormat.h"
  14. #import "FBXCElementSnapshotWrapper+Helpers.h"
  15. #import "FBXCodeCompatibility.h"
  16. #import "XCUIElement+FBCaching.h"
  17. #import "XCUIElement+FBUID.h"
  18. #import "XCUIElement+FBUtilities.h"
  19. #import "XCUIElement+FBWebDriverAttributes.h"
  20. #import "XCUIElementQuery.h"
  21. #import "FBElementUtils.h"
  22. #import "FBXCodeCompatibility.h"
  23. #import "FBXPath.h"
  24. @implementation XCUIElement (FBFind)
  25. + (NSArray<XCUIElement *> *)fb_extractMatchingElementsFromQuery:(XCUIElementQuery *)query
  26. shouldReturnAfterFirstMatch:(BOOL)shouldReturnAfterFirstMatch
  27. {
  28. if (!shouldReturnAfterFirstMatch) {
  29. return query.fb_allMatches;
  30. }
  31. XCUIElement *matchedElement = query.fb_firstMatch;
  32. return matchedElement ? @[matchedElement] : @[];
  33. }
  34. - (id<FBXCElementSnapshot>)fb_cachedSnapshotWithQuery:(XCUIElementQuery *)query
  35. {
  36. return [self isKindOfClass:XCUIApplication.class] ? query.rootElementSnapshot : self.fb_cachedSnapshot;
  37. }
  38. #pragma mark - Search by ClassName
  39. - (NSArray<XCUIElement *> *)fb_descendantsMatchingClassName:(NSString *)className
  40. shouldReturnAfterFirstMatch:(BOOL)shouldReturnAfterFirstMatch
  41. {
  42. XCUIElementType type = [FBElementTypeTransformer elementTypeWithTypeName:className];
  43. XCUIElementQuery *query = [self.fb_query descendantsMatchingType:type];
  44. NSMutableArray *result = [NSMutableArray array];
  45. [result addObjectsFromArray:[self.class fb_extractMatchingElementsFromQuery:query
  46. shouldReturnAfterFirstMatch:shouldReturnAfterFirstMatch]];
  47. id<FBXCElementSnapshot> cachedSnapshot = [self fb_cachedSnapshotWithQuery:query];
  48. if (type == XCUIElementTypeAny || cachedSnapshot.elementType == type) {
  49. if (shouldReturnAfterFirstMatch || result.count == 0) {
  50. return @[self];
  51. }
  52. [result insertObject:self atIndex:0];
  53. }
  54. return result.copy;
  55. }
  56. #pragma mark - Search by property value
  57. - (NSArray<XCUIElement *> *)fb_descendantsMatchingProperty:(NSString *)property
  58. value:(NSString *)value
  59. partialSearch:(BOOL)partialSearch
  60. {
  61. NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:(partialSearch ? @"%K CONTAINS %@" : @"%K == %@"), property, value];
  62. return [self fb_descendantsMatchingPredicate:searchPredicate shouldReturnAfterFirstMatch:NO];
  63. }
  64. #pragma mark - Search by Predicate String
  65. - (NSArray<XCUIElement *> *)fb_descendantsMatchingPredicate:(NSPredicate *)predicate
  66. shouldReturnAfterFirstMatch:(BOOL)shouldReturnAfterFirstMatch
  67. {
  68. NSPredicate *formattedPredicate = [NSPredicate fb_snapshotBlockPredicateWithPredicate:predicate];
  69. XCUIElementQuery *query = [[self.fb_query descendantsMatchingType:XCUIElementTypeAny] matchingPredicate:formattedPredicate];
  70. NSMutableArray<XCUIElement *> *result = [NSMutableArray array];
  71. [result addObjectsFromArray:[self.class fb_extractMatchingElementsFromQuery:query
  72. shouldReturnAfterFirstMatch:shouldReturnAfterFirstMatch]];
  73. id<FBXCElementSnapshot> cachedSnapshot = [self fb_cachedSnapshotWithQuery:query];
  74. // Include self element into predicate search
  75. if ([formattedPredicate evaluateWithObject:cachedSnapshot]) {
  76. if (shouldReturnAfterFirstMatch || result.count == 0) {
  77. return @[self];
  78. }
  79. [result insertObject:self atIndex:0];
  80. }
  81. return result.copy;
  82. }
  83. #pragma mark - Search by xpath
  84. - (NSArray<XCUIElement *> *)fb_descendantsMatchingXPathQuery:(NSString *)xpathQuery
  85. shouldReturnAfterFirstMatch:(BOOL)shouldReturnAfterFirstMatch
  86. {
  87. // XPath will try to match elements only class name, so requesting elements by XCUIElementTypeAny will not work. We should use '*' instead.
  88. xpathQuery = [xpathQuery stringByReplacingOccurrencesOfString:@"XCUIElementTypeAny" withString:@"*"];
  89. NSArray<id<FBXCElementSnapshot>> *matchingSnapshots = [FBXPath matchesWithRootElement:self forQuery:xpathQuery];
  90. if (0 == [matchingSnapshots count]) {
  91. return @[];
  92. }
  93. if (shouldReturnAfterFirstMatch) {
  94. id<FBXCElementSnapshot> snapshot = matchingSnapshots.firstObject;
  95. matchingSnapshots = @[snapshot];
  96. }
  97. XCUIElement *scopeRoot = FBConfiguration.limitXpathContextScope ? self : self.application;
  98. return [scopeRoot fb_filterDescendantsWithSnapshots:matchingSnapshots
  99. onlyChildren:NO];
  100. }
  101. #pragma mark - Search by Accessibility Id
  102. - (NSArray<XCUIElement *> *)fb_descendantsMatchingIdentifier:(NSString *)accessibilityId
  103. shouldReturnAfterFirstMatch:(BOOL)shouldReturnAfterFirstMatch
  104. {
  105. NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id<FBXCElementSnapshot> snapshot,
  106. NSDictionary<NSString *,id> * _Nullable bindings) {
  107. @autoreleasepool {
  108. return [[FBXCElementSnapshotWrapper wdNameWithSnapshot:snapshot] isEqualToString:accessibilityId];
  109. }
  110. }];
  111. return [self fb_descendantsMatchingPredicate:predicate
  112. shouldReturnAfterFirstMatch:shouldReturnAfterFirstMatch];
  113. }
  114. @end