XCUIElement+FBUtilities.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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+FBUtilities.h"
  10. #import <objc/runtime.h>
  11. #import "FBConfiguration.h"
  12. #import "FBExceptions.h"
  13. #import "FBImageUtils.h"
  14. #import "FBLogger.h"
  15. #import "FBMacros.h"
  16. #import "FBMathUtils.h"
  17. #import "FBRunLoopSpinner.h"
  18. #import "FBSettings.h"
  19. #import "FBScreenshot.h"
  20. #import "FBXCAXClientProxy.h"
  21. #import "FBXCodeCompatibility.h"
  22. #import "FBXCElementSnapshot.h"
  23. #import "FBXCElementSnapshotWrapper+Helpers.h"
  24. #import "XCUIApplication.h"
  25. #import "XCUIApplication+FBQuiescence.h"
  26. #import "XCUIApplicationImpl.h"
  27. #import "XCUIApplicationProcess.h"
  28. #import "XCTElementSetTransformer-Protocol.h"
  29. #import "XCTestPrivateSymbols.h"
  30. #import "XCTRunnerDaemonSession.h"
  31. #import "XCUIElement+FBCaching.h"
  32. #import "XCUIElement+FBWebDriverAttributes.h"
  33. #import "XCUIElementQuery.h"
  34. #import "XCUIElementQuery+FBHelpers.h"
  35. #import "XCUIElement+FBUID.h"
  36. #import "XCUIScreen.h"
  37. #import "XCUIElement+FBResolve.h"
  38. #define DEFAULT_AX_TIMEOUT 60.
  39. @implementation XCUIElement (FBUtilities)
  40. - (id<FBXCElementSnapshot>)fb_takeSnapshot
  41. {
  42. NSError *error = nil;
  43. self.fb_isResolvedFromCache = @(NO);
  44. self.lastSnapshot = [self.fb_query fb_uniqueSnapshotWithError:&error];
  45. if (nil == self.lastSnapshot) {
  46. NSString *hintText = @"Make sure the application UI has the expected state";
  47. if (nil != error
  48. && [error.localizedDescription containsString:@"Identity Binding"]) {
  49. hintText = [NSString stringWithFormat:@"%@. You could also try to switch the binding strategy using the 'boundElementsByIndex' setting for the element lookup", hintText];
  50. }
  51. NSString *reason = [NSString stringWithFormat:@"The previously found element \"%@\" is not present in the current view anymore. %@", self.description, hintText];
  52. if (nil != error) {
  53. reason = [NSString stringWithFormat:@"%@. Original error: %@", reason, error.localizedDescription];
  54. }
  55. @throw [NSException exceptionWithName:FBStaleElementException reason:reason userInfo:@{}];
  56. }
  57. return self.lastSnapshot;
  58. }
  59. - (id<FBXCElementSnapshot>)fb_cachedSnapshot
  60. {
  61. return [self.query fb_cachedSnapshot];
  62. }
  63. - (nullable id<FBXCElementSnapshot>)fb_snapshotWithAllAttributesAndMaxDepth:(NSNumber *)maxDepth
  64. {
  65. NSMutableArray *allNames = [NSMutableArray arrayWithArray:FBStandardAttributeNames()];
  66. [allNames addObjectsFromArray:FBCustomAttributeNames()];
  67. return [self fb_snapshotWithAttributes:allNames.copy
  68. maxDepth:maxDepth];
  69. }
  70. - (nullable id<FBXCElementSnapshot>)fb_snapshotWithAttributes:(NSArray<NSString *> *)attributeNames
  71. maxDepth:(NSNumber *)maxDepth
  72. {
  73. NSSet<NSString *> *standardAttributes = [NSSet setWithArray:FBStandardAttributeNames()];
  74. id<FBXCElementSnapshot> snapshot = self.fb_takeSnapshot;
  75. NSTimeInterval axTimeout = FBConfiguration.customSnapshotTimeout;
  76. if (nil == attributeNames
  77. || [[NSSet setWithArray:attributeNames] isSubsetOfSet:standardAttributes]
  78. || axTimeout < DBL_EPSILON) {
  79. // return the "normal" element snapshot if no custom attributes are requested
  80. return snapshot;
  81. }
  82. id<FBXCAccessibilityElement> axElement = snapshot.accessibilityElement;
  83. if (nil == axElement) {
  84. return nil;
  85. }
  86. NSError *setTimeoutError;
  87. BOOL isTimeoutSet = [FBXCAXClientProxy.sharedClient setAXTimeout:axTimeout
  88. error:&setTimeoutError];
  89. if (!isTimeoutSet) {
  90. [FBLogger logFmt:@"Cannot set snapshoting timeout to %.1fs. Original error: %@",
  91. axTimeout, setTimeoutError.localizedDescription];
  92. }
  93. NSError *error;
  94. id<FBXCElementSnapshot> snapshotWithAttributes = [FBXCAXClientProxy.sharedClient snapshotForElement:axElement
  95. attributes:attributeNames
  96. maxDepth:maxDepth
  97. error:&error];
  98. if (nil == snapshotWithAttributes) {
  99. NSString *description = [FBXCElementSnapshotWrapper ensureWrapped:snapshot].fb_description;
  100. [FBLogger logFmt:@"Cannot take a snapshot with attribute(s) %@ of '%@' after %.2f seconds",
  101. attributeNames, description, axTimeout];
  102. [FBLogger logFmt:@"This timeout could be customized via '%@' setting", FB_SETTING_CUSTOM_SNAPSHOT_TIMEOUT];
  103. [FBLogger logFmt:@"Internal error: %@", error.localizedDescription];
  104. [FBLogger logFmt:@"Falling back to the default snapshotting mechanism for the element '%@' (some attribute values, like visibility or accessibility might not be precise though)", description];
  105. snapshotWithAttributes = self.lastSnapshot;
  106. } else {
  107. self.lastSnapshot = snapshotWithAttributes;
  108. }
  109. if (isTimeoutSet) {
  110. [FBXCAXClientProxy.sharedClient setAXTimeout:DEFAULT_AX_TIMEOUT error:nil];
  111. }
  112. return snapshotWithAttributes;
  113. }
  114. - (NSArray<XCUIElement *> *)fb_filterDescendantsWithSnapshots:(NSArray<id<FBXCElementSnapshot>> *)snapshots
  115. selfUID:(NSString *)selfUID
  116. onlyChildren:(BOOL)onlyChildren
  117. {
  118. if (0 == snapshots.count) {
  119. return @[];
  120. }
  121. NSMutableArray<NSString *> *matchedIds = [NSMutableArray new];
  122. for (id<FBXCElementSnapshot> snapshot in snapshots) {
  123. NSString *uid = [FBXCElementSnapshotWrapper wdUIDWithSnapshot:snapshot];
  124. if (nil != uid) {
  125. [matchedIds addObject:uid];
  126. }
  127. }
  128. NSMutableArray<XCUIElement *> *matchedElements = [NSMutableArray array];
  129. NSString *uid = selfUID;
  130. if (nil == uid) {
  131. uid = self.fb_isResolvedFromCache.boolValue
  132. ? [FBXCElementSnapshotWrapper wdUIDWithSnapshot:self.lastSnapshot]
  133. : self.fb_uid;
  134. }
  135. if (nil != uid && [matchedIds containsObject:uid]) {
  136. XCUIElement *stableSelf = self.fb_stableInstance;
  137. stableSelf.fb_isResolvedNatively = @NO;
  138. if (1 == snapshots.count) {
  139. return @[stableSelf];
  140. }
  141. [matchedElements addObject:stableSelf];
  142. }
  143. XCUIElementType type = XCUIElementTypeAny;
  144. NSArray<NSNumber *> *uniqueTypes = [snapshots valueForKeyPath:[NSString stringWithFormat:@"@distinctUnionOfObjects.%@", FBStringify(XCUIElement, elementType)]];
  145. if (uniqueTypes && [uniqueTypes count] == 1) {
  146. type = [uniqueTypes.firstObject intValue];
  147. }
  148. XCUIElementQuery *query = onlyChildren
  149. ? [self.fb_query childrenMatchingType:type]
  150. : [self.fb_query descendantsMatchingType:type];
  151. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K IN %@",FBStringify(FBXCElementSnapshotWrapper, fb_uid), matchedIds];
  152. [matchedElements addObjectsFromArray:[query matchingPredicate:predicate].allElementsBoundByIndex];
  153. for (XCUIElement *el in matchedElements) {
  154. el.fb_isResolvedNatively = @NO;
  155. }
  156. return matchedElements.copy;
  157. }
  158. - (void)fb_waitUntilStable
  159. {
  160. [self fb_waitUntilStableWithTimeout:FBConfiguration.waitForIdleTimeout];
  161. }
  162. - (void)fb_waitUntilStableWithTimeout:(NSTimeInterval)timeout
  163. {
  164. if (timeout < DBL_EPSILON) {
  165. return;
  166. }
  167. NSTimeInterval previousTimeout = FBConfiguration.waitForIdleTimeout;
  168. BOOL previousQuiescence = self.application.fb_shouldWaitForQuiescence;
  169. FBConfiguration.waitForIdleTimeout = timeout;
  170. if (!previousQuiescence) {
  171. self.application.fb_shouldWaitForQuiescence = YES;
  172. }
  173. [[[self.application applicationImpl] currentProcess]
  174. waitForQuiescenceIncludingAnimationsIdle:YES];
  175. if (previousQuiescence != self.application.fb_shouldWaitForQuiescence) {
  176. self.application.fb_shouldWaitForQuiescence = previousQuiescence;
  177. }
  178. FBConfiguration.waitForIdleTimeout = previousTimeout;
  179. }
  180. @end