XCUIElementAttributesTests.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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+FBWebDriverAttributes.h"
  13. #import "XCUIElement+FBFind.h"
  14. #import "FBElementUtils.h"
  15. #import "FBConfiguration.h"
  16. #import "FBResponsePayload.h"
  17. #import "FBXCodeCompatibility.h"
  18. @interface XCUIElementAttributesTests : FBIntegrationTestCase
  19. @property (nonatomic, strong) XCUIElement *matchingElement;
  20. @end
  21. @implementation XCUIElementAttributesTests
  22. - (void)setUp
  23. {
  24. [super setUp];
  25. static dispatch_once_t onceToken;
  26. dispatch_once(&onceToken, ^{
  27. [self launchApplication];
  28. });
  29. XCUIElement *testedView = self.testedApplication.otherElements[@"MainView"];
  30. FBAssertWaitTillBecomesTrue(testedView.exists);
  31. self.matchingElement = [[testedView fb_descendantsMatchingIdentifier:@"Alerts" shouldReturnAfterFirstMatch:YES] firstObject];
  32. XCTAssertNotNil(self.matchingElement);
  33. }
  34. - (void)verifyGettingAttributeWithShortcut:(NSString *)shortcutName expectedValue:(id)expectedValue
  35. {
  36. NSString *fullAttributeName = [NSString stringWithFormat:@"wd%@", [NSString stringWithFormat:@"%@%@", [[shortcutName substringToIndex:1] uppercaseString], [shortcutName substringFromIndex:1]]];
  37. id actualValue = [self.matchingElement fb_valueForWDAttributeName:fullAttributeName];
  38. id actualShortcutValue = [self.matchingElement fb_valueForWDAttributeName:shortcutName];
  39. if (nil == expectedValue) {
  40. XCTAssertNil(actualValue);
  41. XCTAssertNil(actualShortcutValue);
  42. return;
  43. }
  44. if ([actualValue isKindOfClass:NSString.class]) {
  45. XCTAssertTrue([actualValue isEqualToString:expectedValue]);
  46. XCTAssertTrue([actualShortcutValue isEqualToString:expectedValue]);
  47. } else if ([actualValue isKindOfClass:NSNumber.class]) {
  48. XCTAssertTrue([actualValue isEqualToNumber:expectedValue]);
  49. XCTAssertTrue([actualShortcutValue isEqualToNumber:expectedValue]);
  50. } else {
  51. XCTAssertEqual(actualValue, expectedValue);
  52. XCTAssertEqual(actualShortcutValue, expectedValue);
  53. }
  54. }
  55. - (void)testGetNameAttribute
  56. {
  57. [self verifyGettingAttributeWithShortcut:@"name" expectedValue:self.matchingElement.wdName];
  58. }
  59. - (void)testGetValueAttribute
  60. {
  61. [self verifyGettingAttributeWithShortcut:@"value" expectedValue:self.matchingElement.wdValue];
  62. }
  63. - (void)testGetLabelAttribute
  64. {
  65. [self verifyGettingAttributeWithShortcut:@"label" expectedValue:self.matchingElement.wdLabel];
  66. }
  67. - (void)testGetTypeAttribute
  68. {
  69. [self verifyGettingAttributeWithShortcut:@"type" expectedValue:self.matchingElement.wdType];
  70. }
  71. - (void)testGetRectAttribute
  72. {
  73. NSString *shortcutName = @"rect";
  74. for (NSString *key in @[@"x", @"y", @"width", @"height"]) {
  75. NSNumber *actualValue = [self.matchingElement fb_valueForWDAttributeName:[FBElementUtils wdAttributeNameForAttributeName:shortcutName]][key];
  76. NSNumber *actualShortcutValue = [self.matchingElement fb_valueForWDAttributeName:shortcutName][key];
  77. NSNumber *expectedValue = self.matchingElement.wdRect[key];
  78. XCTAssertTrue([actualValue isEqualToNumber:expectedValue]);
  79. XCTAssertTrue([actualShortcutValue isEqualToNumber:expectedValue]);
  80. }
  81. }
  82. - (void)testGetEnabledAttribute
  83. {
  84. [self verifyGettingAttributeWithShortcut:@"enabled" expectedValue:[NSNumber numberWithBool:self.matchingElement.wdEnabled]];
  85. }
  86. - (void)testGetAccessibleAttribute
  87. {
  88. [self verifyGettingAttributeWithShortcut:@"accessible" expectedValue:[NSNumber numberWithBool:self.matchingElement.wdAccessible]];
  89. }
  90. - (void)testGetUidAttribute
  91. {
  92. [self verifyGettingAttributeWithShortcut:@"UID" expectedValue:self.matchingElement.wdUID];
  93. }
  94. - (void)testGetVisibleAttribute
  95. {
  96. [self verifyGettingAttributeWithShortcut:@"visible" expectedValue:[NSNumber numberWithBool:self.matchingElement.wdVisible]];
  97. }
  98. - (void)testGetAccessibilityContainerAttribute
  99. {
  100. [self verifyGettingAttributeWithShortcut:@"accessibilityContainer" expectedValue:[NSNumber numberWithBool:self.matchingElement.wdAccessibilityContainer]];
  101. }
  102. - (void)testGetInvalidAttribute
  103. {
  104. XCTAssertThrowsSpecificNamed([self verifyGettingAttributeWithShortcut:@"invalid" expectedValue:@"blabla"], NSException, FBUnknownAttributeException);
  105. }
  106. @end
  107. @interface XCUIElementFBFindTests_CompactResponses : FBIntegrationTestCase
  108. @end
  109. @implementation XCUIElementFBFindTests_CompactResponses
  110. - (void)setUp
  111. {
  112. [super setUp];
  113. static dispatch_once_t onceToken;
  114. dispatch_once(&onceToken, ^{
  115. [self launchApplication];
  116. });
  117. }
  118. - (void)testCompactResponseYes
  119. {
  120. XCUIElement *alertsButton = self.testedApplication.buttons[@"Alerts"];
  121. NSDictionary *fields = FBDictionaryResponseWithElement(alertsButton, YES);
  122. XCTAssertNotNil(fields[@"ELEMENT"]);
  123. XCTAssertNotNil(fields[@"element-6066-11e4-a52e-4f735466cecf"]);
  124. XCTAssertEqual(fields.count, 2);
  125. }
  126. - (void)testCompactResponseNo
  127. {
  128. XCUIElement *alertsButton = self.testedApplication.buttons[@"Alerts"];
  129. NSDictionary *fields = FBDictionaryResponseWithElement(alertsButton, NO);
  130. XCTAssertNotNil(fields[@"ELEMENT"]);
  131. XCTAssertNotNil(fields[@"element-6066-11e4-a52e-4f735466cecf"]);
  132. XCTAssertEqualObjects(fields[@"type"], @"XCUIElementTypeButton");
  133. XCTAssertEqualObjects(fields[@"label"], @"Alerts");
  134. XCTAssertEqual(fields.count, 4);
  135. }
  136. @end
  137. @interface XCUIElementFBFindTests_ResponseFields : FBIntegrationTestCase
  138. @end
  139. @implementation XCUIElementFBFindTests_ResponseFields
  140. - (void)setUp
  141. {
  142. [super setUp];
  143. static dispatch_once_t onceToken;
  144. dispatch_once(&onceToken, ^{
  145. [self launchApplication];
  146. });
  147. }
  148. - (void)testCompactResponseYesWithResponseAttributesSet
  149. {
  150. [FBConfiguration setElementResponseAttributes:@"name,text,enabled"];
  151. XCUIElement *alertsButton = self.testedApplication.buttons[@"Alerts"];
  152. NSDictionary *fields = FBDictionaryResponseWithElement(alertsButton, YES);
  153. XCTAssertNotNil(fields[@"ELEMENT"]);
  154. XCTAssertNotNil(fields[@"element-6066-11e4-a52e-4f735466cecf"]);
  155. XCTAssertEqual(fields.count, 2);
  156. }
  157. - (void)testCompactResponseNoWithResponseAttributesSet
  158. {
  159. [FBConfiguration setElementResponseAttributes:@"name,text,enabled"];
  160. XCUIElement *alertsButton = self.testedApplication.buttons[@"Alerts"];
  161. NSDictionary *fields = FBDictionaryResponseWithElement(alertsButton, NO);
  162. XCTAssertNotNil(fields[@"ELEMENT"]);
  163. XCTAssertNotNil(fields[@"element-6066-11e4-a52e-4f735466cecf"]);
  164. XCTAssertEqualObjects(fields[@"name"], @"XCUIElementTypeButton");
  165. XCTAssertEqualObjects(fields[@"text"], @"Alerts");
  166. XCTAssertEqualObjects(fields[@"enabled"], @(YES));
  167. XCTAssertEqual(fields.count, 5);
  168. }
  169. - (void)testInvalidAttribute
  170. {
  171. [FBConfiguration setElementResponseAttributes:@"invalid_field,name"];
  172. XCUIElement *alertsButton = self.testedApplication.buttons[@"Alerts"];
  173. NSDictionary *fields = FBDictionaryResponseWithElement(alertsButton, NO);
  174. XCTAssertNotNil(fields[@"ELEMENT"]);
  175. XCTAssertNotNil(fields[@"element-6066-11e4-a52e-4f735466cecf"]);
  176. XCTAssertEqualObjects(fields[@"name"], @"XCUIElementTypeButton");
  177. XCTAssertEqual(fields.count, 3);
  178. }
  179. - (void)testKnownAttributes
  180. {
  181. [FBConfiguration setElementResponseAttributes:@"name,type,label,text,rect,enabled,displayed,selected"];
  182. XCUIElement *alertsButton = self.testedApplication.buttons[@"Alerts"];
  183. NSDictionary *fields = FBDictionaryResponseWithElement(alertsButton, NO);
  184. XCTAssertNotNil(fields[@"ELEMENT"]);
  185. XCTAssertNotNil(fields[@"element-6066-11e4-a52e-4f735466cecf"]);
  186. XCTAssertEqualObjects(fields[@"name"], @"XCUIElementTypeButton");
  187. XCTAssertEqualObjects(fields[@"type"], @"XCUIElementTypeButton");
  188. XCTAssertEqualObjects(fields[@"label"], @"Alerts");
  189. XCTAssertEqualObjects(fields[@"text"], @"Alerts");
  190. XCTAssertTrue(matchesRegex([fields[@"rect"] description], @"\\{\\s*height = [0-9]+;\\s*width = [0-9]+;\\s*x = [0-9]+;\\s*y = [0-9]+;\\s*\\}"));
  191. XCTAssertEqualObjects(fields[@"enabled"], @(YES));
  192. XCTAssertEqualObjects(fields[@"displayed"], @(YES));
  193. XCTAssertEqualObjects(fields[@"selected"], @(NO));
  194. XCTAssertEqual(fields.count, 10);
  195. }
  196. - (void)testArbitraryAttributes
  197. {
  198. [FBConfiguration setElementResponseAttributes:@"attribute/name,attribute/value"];
  199. XCUIElement *alertsButton = self.testedApplication.buttons[@"Alerts"];
  200. NSDictionary *fields = FBDictionaryResponseWithElement(alertsButton, NO);
  201. XCTAssertNotNil(fields[@"ELEMENT"]);
  202. XCTAssertNotNil(fields[@"element-6066-11e4-a52e-4f735466cecf"]);
  203. XCTAssertEqualObjects(fields[@"attribute/name"], @"Alerts");
  204. XCTAssertEqualObjects(fields[@"attribute/value"], [NSNull null]);
  205. XCTAssertEqual(fields.count, 4);
  206. }
  207. static BOOL matchesRegex(NSString *target, NSString *pattern) {
  208. if (!target)
  209. return NO;
  210. NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:NULL];
  211. return [regex numberOfMatchesInString:target options:0 range:NSMakeRange(0, target.length)] == 1;
  212. }
  213. @end