XCElementSnapshotHelperTests.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 <XCTest/XCTest.h>
  10. #import "FBIntegrationTestCase.h"
  11. #import "FBTestMacros.h"
  12. #import "XCUIElement.h"
  13. #import "XCUIElement+FBIsVisible.h"
  14. #import "XCUIElement+FBUtilities.h"
  15. #import "XCUIElement+FBWebDriverAttributes.h"
  16. #import "FBXCElementSnapshotWrapper+Helpers.h"
  17. #import "FBXCodeCompatibility.h"
  18. @interface XCElementSnapshotHelperTests : FBIntegrationTestCase
  19. @property (nonatomic, strong) XCUIElement *testedView;
  20. @end
  21. @implementation XCElementSnapshotHelperTests
  22. - (void)setUp
  23. {
  24. [super setUp];
  25. static dispatch_once_t onceToken;
  26. dispatch_once(&onceToken, ^{
  27. [self launchApplication];
  28. });
  29. self.testedView = self.testedApplication.otherElements[@"MainView"];
  30. XCTAssertTrue(self.testedView.exists);
  31. }
  32. - (void)testDescendantsMatchingType
  33. {
  34. NSSet<NSString *> *expectedLabels = [NSSet setWithArray:@[
  35. @"Alerts",
  36. @"Attributes",
  37. @"Scrolling",
  38. @"Deadlock app",
  39. @"Touch",
  40. ]];
  41. NSArray<id<FBXCElementSnapshot>> *matchingSnapshots = [[FBXCElementSnapshotWrapper ensureWrapped:
  42. [self.testedView fb_customSnapshot]]
  43. fb_descendantsMatchingType:XCUIElementTypeButton];
  44. XCTAssertEqual(matchingSnapshots.count, expectedLabels.count);
  45. NSArray<NSString *> *labels = [matchingSnapshots valueForKeyPath:@"@distinctUnionOfObjects.label"];
  46. XCTAssertEqualObjects([NSSet setWithArray:labels], expectedLabels);
  47. NSArray<NSNumber *> *types = [matchingSnapshots valueForKeyPath:@"@distinctUnionOfObjects.elementType"];
  48. XCTAssertEqual(types.count, 1, @"matchingSnapshots should contain only one type");
  49. XCTAssertEqualObjects(types.lastObject, @(XCUIElementTypeButton), @"matchingSnapshots should contain only one type");
  50. }
  51. - (void)testParentMatchingType
  52. {
  53. XCUIElement *button = self.testedApplication.buttons[@"Alerts"];
  54. FBAssertWaitTillBecomesTrue(button.exists);
  55. id<FBXCElementSnapshot> windowSnapshot = [[FBXCElementSnapshotWrapper ensureWrapped:
  56. [self.testedView fb_customSnapshot]]
  57. fb_parentMatchingType:XCUIElementTypeWindow];
  58. XCTAssertNotNil(windowSnapshot);
  59. XCTAssertEqual(windowSnapshot.elementType, XCUIElementTypeWindow);
  60. }
  61. @end
  62. @interface XCElementSnapshotHelperTests_AttributePage : FBIntegrationTestCase
  63. @end
  64. @implementation XCElementSnapshotHelperTests_AttributePage
  65. - (void)setUp
  66. {
  67. [super setUp];
  68. static dispatch_once_t onceToken;
  69. dispatch_once(&onceToken, ^{
  70. [self launchApplication];
  71. [self goToAttributesPage];
  72. });
  73. }
  74. - (void)testParentMatchingOneOfTypes
  75. {
  76. XCUIElement *todayPickerWheel = self.testedApplication.pickerWheels[@"Today"];
  77. FBAssertWaitTillBecomesTrue(todayPickerWheel.exists);
  78. id<FBXCElementSnapshot> datePicker = [[FBXCElementSnapshotWrapper ensureWrapped:
  79. [todayPickerWheel fb_customSnapshot]]
  80. fb_parentMatchingOneOfTypes:@[@(XCUIElementTypeDatePicker), @(XCUIElementTypeWindow)]];
  81. XCTAssertNotNil(datePicker);
  82. XCTAssertEqual(datePicker.elementType, XCUIElementTypeDatePicker);
  83. }
  84. - (void)testParentMatchingOneOfTypesWithXCUIElementTypeAny
  85. {
  86. XCUIElement *todayPickerWheel = self.testedApplication.pickerWheels[@"Today"];
  87. FBAssertWaitTillBecomesTrue(todayPickerWheel.exists);
  88. id<FBXCElementSnapshot> otherSnapshot =[[FBXCElementSnapshotWrapper ensureWrapped:
  89. [todayPickerWheel fb_customSnapshot]]
  90. fb_parentMatchingOneOfTypes:@[@(XCUIElementTypeAny)]];
  91. XCTAssertNotNil(otherSnapshot);
  92. }
  93. - (void)testParentMatchingOneOfTypesWithAbsentParents
  94. {
  95. XCUIElement *todayPickerWheel = self.testedApplication.pickerWheels[@"Today"];
  96. FBAssertWaitTillBecomesTrue(todayPickerWheel.exists);
  97. id<FBXCElementSnapshot> otherSnapshot = [[FBXCElementSnapshotWrapper ensureWrapped:
  98. [todayPickerWheel fb_customSnapshot]]
  99. fb_parentMatchingOneOfTypes:@[@(XCUIElementTypeTab), @(XCUIElementTypeLink)]];
  100. XCTAssertNil(otherSnapshot);
  101. }
  102. @end
  103. @interface XCElementSnapshotHelperTests_ScrollView : FBIntegrationTestCase
  104. @end
  105. @implementation XCElementSnapshotHelperTests_ScrollView
  106. - (void)setUp
  107. {
  108. [super setUp];
  109. static dispatch_once_t onceToken;
  110. dispatch_once(&onceToken, ^{
  111. [self launchApplication];
  112. [self goToScrollPageWithCells:false];
  113. });
  114. }
  115. - (void)testParentMatchingOneOfTypesWithFilter
  116. {
  117. XCUIElement *threeStaticText = self.testedApplication.staticTexts[@"3"];
  118. FBAssertWaitTillBecomesTrue(threeStaticText.exists);
  119. NSArray *acceptedParents = @[
  120. @(XCUIElementTypeScrollView),
  121. @(XCUIElementTypeCollectionView),
  122. @(XCUIElementTypeTable),
  123. ];
  124. id<FBXCElementSnapshot> scrollView = [[FBXCElementSnapshotWrapper ensureWrapped:
  125. [threeStaticText fb_customSnapshot]]
  126. fb_parentMatchingOneOfTypes:acceptedParents
  127. filter:^BOOL(id<FBXCElementSnapshot> snapshot) {
  128. return [[FBXCElementSnapshotWrapper ensureWrapped:snapshot] isWDVisible];
  129. }];
  130. XCTAssertEqualObjects(scrollView.identifier, @"scrollView");
  131. }
  132. - (void)testParentMatchingOneOfTypesWithFilterRetruningNo
  133. {
  134. XCUIElement *threeStaticText = self.testedApplication.staticTexts[@"3"];
  135. FBAssertWaitTillBecomesTrue(threeStaticText.exists);
  136. NSArray *acceptedParents = @[
  137. @(XCUIElementTypeScrollView),
  138. @(XCUIElementTypeCollectionView),
  139. @(XCUIElementTypeTable),
  140. ];
  141. id<FBXCElementSnapshot> scrollView = [[FBXCElementSnapshotWrapper ensureWrapped:
  142. [threeStaticText fb_customSnapshot]]
  143. fb_parentMatchingOneOfTypes:acceptedParents
  144. filter:^BOOL(id<FBXCElementSnapshot> snapshot) {
  145. return NO;
  146. }];
  147. XCTAssertNil(scrollView);
  148. }
  149. - (void)testDescendantsCellSnapshots
  150. {
  151. XCUIElement *scrollView = self.testedApplication.scrollViews[@"scrollView"];
  152. FBAssertWaitTillBecomesTrue(self.testedApplication.staticTexts[@"3"].fb_isVisible);
  153. NSArray *cells = [[FBXCElementSnapshotWrapper ensureWrapped:
  154. [scrollView fb_customSnapshot]]
  155. fb_descendantsCellSnapshots];
  156. XCTAssertGreaterThanOrEqual(cells.count, 10);
  157. id<FBXCElementSnapshot> element = cells.firstObject;
  158. XCTAssertEqualObjects(element.label, @"0");
  159. }
  160. @end
  161. @interface XCElementSnapshotHelperTests_ScrollViewCells : FBIntegrationTestCase
  162. @end
  163. @implementation XCElementSnapshotHelperTests_ScrollViewCells
  164. - (void)setUp
  165. {
  166. [super setUp];
  167. static dispatch_once_t onceToken;
  168. dispatch_once(&onceToken, ^{
  169. [self launchApplication];
  170. [self goToScrollPageWithCells:true];
  171. });
  172. }
  173. - (void)testParentCellSnapshot
  174. {
  175. FBAssertWaitTillBecomesTrue(self.testedApplication.staticTexts[@"3"].fb_isVisible);
  176. XCUIElement *threeStaticText = self.testedApplication.staticTexts[@"3"];
  177. id<FBXCElementSnapshot> xcuiElementCell = [[FBXCElementSnapshotWrapper ensureWrapped:
  178. [threeStaticText fb_customSnapshot]]
  179. fb_parentCellSnapshot];
  180. XCTAssertEqual(xcuiElementCell.elementType, 75);
  181. }
  182. @end