XCUIElement+FBWebDriverAttributes.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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+FBWebDriverAttributes.h"
  10. #import "FBElementTypeTransformer.h"
  11. #import "FBLogger.h"
  12. #import "FBMacros.h"
  13. #import "FBXCElementSnapshotWrapper.h"
  14. #import "XCUIElement+FBAccessibility.h"
  15. #import "XCUIElement+FBIsVisible.h"
  16. #import "XCUIElement+FBUID.h"
  17. #import "XCUIElement.h"
  18. #import "XCUIElement+FBUtilities.h"
  19. #import "FBElementUtils.h"
  20. #import "XCTestPrivateSymbols.h"
  21. #import "XCUIHitPointResult.h"
  22. #define BROKEN_RECT CGRectMake(-1, -1, 0, 0)
  23. @implementation XCUIElement (WebDriverAttributesForwarding)
  24. - (id<FBXCElementSnapshot>)fb_snapshotForAttributeName:(NSString *)name
  25. {
  26. // These attributes are special, because we can only retrieve them from
  27. // the snapshot if we explicitly ask XCTest to include them into the query while taking it.
  28. // That is why fb_snapshotWithAllAttributes method must be used instead of the default snapshot
  29. // call
  30. if ([name isEqualToString:FBStringify(XCUIElement, isWDVisible)]) {
  31. return [self fb_snapshotWithAttributes:@[FB_XCAXAIsVisibleAttributeName]
  32. maxDepth:@1];
  33. }
  34. if ([name isEqualToString:FBStringify(XCUIElement, isWDAccessible)]) {
  35. return [self fb_snapshotWithAttributes:@[FB_XCAXAIsElementAttributeName]
  36. maxDepth:@1];
  37. }
  38. if ([name isEqualToString:FBStringify(XCUIElement, isWDAccessibilityContainer)]) {
  39. return [self fb_snapshotWithAttributes:@[FB_XCAXAIsElementAttributeName]
  40. maxDepth:nil];
  41. }
  42. return self.fb_takeSnapshot;
  43. }
  44. - (id)fb_valueForWDAttributeName:(NSString *)name
  45. {
  46. NSString *wdAttributeName = [FBElementUtils wdAttributeNameForAttributeName:name];
  47. id<FBXCElementSnapshot> snapshot = [self fb_snapshotForAttributeName:wdAttributeName];
  48. return [[FBXCElementSnapshotWrapper ensureWrapped:snapshot] fb_valueForWDAttributeName:name];
  49. }
  50. - (id)forwardingTargetForSelector:(SEL)aSelector
  51. {
  52. static dispatch_once_t onceToken;
  53. static NSSet<NSString *> *fbElementAttributeNames;
  54. dispatch_once(&onceToken, ^{
  55. fbElementAttributeNames = [FBElementUtils selectorNamesWithProtocol:@protocol(FBElement)];
  56. });
  57. NSString* attributeName = NSStringFromSelector(aSelector);
  58. return [fbElementAttributeNames containsObject:attributeName]
  59. ? [FBXCElementSnapshotWrapper ensureWrapped:[self fb_snapshotForAttributeName:attributeName]]
  60. : nil;
  61. }
  62. @end
  63. @implementation FBXCElementSnapshotWrapper (WebDriverAttributes)
  64. - (id)fb_valueForWDAttributeName:(NSString *)name
  65. {
  66. return [self valueForKey:[FBElementUtils wdAttributeNameForAttributeName:name]];
  67. }
  68. - (NSString *)wdValue
  69. {
  70. id value = self.value;
  71. XCUIElementType elementType = self.elementType;
  72. if (elementType == XCUIElementTypeStaticText) {
  73. NSString *label = self.label;
  74. value = FBFirstNonEmptyValue(value, label);
  75. } else if (elementType == XCUIElementTypeButton) {
  76. NSNumber *isSelected = self.isSelected ? @YES : nil;
  77. value = FBFirstNonEmptyValue(value, isSelected);
  78. } else if (elementType == XCUIElementTypeSwitch) {
  79. value = @([value boolValue]);
  80. } else if (elementType == XCUIElementTypeTextView ||
  81. elementType == XCUIElementTypeTextField ||
  82. elementType == XCUIElementTypeSecureTextField) {
  83. NSString *placeholderValue = self.placeholderValue;
  84. value = FBFirstNonEmptyValue(value, placeholderValue);
  85. }
  86. value = FBTransferEmptyStringToNil(value);
  87. if (value) {
  88. value = [NSString stringWithFormat:@"%@", value];
  89. }
  90. return value;
  91. }
  92. + (NSString *)wdNameWithSnapshot:(id<FBXCElementSnapshot>)snapshot
  93. {
  94. NSString *identifier = snapshot.identifier;
  95. if (nil != identifier && identifier.length != 0) {
  96. return identifier;
  97. }
  98. NSString *label = snapshot.label;
  99. return FBTransferEmptyStringToNil(label);
  100. }
  101. - (NSString *)wdName
  102. {
  103. return [self.class wdNameWithSnapshot:self.snapshot];
  104. }
  105. - (NSString *)wdLabel
  106. {
  107. NSString *label = self.label;
  108. XCUIElementType elementType = self.elementType;
  109. if (elementType == XCUIElementTypeTextField || elementType == XCUIElementTypeSecureTextField ) {
  110. return label;
  111. }
  112. return FBTransferEmptyStringToNil(label);
  113. }
  114. - (NSString *)wdType
  115. {
  116. return [FBElementTypeTransformer stringWithElementType:self.elementType];
  117. }
  118. - (NSString *)wdUID
  119. {
  120. return self.fb_uid;
  121. }
  122. - (CGRect)wdFrame
  123. {
  124. CGRect frame = self.frame;
  125. // It is mandatory to replace all Infinity values with numbers to avoid JSON parsing
  126. // exceptions like https://github.com/facebook/WebDriverAgent/issues/639#issuecomment-314421206
  127. // caused by broken element dimensions returned by XCTest
  128. return (isinf(frame.size.width) || isinf(frame.size.height)
  129. || isinf(frame.origin.x) || isinf(frame.origin.y))
  130. ? CGRectIntegral(BROKEN_RECT)
  131. : CGRectIntegral(frame);
  132. }
  133. - (BOOL)isWDVisible
  134. {
  135. return self.fb_isVisible;
  136. }
  137. - (BOOL)isWDFocused
  138. {
  139. return self.hasFocus;
  140. }
  141. - (BOOL)isWDAccessible
  142. {
  143. XCUIElementType elementType = self.elementType;
  144. // Special cases:
  145. // Table view cell: we consider it accessible if it's container is accessible
  146. // Text fields: actual accessible element isn't text field itself, but nested element
  147. if (elementType == XCUIElementTypeCell) {
  148. if (!self.fb_isAccessibilityElement) {
  149. id<FBXCElementSnapshot> containerView = [[self children] firstObject];
  150. if (![FBXCElementSnapshotWrapper ensureWrapped:containerView].fb_isAccessibilityElement) {
  151. return NO;
  152. }
  153. }
  154. } else if (elementType != XCUIElementTypeTextField && elementType != XCUIElementTypeSecureTextField) {
  155. if (!self.fb_isAccessibilityElement) {
  156. return NO;
  157. }
  158. }
  159. id<FBXCElementSnapshot> parentSnapshot = self.parent;
  160. while (parentSnapshot) {
  161. // In the scenario when table provides Search results controller, table could be marked as accessible element, even though it isn't
  162. // As it is highly unlikely that table view should ever be an accessibility element itself,
  163. // for now we work around that by skipping Table View in container checks
  164. if ([FBXCElementSnapshotWrapper ensureWrapped:parentSnapshot].fb_isAccessibilityElement
  165. && parentSnapshot.elementType != XCUIElementTypeTable) {
  166. return NO;
  167. }
  168. parentSnapshot = parentSnapshot.parent;
  169. }
  170. return YES;
  171. }
  172. - (BOOL)isWDAccessibilityContainer
  173. {
  174. NSArray<id<FBXCElementSnapshot>> *children = self.children;
  175. for (id<FBXCElementSnapshot> child in children) {
  176. FBXCElementSnapshotWrapper *wrappedChild = [FBXCElementSnapshotWrapper ensureWrapped:child];
  177. if (wrappedChild.isWDAccessibilityContainer || wrappedChild.fb_isAccessibilityElement) {
  178. return YES;
  179. }
  180. }
  181. return NO;
  182. }
  183. - (BOOL)isWDEnabled
  184. {
  185. return self.isEnabled;
  186. }
  187. - (BOOL)isWDSelected
  188. {
  189. return self.isSelected;
  190. }
  191. - (NSUInteger)wdIndex
  192. {
  193. if (nil != self.parent) {
  194. for (NSUInteger index = 0; index < self.parent.children.count; ++index) {
  195. if ([self.parent.children objectAtIndex:index] == self.snapshot) {
  196. return index;
  197. }
  198. }
  199. }
  200. return 0;
  201. }
  202. - (BOOL)isWDHittable
  203. {
  204. XCUIHitPointResult *result = [self hitPoint:nil];
  205. return nil == result ? NO : result.hittable;
  206. }
  207. - (NSDictionary *)wdRect
  208. {
  209. CGRect frame = self.wdFrame;
  210. return @{
  211. @"x": @(CGRectGetMinX(frame)),
  212. @"y": @(CGRectGetMinY(frame)),
  213. @"width": @(CGRectGetWidth(frame)),
  214. @"height": @(CGRectGetHeight(frame)),
  215. };
  216. }
  217. @end