FBElementAttributeTests.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "FBFindElementCommands.h"
  12. #import "FBTestMacros.h"
  13. #import "FBXCodeCompatibility.h"
  14. #import "XCUIElement+FBAccessibility.h"
  15. #import "XCUIElement+FBIsVisible.h"
  16. #import "XCUIElement+FBWebDriverAttributes.h"
  17. @interface FBElementAttributeTests : FBIntegrationTestCase
  18. @end
  19. @implementation FBElementAttributeTests
  20. - (void)setUp
  21. {
  22. [super setUp];
  23. static dispatch_once_t onceToken;
  24. dispatch_once(&onceToken, ^{
  25. [self launchApplication];
  26. [self goToAttributesPage];
  27. });
  28. }
  29. - (void)testElementAccessibilityAttributes
  30. {
  31. // "Button" is accessibility element, and therefore isn't accessibility container
  32. XCUIElement *buttonElement = self.testedApplication.buttons[@"Button"];
  33. XCTAssertTrue(buttonElement.exists);
  34. XCTAssertTrue(buttonElement.fb_isAccessibilityElement);
  35. XCTAssertFalse(buttonElement.isWDAccessibilityContainer);
  36. }
  37. - (void)testContainerAccessibilityAttributes
  38. {
  39. // "not_accessible" isn't accessibility element, but contains accessibility elements, so it is accessibility container
  40. XCUIElement *inaccessibleButtonElement = self.testedApplication.buttons[@"not_accessible"];
  41. XCTAssertTrue(inaccessibleButtonElement.exists);
  42. XCTAssertFalse(inaccessibleButtonElement.fb_isAccessibilityElement);
  43. // FIXME: Xcode 11 environment returns false even if iOS 12
  44. // We must fix here to XCTAssertTrue if Xcode version will return the value properly
  45. XCTAssertFalse(inaccessibleButtonElement.isWDAccessibilityContainer);
  46. }
  47. - (void)testIgnoredAccessibilityAttributes
  48. {
  49. // Images are neither accessibility elements nor contain them, so both checks should fail
  50. XCUIElement *imageElement = self.testedApplication.images.allElementsBoundByIndex.firstObject;
  51. XCTAssertTrue(imageElement.exists);
  52. XCTAssertFalse(imageElement.fb_isAccessibilityElement);
  53. XCTAssertFalse(imageElement.isWDAccessibilityContainer);
  54. }
  55. - (void)testButtonAttributes
  56. {
  57. XCUIElement *element = self.testedApplication.buttons[@"Button"];
  58. XCTAssertTrue(element.exists);
  59. XCTAssertEqualObjects(element.wdType, @"XCUIElementTypeButton");
  60. XCTAssertEqualObjects(element.wdName, @"Button");
  61. XCTAssertEqualObjects(element.wdLabel, @"Button");
  62. XCTAssertNil(element.wdValue);
  63. XCTAssertFalse(element.wdSelected);
  64. XCTAssertTrue(element.fb_isVisible);
  65. [element tap];
  66. XCTAssertTrue(element.wdValue.boolValue);
  67. XCTAssertTrue(element.wdSelected);
  68. }
  69. - (void)testLabelAttributes
  70. {
  71. XCUIElement *element = self.testedApplication.staticTexts[@"Label"];
  72. XCTAssertTrue(element.exists);
  73. XCTAssertEqualObjects(element.wdType, @"XCUIElementTypeStaticText");
  74. XCTAssertEqualObjects(element.wdName, @"Label");
  75. XCTAssertEqualObjects(element.wdLabel, @"Label");
  76. XCTAssertEqualObjects(element.wdValue, @"Label");
  77. }
  78. - (void)testIndexAttributes
  79. {
  80. XCUIElement *element = self.testedApplication.buttons[@"Button"];
  81. XCTAssertTrue(element.exists);
  82. XCTAssertEqual(element.wdIndex, 2);
  83. XCUIElement *element2 = self.testedApplication;
  84. XCTAssertTrue(element2.exists);
  85. XCTAssertEqual(element2.wdIndex, 0);
  86. }
  87. - (void)testAccessibilityTraits
  88. {
  89. XCUIElement *button = self.testedApplication.buttons.firstMatch;
  90. XCTAssertTrue(button.exists);
  91. NSArray *buttonTraits = [button.wdTraits componentsSeparatedByString:@", "];
  92. NSArray *expectedButtonTraits = @[@"Button"];
  93. XCTAssertEqual(buttonTraits.count, expectedButtonTraits.count, @"Button should have exactly 1 trait");
  94. XCTAssertEqualObjects(buttonTraits, expectedButtonTraits);
  95. XCTAssertEqualObjects(button.wdType, @"XCUIElementTypeButton");
  96. XCUIElement *toggle = self.testedApplication.switches.firstMatch;
  97. XCTAssertTrue(toggle.exists);
  98. // iOS 17.0 specific traits if available
  99. NSArray *toggleTraits = [toggle.wdTraits componentsSeparatedByString:@", "];
  100. NSArray *expectedToggleTraits;
  101. #if __clang_major__ >= 16
  102. if (@available(iOS 17.0, *)) {
  103. expectedToggleTraits = @[@"ToggleButton", @"Button"];
  104. XCTAssertEqual(toggleTraits.count, 2, @"Toggle should have exactly 2 traits on iOS 17+");
  105. }
  106. #else
  107. expectedToggleTraits = @[@"Button"];
  108. XCTAssertEqual(toggleTraits.count, 1, @"Toggle should have exactly 1 trait on iOS < 17");
  109. #endif
  110. XCTAssertEqualObjects(toggleTraits, expectedToggleTraits);
  111. XCTAssertEqualObjects(toggle.wdType, @"XCUIElementTypeSwitch");
  112. XCUIElement *slider = self.testedApplication.sliders.firstMatch;
  113. XCTAssertTrue(slider.exists);
  114. NSArray *sliderTraits = [slider.wdTraits componentsSeparatedByString:@", "];
  115. NSArray *expectedSliderTraits = @[@"Adjustable"];
  116. XCTAssertEqual(sliderTraits.count, expectedSliderTraits.count, @"Slider should have exactly 1 trait");
  117. XCTAssertEqualObjects(sliderTraits, expectedSliderTraits);
  118. XCTAssertEqualObjects(slider.wdType, @"XCUIElementTypeSlider");
  119. XCUIElement *picker = self.testedApplication.pickerWheels.firstMatch;
  120. XCTAssertTrue(picker.exists);
  121. NSArray *pickerTraits = [picker.wdTraits componentsSeparatedByString:@", "];
  122. NSArray *expectedPickerTraits = @[@"Adjustable"];
  123. XCTAssertEqual(pickerTraits.count, expectedPickerTraits.count, @"Picker should have exactly 1 trait");
  124. XCTAssertEqualObjects(pickerTraits, expectedPickerTraits);
  125. XCTAssertEqualObjects(picker.wdType, @"XCUIElementTypePickerWheel");
  126. }
  127. - (void)testTextFieldAttributes
  128. {
  129. XCUIElement *element = self.testedApplication.textFields[@"Value"];
  130. XCTAssertTrue(element.exists);
  131. XCTAssertEqualObjects(element.wdType, @"XCUIElementTypeTextField");
  132. XCTAssertNil(element.wdName);
  133. XCTAssertEqualObjects(element.wdLabel, @"");
  134. XCTAssertEqualObjects(element.wdValue, @"Value");
  135. }
  136. - (void)testTextFieldWithAccessibilityIdentifiersAttributes
  137. {
  138. XCUIElement *element = self.testedApplication.textFields[@"aIdentifier"];
  139. XCTAssertTrue(element.exists);
  140. XCTAssertEqualObjects(element.wdType, @"XCUIElementTypeTextField");
  141. XCTAssertEqualObjects(element.wdName, @"aIdentifier");
  142. XCTAssertEqualObjects(element.wdLabel, @"aLabel");
  143. XCTAssertEqualObjects(element.wdValue, @"Value2");
  144. }
  145. - (void)testSegmentedControlAttributes
  146. {
  147. XCUIElement *element = self.testedApplication.segmentedControls.element;
  148. XCTAssertTrue(element.exists);
  149. XCTAssertEqualObjects(element.wdType, @"XCUIElementTypeSegmentedControl");
  150. XCTAssertNil(element.wdName);
  151. XCTAssertNil(element.wdLabel);
  152. XCTAssertNil(element.wdValue);
  153. }
  154. - (void)testSliderAttributes
  155. {
  156. XCUIElement *element = self.testedApplication.sliders.element;
  157. XCTAssertTrue(element.exists);
  158. XCTAssertEqualObjects(element.wdType, @"XCUIElementTypeSlider");
  159. XCTAssertNil(element.wdName);
  160. XCTAssertNil(element.wdLabel);
  161. XCTAssertTrue([element.wdValue containsString:@"50"]);
  162. NSNumber *minValue = element.wdMinValue;
  163. NSNumber *maxValue = element.wdMaxValue;
  164. XCTAssertNotNil(minValue, @"Slider minValue should not be nil");
  165. XCTAssertNotNil(maxValue, @"Slider maxValue should not be nil");
  166. XCTAssertEqualObjects(minValue, @0);
  167. XCTAssertEqualObjects(maxValue, @1);
  168. }
  169. - (void)testActivityIndicatorAttributes
  170. {
  171. XCUIElement *element = self.testedApplication.activityIndicators.element;
  172. XCTAssertTrue(element.exists);
  173. XCTAssertEqualObjects(element.wdType, @"XCUIElementTypeActivityIndicator");
  174. XCTAssertEqualObjects(element.wdName, @"Progress halted");
  175. XCTAssertEqualObjects(element.wdLabel, @"Progress halted");
  176. XCTAssertEqualObjects(element.wdValue, @"0");
  177. }
  178. - (void)testSwitchAttributes
  179. {
  180. XCUIElement *element = self.testedApplication.switches.element;
  181. XCTAssertTrue(element.exists);
  182. XCTAssertEqualObjects(element.wdType, @"XCUIElementTypeSwitch");
  183. XCTAssertNil(element.wdName);
  184. XCTAssertNil(element.wdLabel);
  185. XCTAssertNil(element.wdPlaceholderValue);
  186. XCTAssertEqualObjects(element.wdValue, @"1");
  187. XCTAssertFalse(element.wdSelected);
  188. XCTAssertEqual(element.wdHittable, element.hittable);
  189. [element tap];
  190. XCTAssertEqualObjects(element.wdValue, @"0");
  191. XCTAssertFalse(element.wdSelected);
  192. }
  193. - (void)testPickerWheelAttributes
  194. {
  195. XCUIElement *element = self.testedApplication.pickerWheels[@"Today"];
  196. XCTAssertTrue(element.exists);
  197. XCTAssertEqualObjects(element.wdType, @"XCUIElementTypePickerWheel");
  198. XCTAssertNil(element.wdName);
  199. XCTAssertNil(element.wdLabel);
  200. XCTAssertEqualObjects(element.wdValue, @"Today");
  201. }
  202. - (void)testPageIndicatorAttributes
  203. {
  204. XCUIElement *element = self.testedApplication.pageIndicators.element;
  205. XCTAssertTrue(element.exists);
  206. XCTAssertEqualObjects(element.wdType, @"XCUIElementTypePageIndicator");
  207. XCTAssertNil(element.wdName);
  208. XCTAssertNil(element.wdLabel);
  209. XCTAssertEqualObjects(element.wdValue, @"page 1 of 3");
  210. }
  211. - (void)testTextViewAttributes
  212. {
  213. XCUIElement *element = self.testedApplication.textViews.element;
  214. XCTAssertTrue(element.exists);
  215. XCTAssertEqualObjects(element.wdType, @"XCUIElementTypeTextView");
  216. XCTAssertNil(element.wdName);
  217. XCTAssertNil(element.wdLabel);
  218. XCTAssertEqualObjects(element.wdValue, @"1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901");
  219. }
  220. @end