FBXPathIntegrationTests.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. FBAssertWaitTillBecomesTrue(nil != matchingElement.fb_takeSnapshot);
  40. id<FBXCElementSnapshot> snapshot = matchingElement.fb_takeSnapshot;
  41. // Over iOS13, snapshot returns a child.
  42. // The purpose of here is return a single element to replace children with an empty array for testing.
  43. snapshot.children = @[];
  44. return snapshot;
  45. }
  46. - (void)testSingleDescendantXMLRepresentation
  47. {
  48. id<FBXCElementSnapshot> snapshot = self.destinationSnapshot;
  49. FBXCElementSnapshotWrapper *wrappedSnapshot = [FBXCElementSnapshotWrapper ensureWrapped:snapshot];
  50. NSString *xmlStr = [FBXPath xmlStringWithRootElement:wrappedSnapshot
  51. options:nil];
  52. XCTAssertNotNil(xmlStr);
  53. NSString *expectedXml = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<%@ type=\"%@\" name=\"%@\" label=\"%@\" enabled=\"%@\" visible=\"%@\" accessible=\"%@\" x=\"%@\" y=\"%@\" width=\"%@\" height=\"%@\" index=\"%lu\"/>\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];
  54. XCTAssertEqualObjects(xmlStr, expectedXml);
  55. }
  56. - (void)testSingleDescendantXMLRepresentationWithScope
  57. {
  58. id<FBXCElementSnapshot> snapshot = self.destinationSnapshot;
  59. NSString *scope = @"AppiumAUT";
  60. FBXMLGenerationOptions *options = [[FBXMLGenerationOptions new] withScope:scope];
  61. FBXCElementSnapshotWrapper *wrappedSnapshot = [FBXCElementSnapshotWrapper ensureWrapped:snapshot];
  62. NSString *xmlStr = [FBXPath xmlStringWithRootElement:wrappedSnapshot
  63. options:options];
  64. XCTAssertNotNil(xmlStr);
  65. 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\"/>\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, scope];
  66. XCTAssertEqualObjects(xmlStr, expectedXml);
  67. }
  68. - (void)testSingleDescendantXMLRepresentationWithoutAttributes
  69. {
  70. id<FBXCElementSnapshot> snapshot = self.destinationSnapshot;
  71. FBXMLGenerationOptions *options = [[FBXMLGenerationOptions new]
  72. withExcludedAttributes:@[@"visible", @"enabled", @"index", @"blabla"]];
  73. FBXCElementSnapshotWrapper *wrappedSnapshot = [FBXCElementSnapshotWrapper ensureWrapped:snapshot];
  74. NSString *xmlStr = [FBXPath xmlStringWithRootElement:wrappedSnapshot
  75. options:options];
  76. XCTAssertNotNil(xmlStr);
  77. NSString *expectedXml = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<%@ type=\"%@\" name=\"%@\" label=\"%@\" accessible=\"%@\" x=\"%@\" y=\"%@\" width=\"%@\" height=\"%@\"/>\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]];
  78. XCTAssertEqualObjects(xmlStr, expectedXml);
  79. }
  80. - (void)testFindMatchesInElement
  81. {
  82. NSArray<id<FBXCElementSnapshot>> *matchingSnapshots = [FBXPath matchesWithRootElement:self.testedApplication forQuery:@"//XCUIElementTypeButton"];
  83. XCTAssertEqual([matchingSnapshots count], 5);
  84. for (id<FBXCElementSnapshot> element in matchingSnapshots) {
  85. XCTAssertTrue([[FBXCElementSnapshotWrapper ensureWrapped:element].wdType isEqualToString:@"XCUIElementTypeButton"]);
  86. }
  87. }
  88. - (void)testFindMatchesInElementWithDotNotation
  89. {
  90. NSArray<id<FBXCElementSnapshot>> *matchingSnapshots = [FBXPath matchesWithRootElement:self.testedApplication forQuery:@".//XCUIElementTypeButton"];
  91. XCTAssertEqual([matchingSnapshots count], 5);
  92. for (id<FBXCElementSnapshot> element in matchingSnapshots) {
  93. XCTAssertTrue([[FBXCElementSnapshotWrapper ensureWrapped:element].wdType isEqualToString:@"XCUIElementTypeButton"]);
  94. }
  95. }
  96. @end