FBXPathIntegrationTests.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "FBMacros.h"
  12. #import "FBTestMacros.h"
  13. #import "FBXPath.h"
  14. #import "FBXCodeCompatibility.h"
  15. #import "FBXCElementSnapshotWrapper+Helpers.h"
  16. #import "FBXMLGenerationOptions.h"
  17. #import "XCUIElement.h"
  18. #import "XCUIElement+FBFind.h"
  19. #import "XCUIElement+FBUtilities.h"
  20. #import "XCUIElement+FBWebDriverAttributes.h"
  21. @interface FBXPathIntegrationTests : FBIntegrationTestCase
  22. @property (nonatomic, strong) XCUIElement *testedView;
  23. @end
  24. @implementation FBXPathIntegrationTests
  25. - (void)setUp
  26. {
  27. [super setUp];
  28. static dispatch_once_t onceToken;
  29. dispatch_once(&onceToken, ^{
  30. [self launchApplication];
  31. });
  32. self.testedView = self.testedApplication.otherElements[@"MainView"];
  33. XCTAssertTrue(self.testedView.exists);
  34. FBAssertWaitTillBecomesTrue(self.testedView.buttons.count > 0);
  35. }
  36. - (id<FBXCElementSnapshot>)destinationSnapshot
  37. {
  38. XCUIElement *matchingElement = self.testedView.buttons.allElementsBoundByIndex.firstObject;
  39. id<FBXCElementSnapshot> snapshot = [matchingElement fb_customSnapshot];
  40. // Over iOS13, snapshot returns a child.
  41. // The purpose of here is return a single element to replace children with an empty array for testing.
  42. snapshot.children = @[];
  43. return snapshot;
  44. }
  45. - (void)testSingleDescendantXMLRepresentation
  46. {
  47. id<FBXCElementSnapshot> snapshot = self.destinationSnapshot;
  48. FBXCElementSnapshotWrapper *wrappedSnapshot = [FBXCElementSnapshotWrapper ensureWrapped:snapshot];
  49. NSString *xmlStr = [FBXPath xmlStringWithRootElement:wrappedSnapshot
  50. options:nil];
  51. XCTAssertNotNil(xmlStr);
  52. NSString *expectedXml = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<%@ type=\"%@\" name=\"%@\" label=\"%@\" enabled=\"%@\" visible=\"%@\" accessible=\"%@\" x=\"%@\" y=\"%@\" width=\"%@\" height=\"%@\" index=\"%lu\" traits=\"%@\"/>\n", wrappedSnapshot.wdType, wrappedSnapshot.wdType, wrappedSnapshot.wdName, wrappedSnapshot.wdLabel, FBBoolToString(wrappedSnapshot.wdEnabled), FBBoolToString(wrappedSnapshot.wdVisible), FBBoolToString(wrappedSnapshot.wdAccessible), [wrappedSnapshot.wdRect[@"x"] stringValue], [wrappedSnapshot.wdRect[@"y"] stringValue], [wrappedSnapshot.wdRect[@"width"] stringValue], [wrappedSnapshot.wdRect[@"height"] stringValue], wrappedSnapshot.wdIndex, wrappedSnapshot.wdTraits];
  53. XCTAssertEqualObjects(xmlStr, expectedXml);
  54. }
  55. - (void)testSingleDescendantXMLRepresentationWithScope
  56. {
  57. id<FBXCElementSnapshot> snapshot = self.destinationSnapshot;
  58. NSString *scope = @"AppiumAUT";
  59. FBXMLGenerationOptions *options = [[FBXMLGenerationOptions new] withScope:scope];
  60. FBXCElementSnapshotWrapper *wrappedSnapshot = [FBXCElementSnapshotWrapper ensureWrapped:snapshot];
  61. NSString *xmlStr = [FBXPath xmlStringWithRootElement:wrappedSnapshot
  62. options:options];
  63. XCTAssertNotNil(xmlStr);
  64. NSString *expectedXml = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<%@>\n <%@ type=\"%@\" name=\"%@\" label=\"%@\" enabled=\"%@\" visible=\"%@\" accessible=\"%@\" x=\"%@\" y=\"%@\" width=\"%@\" height=\"%@\" index=\"%lu\" traits=\"%@\"/>\n</%@>\n", scope, wrappedSnapshot.wdType, wrappedSnapshot.wdType, wrappedSnapshot.wdName, wrappedSnapshot.wdLabel, FBBoolToString(wrappedSnapshot.wdEnabled), FBBoolToString(wrappedSnapshot.wdVisible), FBBoolToString(wrappedSnapshot.wdAccessible), [wrappedSnapshot.wdRect[@"x"] stringValue], [wrappedSnapshot.wdRect[@"y"] stringValue], [wrappedSnapshot.wdRect[@"width"] stringValue], [wrappedSnapshot.wdRect[@"height"] stringValue], wrappedSnapshot.wdIndex, wrappedSnapshot.wdTraits, scope];
  65. XCTAssertEqualObjects(xmlStr, expectedXml);
  66. }
  67. - (void)testSingleDescendantXMLRepresentationWithoutAttributes
  68. {
  69. id<FBXCElementSnapshot> snapshot = self.destinationSnapshot;
  70. FBXMLGenerationOptions *options = [[FBXMLGenerationOptions new]
  71. withExcludedAttributes:@[@"visible", @"enabled", @"index", @"blabla"]];
  72. FBXCElementSnapshotWrapper *wrappedSnapshot = [FBXCElementSnapshotWrapper ensureWrapped:snapshot];
  73. NSString *xmlStr = [FBXPath xmlStringWithRootElement:wrappedSnapshot
  74. options:options];
  75. XCTAssertNotNil(xmlStr);
  76. NSString *expectedXml = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<%@ type=\"%@\" name=\"%@\" label=\"%@\" accessible=\"%@\" x=\"%@\" y=\"%@\" width=\"%@\" height=\"%@\" traits=\"%@\"/>\n", wrappedSnapshot.wdType, wrappedSnapshot.wdType, wrappedSnapshot.wdName, wrappedSnapshot.wdLabel, FBBoolToString(wrappedSnapshot.wdAccessible), [wrappedSnapshot.wdRect[@"x"] stringValue], [wrappedSnapshot.wdRect[@"y"] stringValue], [wrappedSnapshot.wdRect[@"width"] stringValue], [wrappedSnapshot.wdRect[@"height"] stringValue], wrappedSnapshot.wdTraits];
  77. XCTAssertEqualObjects(xmlStr, expectedXml);
  78. }
  79. - (void)testFindMatchesInElement
  80. {
  81. NSArray<id<FBXCElementSnapshot>> *matchingSnapshots = [FBXPath matchesWithRootElement:self.testedApplication forQuery:@"//XCUIElementTypeButton"];
  82. XCTAssertEqual([matchingSnapshots count], 5);
  83. for (id<FBXCElementSnapshot> element in matchingSnapshots) {
  84. XCTAssertTrue([[FBXCElementSnapshotWrapper ensureWrapped:element].wdType isEqualToString:@"XCUIElementTypeButton"]);
  85. }
  86. }
  87. - (void)testFindMatchesWithoutContextScopeLimit
  88. {
  89. XCUIElement *button = self.testedApplication.buttons.firstMatch;
  90. BOOL previousValue = FBConfiguration.limitXpathContextScope;
  91. FBConfiguration.limitXpathContextScope = NO;
  92. @try {
  93. NSArray *parentSnapshots = [FBXPath matchesWithRootElement:button forQuery:@".."];
  94. XCTAssertEqual(parentSnapshots.count, 1);
  95. XCTAssertEqualObjects(
  96. [FBXCElementSnapshotWrapper ensureWrapped:[parentSnapshots objectAtIndex:0]].wdLabel,
  97. @"MainView"
  98. );
  99. NSArray *elements = [button.application fb_filterDescendantsWithSnapshots:parentSnapshots onlyChildren:NO];
  100. XCTAssertEqual(elements.count, 1);
  101. XCTAssertEqualObjects(
  102. [[elements objectAtIndex:0] wdLabel],
  103. @"MainView"
  104. );
  105. NSArray *currentSnapshots = [FBXPath matchesWithRootElement:button forQuery:@"."];
  106. XCTAssertEqual(currentSnapshots.count, 1);
  107. XCTAssertEqualObjects(
  108. [FBXCElementSnapshotWrapper ensureWrapped:[currentSnapshots objectAtIndex:0]].wdType,
  109. @"XCUIElementTypeButton"
  110. );
  111. NSArray *currentElements = [button.application fb_filterDescendantsWithSnapshots:currentSnapshots onlyChildren:NO];
  112. XCTAssertEqual(currentElements.count, 1);
  113. XCTAssertEqualObjects(
  114. [[currentElements objectAtIndex:0] wdType],
  115. @"XCUIElementTypeButton"
  116. );
  117. } @finally {
  118. FBConfiguration.limitXpathContextScope = previousValue;
  119. }
  120. }
  121. - (void)testFindMatchesInElementWithDotNotation
  122. {
  123. NSArray<id<FBXCElementSnapshot>> *matchingSnapshots = [FBXPath matchesWithRootElement:self.testedApplication forQuery:@".//XCUIElementTypeButton"];
  124. XCTAssertEqual([matchingSnapshots count], 5);
  125. for (id<FBXCElementSnapshot> element in matchingSnapshots) {
  126. XCTAssertTrue([[FBXCElementSnapshotWrapper ensureWrapped:element].wdType isEqualToString:@"XCUIElementTypeButton"]);
  127. }
  128. }
  129. @end