XCUIElement+FBUID.m 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 <objc/runtime.h>
  10. #import "XCUIElement+FBUID.h"
  11. #import "FBElementUtils.h"
  12. #import "FBLogger.h"
  13. #import "XCUIApplication.h"
  14. #import "XCUIElement+FBUtilities.h"
  15. @implementation XCUIElement (FBUID)
  16. - (unsigned long long)fb_accessibiltyId
  17. {
  18. return [FBElementUtils idWithAccessibilityElement:([self isKindOfClass:XCUIApplication.class]
  19. ? [(XCUIApplication *)self accessibilityElement]
  20. : [self fb_takeSnapshot].accessibilityElement)];
  21. }
  22. - (NSString *)fb_uid
  23. {
  24. return [self isKindOfClass:XCUIApplication.class]
  25. ? [FBElementUtils uidWithAccessibilityElement:[(XCUIApplication *)self accessibilityElement]]
  26. : [FBXCElementSnapshotWrapper ensureWrapped:[self fb_takeSnapshot]].fb_uid;
  27. }
  28. @end
  29. @implementation FBXCElementSnapshotWrapper (FBUID)
  30. static void swizzled_validatePredicateWithExpressionsAllowed(id self, SEL _cmd, id predicate, BOOL withExpressionsAllowed)
  31. {
  32. }
  33. #pragma clang diagnostic push
  34. #pragma clang diagnostic ignored "-Wobjc-load-method"
  35. + (void)load
  36. {
  37. Class XCElementSnapshotCls = objc_lookUpClass("XCElementSnapshot");
  38. NSAssert(XCElementSnapshotCls != nil, @"Could not locate XCElementSnapshot class");
  39. Method uidMethod = class_getInstanceMethod(self.class, @selector(fb_uid));
  40. class_addMethod(XCElementSnapshotCls, @selector(fb_uid), method_getImplementation(uidMethod), method_getTypeEncoding(uidMethod));
  41. // Support for Xcode 14.3 requires disabling the new predicate validator, see https://github.com/appium/appium/issues/18444
  42. Class XCTElementQueryTransformerPredicateValidatorCls = objc_lookUpClass("XCTElementQueryTransformerPredicateValidator");
  43. if (XCTElementQueryTransformerPredicateValidatorCls == nil) {
  44. return;
  45. }
  46. Method validatePredicateMethod = class_getClassMethod(XCTElementQueryTransformerPredicateValidatorCls, NSSelectorFromString(@"validatePredicate:withExpressionsAllowed:"));
  47. if (validatePredicateMethod == nil) {
  48. [FBLogger log:@"Could not find method +[XCTElementQueryTransformerPredicateValidator validatePredicate:withExpressionsAllowed:]"];
  49. return;
  50. }
  51. IMP swizzledImp = (IMP)swizzled_validatePredicateWithExpressionsAllowed;
  52. method_setImplementation(validatePredicateMethod, swizzledImp);
  53. }
  54. #pragma diagnostic pop
  55. - (unsigned long long)fb_accessibiltyId
  56. {
  57. return [FBElementUtils idWithAccessibilityElement:self.accessibilityElement];
  58. }
  59. + (nullable NSString *)wdUIDWithSnapshot:(id<FBXCElementSnapshot>)snapshot
  60. {
  61. return [FBElementUtils uidWithAccessibilityElement:[snapshot accessibilityElement]];
  62. }
  63. - (NSString *)fb_uid
  64. {
  65. if ([self isKindOfClass:FBXCElementSnapshotWrapper.class]) {
  66. return [self.class wdUIDWithSnapshot:self.snapshot];
  67. }
  68. return [FBElementUtils uidWithAccessibilityElement:[self accessibilityElement]];
  69. }
  70. @end