appium-webdriveragent 16.18.2 → 16.18.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,20 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [16.18.4](https://github.com/appium/appium-ios/compare/appium-webdriveragent@16.18.3...appium-webdriveragent@16.18.4) (2026-11-09)
7
+
8
+ ### Performance Improvements
9
+
10
+ * **webdriveragent:** stop resolving every match again in findElements ([#177](https://github.com/appium/appium-ios/issues/177)) ([ea8354c](https://github.com/appium/appium-ios/commit/ea8354caf2a842a19a82114b8103d7c9446a4e09))
11
+
12
+
13
+ ## [16.18.3](https://github.com/appium/appium-ios/compare/appium-webdriveragent@16.18.2...appium-webdriveragent@16.18.3) (2026-11-08)
14
+
15
+ ### Bug Fixes
16
+
17
+ * **webdriveragent:** cache the screen lookup in the MJPEG server ([#171](https://github.com/appium/appium-ios/issues/171)) ([e764254](https://github.com/appium/appium-ios/commit/e764254f068127669c1ba16d32394836c97307ac))
18
+
19
+
6
20
  ## [16.18.2](https://github.com/appium/appium-ios/compare/appium-webdriveragent@16.18.1...appium-webdriveragent@16.18.2) (2026-11-07)
7
21
 
8
22
  ### Code Refactoring
@@ -7,6 +7,7 @@
7
7
  */
8
8
 
9
9
  #import <XCTest/XCTest.h>
10
+ #import "FBXCElementSnapshot.h"
10
11
 
11
12
  NS_ASSUME_NONNULL_BEGIN
12
13
 
@@ -14,6 +15,13 @@ NS_ASSUME_NONNULL_BEGIN
14
15
 
15
16
  @property (nonatomic, readonly) NSString *fb_cacheId;
16
17
 
18
+ /**
19
+ Seeds the cache ID and response snapshot from a freshly executed lookup.
20
+ Only call with the snapshot that was used to bind this element. Response
21
+ serialization clears lastSnapshot; subsequent commands resolve the live element.
22
+ */
23
+ - (void)fb_cacheSnapshot:(id<FBXCElementSnapshot>)snapshot;
24
+
17
25
  @end
18
26
 
19
27
  NS_ASSUME_NONNULL_END
@@ -7,6 +7,7 @@
7
7
  */
8
8
 
9
9
  #import "XCUIElement+FBCaching.h"
10
+ #import "XCUIElement.h"
10
11
 
11
12
  #import <objc/runtime.h>
12
13
 
@@ -33,4 +34,13 @@ static char XCUIELEMENT_CACHE_ID_KEY;
33
34
  return uid;
34
35
  }
35
36
 
37
+ - (void)fb_cacheSnapshot:(id<FBXCElementSnapshot>)snapshot
38
+ {
39
+ NSString *uid = [FBXCElementSnapshotWrapper wdUIDWithSnapshot:snapshot];
40
+ if (nil != uid) {
41
+ objc_setAssociatedObject(self, &XCUIELEMENT_CACHE_ID_KEY, uid, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
42
+ self.lastSnapshot = snapshot;
43
+ }
44
+ }
45
+
36
46
  @end
@@ -38,7 +38,10 @@
38
38
 
39
39
  - (id<FBXCElementSnapshot>)fb_cachedSnapshotWithQuery:(XCUIElementQuery *)query
40
40
  {
41
- return [self isKindOfClass:XCUIApplication.class] ? query.rootElementSnapshot : self.fb_cachedSnapshot;
41
+ // Elements found without snapshots (see fb_boundAccessibilityElement) are resolved lazily here
42
+ return [self isKindOfClass:XCUIApplication.class]
43
+ ? query.rootElementSnapshot
44
+ : (self.fb_cachedSnapshot ?: [self fb_standardSnapshot]);
42
45
  }
43
46
 
44
47
  #pragma mark - Search by ClassName
@@ -7,11 +7,15 @@
7
7
  */
8
8
 
9
9
  #import "FBXCElementSnapshotWrapper.h"
10
+ #import "FBXCAccessibilityElement.h"
10
11
 
11
12
  NS_ASSUME_NONNULL_BEGIN
12
13
 
13
14
  @interface XCUIElement (FBUID)
14
15
 
16
+ /*! AX element this element is bound to (see allElementsBoundByAccessibilityElement) or nil. Does not take a snapshot. */
17
+ @property (nonatomic, nullable, readonly) id<FBXCAccessibilityElement> fb_boundAccessibilityElement;
18
+
15
19
  /*! Represents unique internal element identifier, which is the same for an element and its snapshot as UUIDv4 */
16
20
  @property (nonatomic, nullable, readonly, copy) NSString *fb_uid;
17
21
 
@@ -14,6 +14,7 @@
14
14
  #import "FBLogger.h"
15
15
  #import "XCUIApplication.h"
16
16
  #import "XCUIElement+FBUtilities.h"
17
+ #import "XCUIElementQuery.h"
17
18
 
18
19
  @implementation XCUIElement (FBUID)
19
20
 
@@ -24,11 +25,32 @@
24
25
  : [self fb_standardSnapshot].accessibilityElement)];
25
26
  }
26
27
 
28
+ - (id<FBXCAccessibilityElement>)fb_boundAccessibilityElement
29
+ {
30
+ // Elements from -allElementsBoundByAccessibilityElement carry `accessibilityElement == <AX element>`
31
+ id transformer = self.query.transformer;
32
+ NSPredicate *predicate = [transformer respondsToSelector:@selector(predicate)] ? [transformer predicate] : nil;
33
+ if (![predicate isKindOfClass:NSComparisonPredicate.class]) {
34
+ return nil;
35
+ }
36
+ NSComparisonPredicate *comparison = (NSComparisonPredicate *)predicate;
37
+ if (comparison.leftExpression.expressionType != NSKeyPathExpressionType
38
+ || ![comparison.leftExpression.keyPath isEqualToString:@"accessibilityElement"]
39
+ || comparison.rightExpression.expressionType != NSConstantValueExpressionType) {
40
+ return nil;
41
+ }
42
+ id value = comparison.rightExpression.constantValue;
43
+ return [value respondsToSelector:@selector(processIdentifier)] ? value : nil;
44
+ }
45
+
27
46
  - (NSString *)fb_uid
28
47
  {
29
- return [self isKindOfClass:XCUIApplication.class]
30
- ? [FBElementUtils uidWithAccessibilityElement:[(XCUIApplication *)self accessibilityElement]]
31
- : [FBXCElementSnapshotWrapper ensureWrapped:[self fb_standardSnapshot]].fb_uid;
48
+ if ([self isKindOfClass:XCUIApplication.class]) {
49
+ return [FBElementUtils uidWithAccessibilityElement:[(XCUIApplication *)self accessibilityElement]];
50
+ }
51
+ id<FBXCAccessibilityElement> boundElement = self.fb_boundAccessibilityElement;
52
+ NSString *boundUid = nil == boundElement ? nil : [FBElementUtils uidWithAccessibilityElement:boundElement];
53
+ return boundUid ?: [FBXCElementSnapshotWrapper ensureWrapped:[self fb_standardSnapshot]].fb_uid;
32
54
  }
33
55
 
34
56
  @end
@@ -41,6 +41,50 @@
41
41
  #import "XCUIScreen.h"
42
42
  #import "XCUIElement+FBResolve.h"
43
43
 
44
+ // Binds every snapshot to its AX element directly, without a query over the whole tree.
45
+ // Returns nil if that is not possible, so the caller can fall back to FBQueryElementsWithUids.
46
+ static NSArray<XCUIElement *> *FBBindSnapshotsToElements(XCUIElementQuery *scopeQuery,
47
+ NSArray<id<FBXCElementSnapshot>> *snapshots,
48
+ NSArray<NSString *> *uids,
49
+ NSString *excludedUid)
50
+ {
51
+ XCUIElementQuery *anyQuery = [scopeQuery descendantsMatchingType:XCUIElementTypeAny];
52
+ if (FBConfiguration.sharedInstance.boundElementsByIndex
53
+ || ![anyQuery respondsToSelector:@selector(_elementMatchingAccessibilityElementOfSnapshot:)]) {
54
+ return nil;
55
+ }
56
+ NSMutableArray<XCUIElement *> *result = [NSMutableArray array];
57
+ for (NSUInteger i = 0; i < snapshots.count; ++i) {
58
+ if ([uids[i] isEqualToString:excludedUid]) {
59
+ continue;
60
+ }
61
+ XCUIElement *el = [anyQuery _elementMatchingAccessibilityElementOfSnapshot:snapshots[i]];
62
+ if (nil == el.fb_boundAccessibilityElement) {
63
+ return nil;
64
+ }
65
+ [result addObject:el];
66
+ }
67
+ return result.copy;
68
+ }
69
+
70
+ // Resolves the elements by their uids with a query over the scope's tree
71
+ static NSArray<XCUIElement *> *FBQueryElementsWithUids(XCUIElementQuery *scopeQuery,
72
+ NSArray<id<FBXCElementSnapshot>> *snapshots,
73
+ NSArray<NSString *> *uids,
74
+ BOOL onlyChildren)
75
+ {
76
+ XCUIElementType type = XCUIElementTypeAny;
77
+ NSArray<NSNumber *> *uniqueTypes = [snapshots valueForKeyPath:[NSString stringWithFormat:@"@distinctUnionOfObjects.%@", FBStringify(XCUIElement, elementType)]];
78
+ if (uniqueTypes && [uniqueTypes count] == 1) {
79
+ type = (XCUIElementType)[uniqueTypes.firstObject intValue];
80
+ }
81
+ XCUIElementQuery *query = onlyChildren
82
+ ? [scopeQuery childrenMatchingType:type]
83
+ : [scopeQuery descendantsMatchingType:type];
84
+ NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K IN %@", FBStringify(FBXCElementSnapshotWrapper, fb_uid), uids];
85
+ return [[query matchingPredicate:predicate] fb_allMatchesWithSnapshotsBoundByIndex:YES];
86
+ }
87
+
44
88
  @implementation XCUIElement (FBUtilities)
45
89
 
46
90
  - (id<FBXCElementSnapshot>)fb_takeSnapshot:(BOOL)isCustom
@@ -87,47 +131,42 @@
87
131
  - (NSArray<XCUIElement *> *)fb_filterDescendantsWithSnapshots:(NSArray<id<FBXCElementSnapshot>> *)snapshots
88
132
  onlyChildren:(BOOL)onlyChildren
89
133
  {
90
- if (0 == snapshots.count) {
91
- return @[];
92
- }
93
- NSMutableArray<NSString *> *matchedIds = [NSMutableArray new];
134
+ NSMutableArray<id<FBXCElementSnapshot>> *matchedSnapshots = [NSMutableArray array];
135
+ NSMutableArray<NSString *> *matchedIds = [NSMutableArray array];
94
136
  for (id<FBXCElementSnapshot> snapshot in snapshots) {
95
137
  @autoreleasepool {
96
138
  NSString *uid = [FBXCElementSnapshotWrapper wdUIDWithSnapshot:snapshot];
97
139
  if (nil != uid) {
140
+ [matchedSnapshots addObject:snapshot];
98
141
  [matchedIds addObject:uid];
99
142
  }
100
143
  }
101
144
  }
102
- NSMutableArray<XCUIElement *> *matchedElements = [NSMutableArray array];
145
+ if (0 == matchedIds.count) {
146
+ return @[];
147
+ }
148
+
149
+ NSMutableArray<XCUIElement *> *result = [NSMutableArray array];
103
150
  // self.lastSnapshot may be stale leftover from an unrelated earlier command.
104
151
  id<FBXCElementSnapshot> selfSnapshot = self.fb_cachedSnapshot;
105
- NSString *uid = nil == selfSnapshot
152
+ NSString *selfUid = nil == selfSnapshot
106
153
  ? self.fb_uid
107
154
  : [FBXCElementSnapshotWrapper wdUIDWithSnapshot:selfSnapshot];
108
- if (nil != uid && [matchedIds containsObject:uid]) {
109
- XCUIElement *stableSelf = [self fb_stableInstanceWithUid:uid];
110
- if (1 == snapshots.count) {
155
+ if (nil != selfUid && [matchedIds containsObject:selfUid]) {
156
+ XCUIElement *stableSelf = [self fb_stableInstanceWithUid:selfUid];
157
+ if (1 == matchedIds.count) {
111
158
  return @[stableSelf];
112
159
  }
113
- [matchedElements addObject:stableSelf];
160
+ [result addObject:stableSelf];
114
161
  }
115
- XCUIElementType type = XCUIElementTypeAny;
116
- NSArray<NSNumber *> *uniqueTypes = [snapshots valueForKeyPath:[NSString stringWithFormat:@"@distinctUnionOfObjects.%@", FBStringify(XCUIElement, elementType)]];
117
- if (uniqueTypes && [uniqueTypes count] == 1) {
118
- type = (XCUIElementType)[uniqueTypes.firstObject intValue];
119
- }
120
- XCUIElementQuery *query = onlyChildren
121
- ? [self.fb_query childrenMatchingType:type]
122
- : [self.fb_query descendantsMatchingType:type];
123
-
124
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K IN %@",FBStringify(FBXCElementSnapshotWrapper, fb_uid), matchedIds];
125
- [matchedElements addObjectsFromArray:[query matchingPredicate:predicate].allElementsBoundByIndex];
126
162
 
127
- for (XCUIElement *el in matchedElements) {
163
+ NSArray<XCUIElement *> *descendants = FBBindSnapshotsToElements(self.fb_query, matchedSnapshots, matchedIds, selfUid)
164
+ ?: FBQueryElementsWithUids(self.fb_query, matchedSnapshots, matchedIds, onlyChildren);
165
+ [result addObjectsFromArray:descendants];
166
+ for (XCUIElement *el in result) {
128
167
  el.fb_isResolvedNatively = @NO;
129
168
  }
130
- return matchedElements.copy;
169
+ return result.copy;
131
170
  }
132
171
 
133
172
  - (void)fb_waitUntilStable
@@ -23,6 +23,13 @@ NS_ASSUME_NONNULL_BEGIN
23
23
  */
24
24
  - (nullable id<FBXCElementSnapshot>)fb_cachedSnapshot;
25
25
 
26
+ /**
27
+ Enumerates matches using the requested binding strategy and retains their query
28
+ snapshots for response serialization. Falls back to normal element resolution
29
+ when the query cannot supply a complete set of cached snapshots.
30
+ */
31
+ - (NSArray<XCUIElement *> *)fb_allMatchesWithSnapshotsBoundByIndex:(BOOL)boundByIndex;
32
+
26
33
  @end
27
34
 
28
35
  NS_ASSUME_NONNULL_END
@@ -10,12 +10,13 @@
10
10
 
11
11
  #import "FBXCodeCompatibility.h"
12
12
  #import "XCUIElementQuery.h"
13
+ #import "XCUIElement+FBCaching.h"
13
14
  #import "FBXCElementSnapshot.h"
14
15
  #import "XCTElementSetTransformer-Protocol.h"
15
16
 
16
17
  @implementation XCUIElementQuery (FBHelpers)
17
18
 
18
- - (nullable id<FBXCElementSnapshot>)fb_cachedSnapshot
19
+ - (nullable NSOrderedSet<id<FBXCElementSnapshot>> *)fb_cachedSnapshots
19
20
  {
20
21
  id<FBXCElementSnapshot> rootElementSnapshot = self.rootElementSnapshot;
21
22
  if (nil == rootElementSnapshot) {
@@ -37,11 +38,36 @@
37
38
  matchingSnapshots = (NSOrderedSet *)[transformer transform:matchingSnapshots
38
39
  relatedElements:nil];
39
40
  }
40
- return matchingSnapshots.count == 1 ? matchingSnapshots.firstObject : nil;
41
+ return matchingSnapshots;
41
42
  } @catch (NSException *e) {
42
43
  [FBLogger logFmt:@"Got an unexpected error while retriveing the cached snapshot: %@", e.reason];
43
44
  }
44
45
  return nil;
45
46
  }
46
47
 
48
+ - (nullable id<FBXCElementSnapshot>)fb_cachedSnapshot
49
+ {
50
+ NSOrderedSet<id<FBXCElementSnapshot>> *snapshots = [self fb_cachedSnapshots];
51
+ return snapshots.count == 1 ? snapshots.firstObject : nil;
52
+ }
53
+
54
+ - (NSArray<XCUIElement *> *)fb_allMatchesWithSnapshotsBoundByIndex:(BOOL)boundByIndex
55
+ {
56
+ NSArray<XCUIElement *> *elements = boundByIndex
57
+ ? self.allElementsBoundByIndex
58
+ : self.allElementsBoundByAccessibilityElement;
59
+ if (elements.count == 0) {
60
+ return elements;
61
+ }
62
+ // Both arrays follow the order of this query. Reapply its transformers once,
63
+ // rather than resolving each bound element against the entire app hierarchy.
64
+ NSOrderedSet<id<FBXCElementSnapshot>> *snapshots = [self fb_cachedSnapshots];
65
+ if (snapshots.count == elements.count) {
66
+ [elements enumerateObjectsUsingBlock:^(XCUIElement *element, NSUInteger idx, BOOL *stop) {
67
+ [element fb_cacheSnapshot:snapshots[idx]];
68
+ }];
69
+ }
70
+ return elements;
71
+ }
72
+
47
73
  @end
@@ -15,11 +15,11 @@
15
15
  <key>CFBundlePackageType</key>
16
16
  <string>FMWK</string>
17
17
  <key>CFBundleShortVersionString</key>
18
- <string>16.18.2</string>
18
+ <string>16.18.4</string>
19
19
  <key>CFBundleSignature</key>
20
20
  <string>????</string>
21
21
  <key>CFBundleVersion</key>
22
- <string>16.18.2</string>
22
+ <string>16.18.4</string>
23
23
  <key>NSPrincipalClass</key>
24
24
  <string/>
25
25
  </dict>
@@ -16,6 +16,7 @@
16
16
  #import "FBMacros.h"
17
17
  #import "FBProtocolHelpers.h"
18
18
  #import "XCUIElementQuery.h"
19
+ #import "XCUIElement+FBCaching.h"
19
20
  #import "XCUIElement+FBResolve.h"
20
21
  #import "XCUIElement+FBUID.h"
21
22
  #import "XCUIElement+FBUtilities.h"
@@ -61,13 +62,60 @@ id<FBResponsePayload> FBResponseWithCachedElement(XCUIElement *element, FBElemen
61
62
  return FBResponseWithStatus([FBCommandStatus okWithValue:response]);
62
63
  }
63
64
 
65
+ // Resolves snapshots of all the given elements with a single app snapshot instead of one per element
66
+ static void FBAttachSnapshots(NSArray<XCUIElement *> *elements)
67
+ {
68
+ NSMutableDictionary<NSString *, NSMutableArray<XCUIElement *> *> *pending = [NSMutableDictionary dictionary];
69
+ for (XCUIElement *element in elements) {
70
+ if (nil != element.lastSnapshot || nil == element.fb_boundAccessibilityElement) {
71
+ continue;
72
+ }
73
+ NSString *uid = element.fb_cacheId;
74
+ if (nil != uid) {
75
+ NSMutableArray<XCUIElement *> *group = pending[uid];
76
+ if (nil == group) {
77
+ group = [NSMutableArray array];
78
+ pending[uid] = group;
79
+ }
80
+ [group addObject:element];
81
+ }
82
+ }
83
+ if (0 == pending.count) {
84
+ return;
85
+ }
86
+ @autoreleasepool {
87
+ XCUIApplication *application = elements.firstObject.application;
88
+ id<FBXCElementSnapshot> root = [application fb_standardSnapshot];
89
+ // The app is long-lived, so don't let it keep the whole tree after this call
90
+ application.lastSnapshot = nil;
91
+ NSMutableArray *snapshots = [NSMutableArray arrayWithObject:root];
92
+ [snapshots addObjectsFromArray:[(id)root _allDescendants]];
93
+ for (id<FBXCElementSnapshot> snapshot in snapshots) {
94
+ NSString *uid = [FBXCElementSnapshotWrapper wdUIDWithSnapshot:snapshot];
95
+ for (XCUIElement *element in (nil == uid ? nil : pending[uid])) {
96
+ element.lastSnapshot = snapshot;
97
+ }
98
+ }
99
+ }
100
+ }
101
+
64
102
  id<FBResponsePayload> FBResponseWithCachedElements(NSArray<XCUIElement *> *elements, FBElementCache *elementCache, BOOL compact)
65
103
  {
104
+ if (!compact) {
105
+ FBAttachSnapshots(elements);
106
+ }
66
107
  NSMutableArray *elementsResponse = [NSMutableArray array];
67
- for (XCUIElement *element in elements) {
68
- [elementCache storeElement:maybeStable(element)];
69
- [elementsResponse addObject:FBDictionaryResponseWithElement(element, compact)];
70
- element.lastSnapshot = nil;
108
+ @try {
109
+ for (XCUIElement *element in elements) {
110
+ [elementCache storeElement:maybeStable(element)];
111
+ [elementsResponse addObject:FBDictionaryResponseWithElement(element, compact)];
112
+ element.lastSnapshot = nil;
113
+ }
114
+ } @finally {
115
+ // Cached elements must not keep the attached snapshots if building a response throws
116
+ for (XCUIElement *element in elements) {
117
+ element.lastSnapshot = nil;
118
+ }
71
119
  }
72
120
  return FBResponseWithStatus([FBCommandStatus okWithValue:elementsResponse]);
73
121
  }
@@ -109,10 +157,15 @@ inline NSDictionary *FBDictionaryResponseWithElement(XCUIElement *element, BOOL
109
157
  {
110
158
  __block NSDictionary *elementResponse = nil;
111
159
  @autoreleasepool {
160
+ NSString *boundUid = element.fb_boundAccessibilityElement ? element.fb_cacheId : nil;
161
+ if (compact && nil != boundUid) {
162
+ elementResponse = FBToElementDict(boundUid);
163
+ return elementResponse;
164
+ }
112
165
  id<FBXCElementSnapshot> snapshot = element.lastSnapshot
113
166
  ?: element.fb_cachedSnapshot
114
167
  ?: [element fb_customSnapshot];
115
- NSDictionary *compactResult = FBToElementDict((NSString *)[FBXCElementSnapshotWrapper wdUIDWithSnapshot:snapshot]);
168
+ NSDictionary *compactResult = FBToElementDict(boundUid ?: (NSString *)[FBXCElementSnapshotWrapper wdUIDWithSnapshot:snapshot]);
116
169
  if (compact) {
117
170
  elementResponse = compactResult;
118
171
  return elementResponse;
@@ -28,6 +28,8 @@ static const NSTimeInterval FRAME_TIMEOUT = 1.;
28
28
  static const NSUInteger MAX_PENDING_FRAMES_PER_CLIENT = 4;
29
29
  static const NSTimeInterval FAILURE_BACKOFF_MIN = 1.0;
30
30
  static const NSTimeInterval FAILURE_BACKOFF_MAX = 10.0;
31
+ // The screen lookup is a round trip to testmanagerd, so it is not repeated for every frame.
32
+ static const uint64_t SCREEN_REFRESH_INTERVAL = 3 * NSEC_PER_SEC;
31
33
 
32
34
  static NSString *const SERVER_NAME = @"WDA MJPEG Server";
33
35
  static const char *QUEUE_NAME = "JPEG Screenshots Provider Queue";
@@ -48,6 +50,10 @@ static NSUInteger FBNormalizedMjpegFramerate(NSUInteger framerate)
48
50
  @property (nonatomic, assign) NSUInteger sentFramesCount;
49
51
  @property (nonatomic, assign) NSUInteger sentBytesCount;
50
52
  @property (nonatomic, assign) NSUInteger droppedFramesCount;
53
+ // Touched only on backgroundQueue.
54
+ @property (nonatomic, strong, nullable) XCUIScreen *cachedScreen;
55
+ @property (nonatomic, strong, nullable) NSNumber *cachedScreenDisplayId;
56
+ @property (nonatomic, assign) uint64_t cachedScreenTimestamp;
51
57
  // Frames submitted but not sent yet, per client. Guarded by @synchronized (self.listeningClients).
52
58
  @property (nonatomic, readonly) NSMapTable<id, NSNumber *> *pendingFrameCounts;
53
59
 
@@ -97,6 +103,25 @@ static NSUInteger FBNormalizedMjpegFramerate(NSUInteger framerate)
97
103
  }
98
104
  }
99
105
 
106
+ - (nullable XCUIScreen *)currentScreenWithError:(NSError **)error
107
+ {
108
+ NSNumber *displayId = FBConfiguration.sharedInstance.currentDisplayId;
109
+ uint64_t now = clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW);
110
+ BOOL isCacheValid = nil != self.cachedScreen
111
+ && (displayId == self.cachedScreenDisplayId || [displayId isEqual:self.cachedScreenDisplayId])
112
+ && now - self.cachedScreenTimestamp < SCREEN_REFRESH_INTERVAL;
113
+ if (isCacheValid) {
114
+ return self.cachedScreen;
115
+ }
116
+ // Resolve with the captured id, so the cache key always matches the cached screen.
117
+ self.cachedScreen = nil == displayId
118
+ ? XCUIScreen.mainScreen
119
+ : [FBScreen screenWithDisplayID:displayId.longLongValue error:error];
120
+ self.cachedScreenDisplayId = displayId;
121
+ self.cachedScreenTimestamp = now;
122
+ return self.cachedScreen;
123
+ }
124
+
100
125
  - (void)streamScreenshot
101
126
  {
102
127
  if (!self.isStreaming) {
@@ -115,13 +140,14 @@ static NSUInteger FBNormalizedMjpegFramerate(NSUInteger framerate)
115
140
  NSError *error;
116
141
  CGFloat compressionQuality = MAX(FBMinCompressionQuality,
117
142
  MIN(FBMaxCompressionQuality, (double)FBConfiguration.sharedInstance.mjpegServerScreenshotQuality / 100.0));
118
- XCUIScreen *screen = [FBScreen currentScreenWithError:&error];
143
+ XCUIScreen *screen = [self currentScreenWithError:&error];
119
144
  NSData *screenshotData = nil == screen ? nil : [FBScreenshot takeInOriginalResolutionWithScreenID:screen.displayID
120
145
  compressionQuality:compressionQuality
121
146
  uti:UTTypeJPEG
122
147
  timeout:FRAME_TIMEOUT
123
148
  error:&error];
124
149
  if (nil == screenshotData) {
150
+ self.cachedScreen = nil;
125
151
  [FBLogger logFmt:@"%@", error.description];
126
152
  self.consecutiveScreenshotFailures++;
127
153
  NSTimeInterval backoffSeconds = MIN(FAILURE_BACKOFF_MAX,
@@ -14,6 +14,7 @@
14
14
  #import "FBLogger.h"
15
15
  #import "XCUIApplication+FBHelpers.h"
16
16
  #import "XCUIElementQuery.h"
17
+ #import "XCUIElementQuery+FBHelpers.h"
17
18
  #import "FBXCTestDaemonsProxy.h"
18
19
  #import "XCTCapabilities.h"
19
20
  #import "XCTMessagingChannel_RunnerToDaemon-Protocol.h"
@@ -46,8 +47,9 @@
46
47
 
47
48
  - (NSArray<XCUIElement *> *)fb_allMatches
48
49
  {
50
+ // AX-bound elements get their uids without snapshots, index-bound ones reuse the query snapshots
49
51
  return FBConfiguration.sharedInstance.boundElementsByIndex
50
- ? self.allElementsBoundByIndex
52
+ ? [self fb_allMatchesWithSnapshotsBoundByIndex:YES]
51
53
  : self.allElementsBoundByAccessibilityElement;
52
54
  }
53
55
 
@@ -11,7 +11,10 @@
11
11
  #import "FBIntegrationTestCase.h"
12
12
  #import "FBConfiguration.h"
13
13
  #import "FBElementUtils.h"
14
+ #import "FBElementCache.h"
15
+ #import "XCUIElement+FBCaching.h"
14
16
  #import "FBExceptions.h"
17
+ #import "FBResponsePayload.h"
15
18
  #import "FBTestMacros.h"
16
19
  #import "XCUIElement.h"
17
20
  #import "XCUIElement+FBFind.h"
@@ -41,6 +44,71 @@
41
44
  FBAssertWaitTillBecomesTrue(self.testedView.exists);
42
45
  }
43
46
 
47
+ // Based on the lookup snapshot reuse checks from https://github.com/appium/appium-ios/pull/175
48
+ - (void)assertLookupSnapshotsWithIndexBinding:(BOOL)boundByIndex
49
+ {
50
+ BOOL previousBinding = FBConfiguration.sharedInstance.boundElementsByIndex;
51
+ @try {
52
+ FBConfiguration.sharedInstance.boundElementsByIndex = boundByIndex;
53
+ NSArray<NSArray<XCUIElement *> *> *lookups = @[
54
+ [self.testedView fb_descendantsMatchingClassName:@"XCUIElementTypeButton" shouldReturnAfterFirstMatch:NO],
55
+ [self.testedView fb_descendantsMatchingClassChain:@"**/XCUIElementTypeButton" shouldReturnAfterFirstMatch:NO],
56
+ [self.testedView fb_descendantsMatchingPredicate:[NSPredicate predicateWithFormat:@"type == 'XCUIElementTypeButton'"]
57
+ shouldReturnAfterFirstMatch:NO],
58
+ [self.testedView fb_descendantsMatchingIdentifier:@"Alerts" shouldReturnAfterFirstMatch:NO],
59
+ [self.testedView fb_descendantsMatchingXPathQuery:@"//XCUIElementTypeButton" shouldReturnAfterFirstMatch:NO],
60
+ ];
61
+ for (NSArray<XCUIElement *> *elements in lookups) {
62
+ XCTAssertGreaterThan(elements.count, 1u);
63
+ FBElementCache *cache = [FBElementCache new];
64
+ NSMutableSet<NSString *> *identifiers = [NSMutableSet set];
65
+ for (XCUIElement *element in elements) {
66
+ id<FBXCElementSnapshot> snapshot = element.lastSnapshot;
67
+ if (boundByIndex) {
68
+ // Index-bound matches reuse the snapshots of the query that found them
69
+ XCTAssertNotNil(snapshot);
70
+ } else {
71
+ XCTAssertNil(snapshot);
72
+ XCTAssertNotNil(element.fb_boundAccessibilityElement);
73
+ }
74
+ NSString *uid = element.fb_cacheId;
75
+ XCTAssertEqualObjects([cache storeElement:element], uid);
76
+ // Cache registration and the compact response must not resolve the element again
77
+ XCTAssertEqual(element.lastSnapshot, snapshot);
78
+ XCTAssertEqualObjects(FBDictionaryResponseWithElement(element, YES)[@"element-6066-11e4-a52e-4f735466cecf"], uid);
79
+ XCTAssertEqual(element.lastSnapshot, snapshot);
80
+ [identifiers addObject:uid];
81
+ // Resolve the element independently to catch order/identity errors
82
+ id<FBXCElementSnapshot> resolved = [element fb_standardSnapshot];
83
+ XCTAssertEqualObjects([FBXCElementSnapshotWrapper wdUIDWithSnapshot:resolved], uid);
84
+ if (nil != snapshot) {
85
+ XCTAssertEqualObjects(snapshot.label, resolved.label);
86
+ XCTAssertEqual(snapshot.elementType, resolved.elementType);
87
+ }
88
+ element.lastSnapshot = snapshot;
89
+ }
90
+ XCTAssertEqual(identifiers.count, elements.count);
91
+ FBResponseWithCachedElements(elements, cache, YES);
92
+ for (XCUIElement *element in elements) {
93
+ XCTAssertNil(element.lastSnapshot);
94
+ XCTAssertEqual([cache elementForUUID:element.fb_cacheId], element);
95
+ }
96
+ }
97
+ } @finally {
98
+ FBConfiguration.sharedInstance.boundElementsByIndex = previousBinding;
99
+ }
100
+ }
101
+
102
+ - (void)testLookupSnapshotsWithAccessibilityBinding
103
+ {
104
+ [self assertLookupSnapshotsWithIndexBinding:NO];
105
+ }
106
+
107
+ - (void)testLookupSnapshotsWithIndexBinding
108
+ {
109
+ [self assertLookupSnapshotsWithIndexBinding:YES];
110
+ }
111
+
44
112
  - (void)testDescendantsWithClassName
45
113
  {
46
114
  NSSet<NSString *> *expectedLabels = [NSSet setWithArray:FBMainViewButtonLabels];
@@ -401,6 +469,100 @@
401
469
  NSException, FBUnknownAttributeException);;
402
470
  }
403
471
 
472
+
473
+ - (void)testUidsOfAccessibilityBoundElementsDoNotNeedSnapshots
474
+ {
475
+ BOOL previousBinding = FBConfiguration.sharedInstance.boundElementsByIndex;
476
+ FBConfiguration.sharedInstance.boundElementsByIndex = NO;
477
+ @try {
478
+ NSArray<XCUIElement *> *matches = [self.testedView fb_descendantsMatchingClassName:@"XCUIElementTypeButton"
479
+ shouldReturnAfterFirstMatch:NO];
480
+ XCTAssertEqual(matches.count, FBMainViewButtonLabels.count);
481
+ for (XCUIElement *match in matches) {
482
+ XCTAssertNotNil(match.fb_boundAccessibilityElement);
483
+ match.lastSnapshot = nil;
484
+ NSString *uid = match.fb_uid;
485
+ XCTAssertNil(match.lastSnapshot, @"Getting the uid must not resolve the element");
486
+ XCTAssertEqualObjects(uid, [FBXCElementSnapshotWrapper wdUIDWithSnapshot:[match fb_standardSnapshot]]);
487
+ }
488
+ } @finally {
489
+ FBConfiguration.sharedInstance.boundElementsByIndex = previousBinding;
490
+ }
491
+ }
492
+
493
+ - (void)testElementResponsesMatchSnapshotUids
494
+ {
495
+ BOOL previousBinding = FBConfiguration.sharedInstance.boundElementsByIndex;
496
+ FBConfiguration.sharedInstance.boundElementsByIndex = NO;
497
+ @try {
498
+ NSArray<XCUIElement *> *matches = [self.testedView fb_descendantsMatchingClassName:@"XCUIElementTypeButton"
499
+ shouldReturnAfterFirstMatch:NO];
500
+ XCTAssertEqual(matches.count, FBMainViewButtonLabels.count);
501
+ NSMutableArray<NSString *> *expectedUids = [NSMutableArray array];
502
+ NSMutableArray<NSString *> *expectedLabels = [NSMutableArray array];
503
+ for (XCUIElement *match in matches) {
504
+ id<FBXCElementSnapshot> snapshot = [match fb_standardSnapshot];
505
+ [expectedUids addObject:(NSString *)[FBXCElementSnapshotWrapper wdUIDWithSnapshot:snapshot]];
506
+ [expectedLabels addObject:snapshot.label];
507
+ match.lastSnapshot = nil;
508
+ }
509
+ XCTAssertEqual([NSSet setWithArray:expectedUids].count, expectedUids.count);
510
+
511
+ FBElementCache *cache = [FBElementCache new];
512
+ NSArray *payloads = @[@(YES), @(NO)];
513
+ for (NSNumber *compact in payloads) {
514
+ id<FBResponsePayload> payload = FBResponseWithCachedElements(matches, cache, compact.boolValue);
515
+ XCTAssertNotNil(payload);
516
+ for (NSUInteger i = 0; i < matches.count; i++) {
517
+ NSDictionary *response = FBDictionaryResponseWithElement(matches[i], compact.boolValue);
518
+ XCTAssertEqualObjects(response[@"ELEMENT"], expectedUids[i]);
519
+ if (!compact.boolValue) {
520
+ XCTAssertEqualObjects(response[@"label"], expectedLabels[i]);
521
+ }
522
+ XCTAssertTrue([cache hasElementWithUUID:expectedUids[i]]);
523
+ }
524
+ }
525
+ } @finally {
526
+ FBConfiguration.sharedInstance.boundElementsByIndex = previousBinding;
527
+ }
528
+ }
529
+
530
+ - (void)testXPathMatchesAreBoundWithoutResolvingEachElement
531
+ {
532
+ NSArray<XCUIElement *> *matches = [self.testedView fb_descendantsMatchingXPathQuery:@"//XCUIElementTypeButton"
533
+ shouldReturnAfterFirstMatch:NO];
534
+ XCTAssertEqual(matches.count, FBMainViewButtonLabels.count);
535
+ NSMutableSet<NSString *> *uids = [NSMutableSet set];
536
+ for (XCUIElement *match in matches) {
537
+ XCTAssertNotNil(match.fb_boundAccessibilityElement);
538
+ XCTAssertNil(match.lastSnapshot, @"Matches must not retain the snapshots they were found with");
539
+ NSString *uid = match.fb_uid;
540
+ XCTAssertNil(match.lastSnapshot, @"Getting the uid must not resolve the element");
541
+ XCTAssertEqualObjects(uid, [FBXCElementSnapshotWrapper wdUIDWithSnapshot:[match fb_standardSnapshot]]);
542
+ XCTAssertEqual(match.elementType, XCUIElementTypeButton);
543
+ [uids addObject:uid];
544
+ }
545
+ XCTAssertEqual(uids.count, matches.count);
546
+ XCTAssertEqualObjects([matches valueForKey:@"label"], FBMainViewButtonLabels);
547
+ }
548
+
549
+ - (void)testXPathMatchesAreIndexBoundWhenRequested
550
+ {
551
+ BOOL previousBinding = FBConfiguration.sharedInstance.boundElementsByIndex;
552
+ FBConfiguration.sharedInstance.boundElementsByIndex = YES;
553
+ @try {
554
+ NSArray<XCUIElement *> *matches = [self.testedView fb_descendantsMatchingXPathQuery:@"//XCUIElementTypeButton"
555
+ shouldReturnAfterFirstMatch:NO];
556
+ XCTAssertEqual(matches.count, FBMainViewButtonLabels.count);
557
+ for (XCUIElement *match in matches) {
558
+ XCTAssertNil(match.fb_boundAccessibilityElement);
559
+ }
560
+ XCTAssertEqualObjects([matches valueForKey:@"label"], FBMainViewButtonLabels);
561
+ } @finally {
562
+ FBConfiguration.sharedInstance.boundElementsByIndex = previousBinding;
563
+ }
564
+ }
565
+
404
566
  @end
405
567
 
406
568
  @interface XCUIElementFBFindTests_AttributesPage : FBIntegrationTestCase
@@ -543,6 +705,21 @@
543
705
  @end
544
706
  @implementation XCUIElementFBFindTests_StaleAppSnapshot
545
707
 
708
+ - (void)testCachedLookupStillChecksStalenessAfterNavigation
709
+ {
710
+ [self launchApplication];
711
+ XCUIElement *element = [self.testedApplication fb_descendantsMatchingClassChain:
712
+ @"**/XCUIElementTypeButton[`label == 'Alerts'`]" shouldReturnAfterFirstMatch:NO].firstObject;
713
+ NSString *uid = element.fb_cacheId;
714
+ FBElementCache *cache = [FBElementCache new];
715
+ FBResponseWithCachedElement(element, cache, YES);
716
+ XCTAssertNil(element.lastSnapshot);
717
+
718
+ [self goToAttributesPage];
719
+ XCTAssertThrowsSpecificNamed([cache elementForUUID:uid checkStaleness:YES], NSException, FBStaleElementException);
720
+ XCTAssertFalse([cache hasElementWithUUID:uid]);
721
+ }
722
+
546
723
  // Regression test for https://github.com/appium/appium/issues/22672.
547
724
  - (void)testClassChainWithIntermediatePositionAfterStaleAppSnapshot
548
725
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "appium-webdriveragent",
3
- "version": "16.18.2",
3
+ "version": "16.18.4",
4
4
  "description": "Package bundling WebDriverAgent",
5
5
  "keywords": [
6
6
  "Appium",
@@ -83,5 +83,5 @@
83
83
  "node": "^20.19.0 || ^22.12.0 || >=24.0.0",
84
84
  "npm": ">=10"
85
85
  },
86
- "gitHead": "efad756d909958468cc0ed3575ef4a78b74bcbf9"
86
+ "gitHead": "b79ff5587b7273c2ad1712a168114ba45641e665"
87
87
  }