XCUIElement+FBUID.m 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_standardSnapshot].accessibilityElement)];
  21. }
  22. - (NSString *)fb_uid
  23. {
  24. return [self isKindOfClass:XCUIApplication.class]
  25. ? [FBElementUtils uidWithAccessibilityElement:[(XCUIApplication *)self accessibilityElement]]
  26. : [FBXCElementSnapshotWrapper ensureWrapped:[self fb_standardSnapshot]].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. #pragma clang diagnostic ignored "-Wcast-function-type-strict"
  36. + (void)load
  37. {
  38. Class XCElementSnapshotCls = objc_lookUpClass("XCElementSnapshot");
  39. NSAssert(XCElementSnapshotCls != nil, @"Could not locate XCElementSnapshot class");
  40. Method uidMethod = class_getInstanceMethod(self.class, @selector(fb_uid));
  41. class_addMethod(XCElementSnapshotCls, @selector(fb_uid), method_getImplementation(uidMethod), method_getTypeEncoding(uidMethod));
  42. // Support for Xcode 14.3 requires disabling the new predicate validator, see https://github.com/appium/appium/issues/18444
  43. Class XCTElementQueryTransformerPredicateValidatorCls = objc_lookUpClass("XCTElementQueryTransformerPredicateValidator");
  44. if (XCTElementQueryTransformerPredicateValidatorCls == nil) {
  45. return;
  46. }
  47. Method validatePredicateMethod = class_getClassMethod(XCTElementQueryTransformerPredicateValidatorCls, NSSelectorFromString(@"validatePredicate:withExpressionsAllowed:"));
  48. if (validatePredicateMethod == nil) {
  49. [FBLogger log:@"Could not find method +[XCTElementQueryTransformerPredicateValidator validatePredicate:withExpressionsAllowed:]"];
  50. return;
  51. }
  52. IMP swizzledImp = (IMP)swizzled_validatePredicateWithExpressionsAllowed;
  53. method_setImplementation(validatePredicateMethod, swizzledImp);
  54. }
  55. #pragma diagnostic pop
  56. - (unsigned long long)fb_accessibiltyId
  57. {
  58. return [FBElementUtils idWithAccessibilityElement:self.accessibilityElement];
  59. }
  60. + (nullable NSString *)wdUIDWithSnapshot:(id<FBXCElementSnapshot>)snapshot
  61. {
  62. return [FBElementUtils uidWithAccessibilityElement:[snapshot accessibilityElement]];
  63. }
  64. - (NSString *)fb_uid
  65. {
  66. if ([self isKindOfClass:FBXCElementSnapshotWrapper.class]) {
  67. return [self.class wdUIDWithSnapshot:self.snapshot];
  68. }
  69. return [FBElementUtils uidWithAccessibilityElement:[self accessibilityElement]];
  70. }
  71. @end