appium-webdriveragent 16.18.3 → 16.18.5

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.5](https://github.com/appium/appium-ios/compare/appium-webdriveragent@16.18.4...appium-webdriveragent@16.18.5) (2026-11-10)
7
+
8
+ ### Bug Fixes
9
+
10
+ * **webdriveragent:** keep a late MJPEG frame instead of backing off ([#179](https://github.com/appium/appium-ios/issues/179)) ([e57ef10](https://github.com/appium/appium-ios/commit/e57ef1069c155fbf37a3c223bcc96af7788025b3))
11
+
12
+
13
+ ## [16.18.4](https://github.com/appium/appium-ios/compare/appium-webdriveragent@16.18.3...appium-webdriveragent@16.18.4) (2026-11-09)
14
+
15
+ ### Performance Improvements
16
+
17
+ * **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))
18
+
19
+
6
20
  ## [16.18.3](https://github.com/appium/appium-ios/compare/appium-webdriveragent@16.18.2...appium-webdriveragent@16.18.3) (2026-11-08)
7
21
 
8
22
  ### Bug Fixes
@@ -1176,6 +1176,7 @@
1176
1176
  EE3A18661CDE734B00DE4205 /* FBKeyboard.h in Headers */ = {isa = PBXBuildFile; fileRef = EE3A18641CDE734B00DE4205 /* FBKeyboard.h */; settings = {ATTRIBUTES = (Public, ); }; };
1177
1177
  EE3A18671CDE734B00DE4205 /* FBKeyboard.m in Sources */ = {isa = PBXBuildFile; fileRef = EE3A18651CDE734B00DE4205 /* FBKeyboard.m */; };
1178
1178
  EE3F8CFE1D08AA17006F02CE /* FBRunLoopSpinnerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = EE3F8CFD1D08AA17006F02CE /* FBRunLoopSpinnerTests.m */; };
1179
+ AA11BB22CC33DD44EE55FF11 /* FBMjpegServerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AA11BB22CC33DD44EE55FF12 /* FBMjpegServerTests.m */; };
1179
1180
  EE3F8D001D08B05F006F02CE /* FBElementTypeTransformerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = EE3F8CFF1D08B05F006F02CE /* FBElementTypeTransformerTests.m */; };
1180
1181
  EE5095E51EBCC9090028E2FE /* FBTypingTest.m in Sources */ = {isa = PBXBuildFile; fileRef = AD76723F1D6B826F00610457 /* FBTypingTest.m */; };
1181
1182
  EE5095EB1EBCC9090028E2FE /* XCElementSnapshotHitPointTests.m in Sources */ = {isa = PBXBuildFile; fileRef = EE006EB21EBA1C7B006900A4 /* XCElementSnapshotHitPointTests.m */; };
@@ -1888,6 +1889,7 @@
1888
1889
  EE3A18641CDE734B00DE4205 /* FBKeyboard.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FBKeyboard.h; path = WebDriverAgentLib/Utilities/FBKeyboard.h; sourceTree = SOURCE_ROOT; };
1889
1890
  EE3A18651CDE734B00DE4205 /* FBKeyboard.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FBKeyboard.m; path = WebDriverAgentLib/Utilities/FBKeyboard.m; sourceTree = SOURCE_ROOT; };
1890
1891
  EE3F8CFD1D08AA17006F02CE /* FBRunLoopSpinnerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FBRunLoopSpinnerTests.m; sourceTree = "<group>"; };
1892
+ AA11BB22CC33DD44EE55FF12 /* FBMjpegServerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FBMjpegServerTests.m; sourceTree = "<group>"; };
1891
1893
  EE3F8CFF1D08B05F006F02CE /* FBElementTypeTransformerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FBElementTypeTransformerTests.m; sourceTree = "<group>"; };
1892
1894
  EE5095FE1EBCC9090028E2FE /* IntegrationTests_2.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = IntegrationTests_2.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
1893
1895
  EE55B3221D1D5388003AAAEC /* FBTableDataSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FBTableDataSource.h; sourceTree = "<group>"; };
@@ -2762,6 +2764,7 @@
2762
2764
  76B7399DDF52C85A21433C1D /* FBHTTPServerTests.m */,
2763
2765
  EE9B76571CF7987300275851 /* FBRouteTests.m */,
2764
2766
  EE3F8CFD1D08AA17006F02CE /* FBRunLoopSpinnerTests.m */,
2767
+ AA11BB22CC33DD44EE55FF12 /* FBMjpegServerTests.m */,
2765
2768
  ADEF63AE1D09DEBE0070A7E3 /* FBRuntimeUtilsTests.m */,
2766
2769
  714801D01FA9D9FA00DC5997 /* FBSDKVersionTests.m */,
2767
2770
  EE6A89251D0B19E60083E92B /* FBSessionTests.m */,
@@ -4922,6 +4925,7 @@
4922
4925
  6C07E44E139D41D6BA092F39 /* FBTouchableViewTests.m in Sources */,
4923
4926
  713352FD26CEF31D00523CBC /* FBLRUCacheTests.m in Sources */,
4924
4927
  EE3F8CFE1D08AA17006F02CE /* FBRunLoopSpinnerTests.m in Sources */,
4928
+ AA11BB22CC33DD44EE55FF11 /* FBMjpegServerTests.m in Sources */,
4925
4929
  A09D847635CA4C155583B967 /* FBXCAXClientProxyTests.m in Sources */,
4926
4930
  714801D11FA9D9FA00DC5997 /* FBSDKVersionTests.m in Sources */,
4927
4931
  EE3F8D001D08B05F006F02CE /* FBElementTypeTransformerTests.m in Sources */,
@@ -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.3</string>
18
+ <string>16.18.5</string>
19
19
  <key>CFBundleSignature</key>
20
20
  <string>????</string>
21
21
  <key>CFBundleVersion</key>
22
- <string>16.18.3</string>
22
+ <string>16.18.5</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;