XCElementSnapshotHelperTests.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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:self.testedView.fb_takeSnapshot]
  42. fb_descendantsMatchingType:XCUIElementTypeButton];
  43. XCTAssertEqual(matchingSnapshots.count, expectedLabels.count);
  44. NSArray<NSString *> *labels = [matchingSnapshots valueForKeyPath:@"@distinctUnionOfObjects.label"];
  45. XCTAssertEqualObjects([NSSet setWithArray:labels], expectedLabels);
  46. NSArray<NSNumber *> *types = [matchingSnapshots valueForKeyPath:@"@distinctUnionOfObjects.elementType"];
  47. XCTAssertEqual(types.count, 1, @"matchingSnapshots should contain only one type");
  48. XCTAssertEqualObjects(types.lastObject, @(XCUIElementTypeButton), @"matchingSnapshots should contain only one type");
  49. }
  50. - (void)testParentMatchingType
  51. {
  52. XCUIElement *button = self.testedApplication.buttons[@"Alerts"];
  53. FBAssertWaitTillBecomesTrue(button.exists);
  54. id<FBXCElementSnapshot> windowSnapshot = [[FBXCElementSnapshotWrapper ensureWrapped:button.fb_takeSnapshot]
  55. fb_parentMatchingType:XCUIElementTypeWindow];
  56. XCTAssertNotNil(windowSnapshot);
  57. XCTAssertEqual(windowSnapshot.elementType, XCUIElementTypeWindow);
  58. }
  59. @end
  60. @interface XCElementSnapshotHelperTests_AttributePage : FBIntegrationTestCase
  61. @end
  62. @implementation XCElementSnapshotHelperTests_AttributePage
  63. - (void)setUp
  64. {
  65. [super setUp];
  66. static dispatch_once_t onceToken;
  67. dispatch_once(&onceToken, ^{
  68. [self launchApplication];
  69. [self goToAttributesPage];
  70. });
  71. }
  72. - (void)testParentMatchingOneOfTypes
  73. {
  74. XCUIElement *todayPickerWheel = self.testedApplication.pickerWheels[@"Today"];
  75. FBAssertWaitTillBecomesTrue(todayPickerWheel.exists);
  76. id<FBXCElementSnapshot> datePicker = [[FBXCElementSnapshotWrapper ensureWrapped:todayPickerWheel.fb_takeSnapshot]
  77. fb_parentMatchingOneOfTypes:@[@(XCUIElementTypeDatePicker), @(XCUIElementTypeWindow)]];
  78. XCTAssertNotNil(datePicker);
  79. XCTAssertEqual(datePicker.elementType, XCUIElementTypeDatePicker);
  80. }
  81. - (void)testParentMatchingOneOfTypesWithXCUIElementTypeAny
  82. {
  83. XCUIElement *todayPickerWheel = self.testedApplication.pickerWheels[@"Today"];
  84. FBAssertWaitTillBecomesTrue(todayPickerWheel.exists);
  85. id<FBXCElementSnapshot> otherSnapshot =[[FBXCElementSnapshotWrapper ensureWrapped:todayPickerWheel.fb_takeSnapshot]
  86. fb_parentMatchingOneOfTypes:@[@(XCUIElementTypeAny)]];
  87. XCTAssertNotNil(otherSnapshot);
  88. }
  89. - (void)testParentMatchingOneOfTypesWithAbsentParents
  90. {
  91. XCUIElement *todayPickerWheel = self.testedApplication.pickerWheels[@"Today"];
  92. FBAssertWaitTillBecomesTrue(todayPickerWheel.exists);
  93. id<FBXCElementSnapshot> otherSnapshot = [[FBXCElementSnapshotWrapper ensureWrapped:todayPickerWheel.fb_takeSnapshot]
  94. fb_parentMatchingOneOfTypes:@[@(XCUIElementTypeTab), @(XCUIElementTypeLink)]];
  95. XCTAssertNil(otherSnapshot);
  96. }
  97. @end
  98. @interface XCElementSnapshotHelperTests_ScrollView : FBIntegrationTestCase
  99. @end
  100. @implementation XCElementSnapshotHelperTests_ScrollView
  101. - (void)setUp
  102. {
  103. [super setUp];
  104. static dispatch_once_t onceToken;
  105. dispatch_once(&onceToken, ^{
  106. [self launchApplication];
  107. [self goToScrollPageWithCells:false];
  108. });
  109. }
  110. - (void)testParentMatchingOneOfTypesWithFilter
  111. {
  112. XCUIElement *threeStaticText = self.testedApplication.staticTexts[@"3"];
  113. FBAssertWaitTillBecomesTrue(threeStaticText.exists);
  114. NSArray *acceptedParents = @[
  115. @(XCUIElementTypeScrollView),
  116. @(XCUIElementTypeCollectionView),
  117. @(XCUIElementTypeTable),
  118. ];
  119. id<FBXCElementSnapshot> scrollView = [[FBXCElementSnapshotWrapper ensureWrapped:threeStaticText.fb_takeSnapshot]
  120. fb_parentMatchingOneOfTypes:acceptedParents
  121. filter:^BOOL(id<FBXCElementSnapshot> snapshot) {
  122. return [[FBXCElementSnapshotWrapper ensureWrapped:snapshot] isWDVisible];
  123. }];
  124. XCTAssertEqualObjects(scrollView.identifier, @"scrollView");
  125. }
  126. - (void)testParentMatchingOneOfTypesWithFilterRetruningNo
  127. {
  128. XCUIElement *threeStaticText = self.testedApplication.staticTexts[@"3"];
  129. FBAssertWaitTillBecomesTrue(threeStaticText.exists);
  130. NSArray *acceptedParents = @[
  131. @(XCUIElementTypeScrollView),
  132. @(XCUIElementTypeCollectionView),
  133. @(XCUIElementTypeTable),
  134. ];
  135. id<FBXCElementSnapshot> scrollView = [[FBXCElementSnapshotWrapper ensureWrapped:threeStaticText.fb_takeSnapshot]
  136. fb_parentMatchingOneOfTypes:acceptedParents
  137. filter:^BOOL(id<FBXCElementSnapshot> snapshot) {
  138. return NO;
  139. }];
  140. XCTAssertNil(scrollView);
  141. }
  142. - (void)testDescendantsCellSnapshots
  143. {
  144. XCUIElement *scrollView = self.testedApplication.scrollViews[@"scrollView"];
  145. FBAssertWaitTillBecomesTrue(self.testedApplication.staticTexts[@"3"].fb_isVisible);
  146. NSArray *cells = [[FBXCElementSnapshotWrapper ensureWrapped:scrollView.fb_takeSnapshot] fb_descendantsCellSnapshots];
  147. XCTAssertGreaterThanOrEqual(cells.count, 10);
  148. id<FBXCElementSnapshot> element = cells.firstObject;
  149. XCTAssertEqualObjects(element.label, @"0");
  150. }
  151. @end
  152. @interface XCElementSnapshotHelperTests_ScrollViewCells : FBIntegrationTestCase
  153. @end
  154. @implementation XCElementSnapshotHelperTests_ScrollViewCells
  155. - (void)setUp
  156. {
  157. [super setUp];
  158. static dispatch_once_t onceToken;
  159. dispatch_once(&onceToken, ^{
  160. [self launchApplication];
  161. [self goToScrollPageWithCells:true];
  162. });
  163. }
  164. - (void)testParentCellSnapshot
  165. {
  166. FBAssertWaitTillBecomesTrue(self.testedApplication.staticTexts[@"3"].fb_isVisible);
  167. XCUIElement *threeStaticText = self.testedApplication.staticTexts[@"3"];
  168. id<FBXCElementSnapshot> xcuiElementCell = [[FBXCElementSnapshotWrapper ensureWrapped:threeStaticText.fb_takeSnapshot] fb_parentCellSnapshot];
  169. XCTAssertEqual(xcuiElementCell.elementType, 75);
  170. }
  171. @end