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.
@@ -14,6 +14,7 @@
14
14
  // Textual import, not `@import Network;` - see the comment in FBTCPSocket.h.
15
15
  #import <Network/Network.h>
16
16
  #import "FBConfiguration.h"
17
+ #import "FBErrorBuilder.h"
17
18
  #import "FBLogger.h"
18
19
  #import "FBScreenshot.h"
19
20
  #import "FBScreen.h"
@@ -22,11 +23,13 @@
22
23
  #import "XCUIScreen.h"
23
24
 
24
25
  static const NSUInteger MAX_FPS = 60;
25
- static const NSTimeInterval FRAME_TIMEOUT = 1.;
26
+ static const NSTimeInterval DEFAULT_FRAME_TIMEOUT = 1.;
27
+ // A frame that misses the frame timeout is still awaited this long (testmanagerd answers it after its snapshot).
28
+ static const NSTimeInterval DEFAULT_LATE_FRAME_LIMIT = 10.;
26
29
  // nw_connection_send buffers without backpressure, so a client that stops reading would retain
27
30
  // every generated frame. Frames past this cap are dropped instead of queued.
28
31
  static const NSUInteger MAX_PENDING_FRAMES_PER_CLIENT = 4;
29
- static const NSTimeInterval FAILURE_BACKOFF_MIN = 1.0;
32
+ static const NSTimeInterval DEFAULT_FAILURE_BACKOFF_MIN = 1.0;
30
33
  static const NSTimeInterval FAILURE_BACKOFF_MAX = 10.0;
31
34
  // The screen lookup is a round trip to testmanagerd, so it is not repeated for every frame.
32
35
  static const uint64_t SCREEN_REFRESH_INTERVAL = 3 * NSEC_PER_SEC;
@@ -39,6 +42,56 @@ static NSUInteger FBNormalizedMjpegFramerate(NSUInteger framerate)
39
42
  return (0 == framerate || framerate > MAX_FPS) ? MAX_FPS : framerate;
40
43
  }
41
44
 
45
+ // A screenshot request whose answer may arrive after the stream stopped waiting for it.
46
+ @interface FBMjpegPendingFrame : NSObject
47
+ @property (nonatomic, readonly, nullable) NSData *data;
48
+ @property (nonatomic, readonly, nullable) NSError *error;
49
+ // What the request was made for: a frame is not reused for other listeners or another display.
50
+ @property (nonatomic, readonly) NSUInteger epoch;
51
+ @property (nonatomic, readonly, nullable) NSNumber *displayId;
52
+ - (instancetype)initWithEpoch:(NSUInteger)epoch displayId:(nullable NSNumber *)displayId;
53
+ - (void)fulfillWithData:(nullable NSData *)data error:(nullable NSError *)error;
54
+ - (BOOL)isOlderThan:(NSTimeInterval)seconds;
55
+ // Returns NO if there is no answer yet.
56
+ - (BOOL)waitWithTimeout:(NSTimeInterval)timeout;
57
+ @end
58
+
59
+ @implementation FBMjpegPendingFrame {
60
+ dispatch_semaphore_t _answered;
61
+ uint64_t _timeRequested;
62
+ }
63
+
64
+ - (instancetype)initWithEpoch:(NSUInteger)epoch displayId:(NSNumber *)displayId
65
+ {
66
+ if ((self = [super init])) {
67
+ _epoch = epoch;
68
+ _displayId = displayId;
69
+ _answered = dispatch_semaphore_create(0);
70
+ _timeRequested = clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW);
71
+ }
72
+ return self;
73
+ }
74
+
75
+ - (BOOL)isOlderThan:(NSTimeInterval)seconds
76
+ {
77
+ return clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW) - _timeRequested >= (uint64_t)(seconds * NSEC_PER_SEC);
78
+ }
79
+
80
+ // The result is written before the signal and read only after the wait succeeds.
81
+ - (void)fulfillWithData:(NSData *)data error:(NSError *)error
82
+ {
83
+ _data = data;
84
+ _error = error;
85
+ dispatch_semaphore_signal(_answered);
86
+ }
87
+
88
+ - (BOOL)waitWithTimeout:(NSTimeInterval)timeout
89
+ {
90
+ return 0 == dispatch_semaphore_wait(_answered, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC)));
91
+ }
92
+
93
+ @end
94
+
42
95
 
43
96
  @interface FBMjpegServer()
44
97
 
@@ -54,6 +107,15 @@ static NSUInteger FBNormalizedMjpegFramerate(NSUInteger framerate)
54
107
  @property (nonatomic, strong, nullable) XCUIScreen *cachedScreen;
55
108
  @property (nonatomic, strong, nullable) NSNumber *cachedScreenDisplayId;
56
109
  @property (nonatomic, assign) uint64_t cachedScreenTimestamp;
110
+ // The request that missed the frame timeout and is still awaited. Touched only on backgroundQueue.
111
+ @property (nonatomic, strong, nullable) FBMjpegPendingFrame *pendingFrame;
112
+ // Bumped when the last client leaves. Guarded by @synchronized (self.listeningClients).
113
+ @property (nonatomic, assign) NSUInteger listenersEpoch;
114
+ // Overridable in tests. The default sends the request to testmanagerd.
115
+ @property (atomic, copy) BOOL (^frameRequest)(void (^completion)(NSData * _Nullable, NSError * _Nullable), NSError **error);
116
+ @property (atomic, assign) NSTimeInterval frameTimeout;
117
+ @property (atomic, assign) NSTimeInterval lateFrameLimit;
118
+ @property (atomic, assign) NSTimeInterval failureBackoffMin;
57
119
  // Frames submitted but not sent yet, per client. Guarded by @synchronized (self.listeningClients).
58
120
  @property (nonatomic, readonly) NSMapTable<id, NSNumber *> *pendingFrameCounts;
59
121
 
@@ -67,6 +129,9 @@ static NSUInteger FBNormalizedMjpegFramerate(NSUInteger framerate)
67
129
  if ((self = [super init])) {
68
130
  _consecutiveScreenshotFailures = 0;
69
131
  _isStreaming = YES;
132
+ _frameTimeout = DEFAULT_FRAME_TIMEOUT;
133
+ _lateFrameLimit = DEFAULT_LATE_FRAME_LIMIT;
134
+ _failureBackoffMin = DEFAULT_FAILURE_BACKOFF_MIN;
70
135
  _sentFramesCount = 0;
71
136
  _sentBytesCount = 0;
72
137
  _listeningClients = [NSMutableArray array];
@@ -76,6 +141,9 @@ static NSUInteger FBNormalizedMjpegFramerate(NSUInteger framerate)
76
141
  dispatch_queue_attr_t queueAttributes = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, 0);
77
142
  _backgroundQueue = dispatch_queue_create(QUEUE_NAME, queueAttributes);
78
143
  __weak typeof(self) weakSelf = self;
144
+ _frameRequest = ^BOOL(void (^completion)(NSData *, NSError *), NSError **error) {
145
+ return [weakSelf requestScreenshotWithCompletion:completion error:error];
146
+ };
79
147
  dispatch_async(_backgroundQueue, ^{
80
148
  [weakSelf streamScreenshot];
81
149
  });
@@ -122,6 +190,67 @@ static NSUInteger FBNormalizedMjpegFramerate(NSUInteger framerate)
122
190
  return self.cachedScreen;
123
191
  }
124
192
 
193
+ - (BOOL)requestScreenshotWithCompletion:(void (^)(NSData * _Nullable, NSError * _Nullable))completion
194
+ error:(NSError **)error
195
+ {
196
+ XCUIScreen *screen = [self currentScreenWithError:error];
197
+ if (nil == screen) {
198
+ return NO;
199
+ }
200
+ CGFloat compressionQuality = MAX(FBMinCompressionQuality,
201
+ MIN(FBMaxCompressionQuality, (double)FBConfiguration.sharedInstance.mjpegServerScreenshotQuality / 100.0));
202
+ return [FBScreenshot requestInOriginalResolutionWithScreenID:screen.displayID
203
+ compressionQuality:compressionQuality
204
+ uti:UTTypeJPEG
205
+ completion:completion
206
+ error:error];
207
+ }
208
+
209
+ - (nullable FBMjpegPendingFrame *)requestFrameWithEpoch:(NSUInteger)epoch error:(NSError **)error
210
+ {
211
+ FBMjpegPendingFrame *frame = [[FBMjpegPendingFrame alloc] initWithEpoch:epoch
212
+ displayId:FBConfiguration.sharedInstance.currentDisplayId];
213
+ BOOL isRequested = self.frameRequest(^(NSData *data, NSError *err) {
214
+ if (nil == data && nil == err) {
215
+ NSError *emptyError;
216
+ [[[FBErrorBuilder builder]
217
+ withDescription:@"The screenshot request has been answered without an image"]
218
+ buildError:&emptyError];
219
+ err = emptyError;
220
+ }
221
+ [frame fulfillWithData:data error:err];
222
+ }, error);
223
+ return isRequested ? frame : nil;
224
+ }
225
+
226
+ // NO if the frame was requested for other listeners or another display.
227
+ - (BOOL)isFrameCurrent:(FBMjpegPendingFrame *)frame
228
+ {
229
+ NSNumber *displayId = FBConfiguration.sharedInstance.currentDisplayId;
230
+ @synchronized (self.listeningClients) {
231
+ return frame.epoch == self.listenersEpoch
232
+ && (displayId == frame.displayId || [displayId isEqual:frame.displayId]);
233
+ }
234
+ }
235
+
236
+ - (nullable FBMjpegPendingFrame *)takePendingFrame
237
+ {
238
+ FBMjpegPendingFrame *frame = self.pendingFrame;
239
+ self.pendingFrame = nil;
240
+ return nil != frame && [self isFrameCurrent:frame] ? frame : nil;
241
+ }
242
+
243
+ - (void)handleScreenshotFailure:(nullable NSError *)error timeStarted:(uint64_t)timeStarted
244
+ {
245
+ self.cachedScreen = nil;
246
+ [FBLogger logFmt:@"%@", error.description];
247
+ self.consecutiveScreenshotFailures++;
248
+ NSTimeInterval backoffSeconds = MIN(FAILURE_BACKOFF_MAX,
249
+ self.failureBackoffMin * (1 << MIN(self.consecutiveScreenshotFailures, 4)));
250
+ uint64_t backoffInterval = (uint64_t)(backoffSeconds * NSEC_PER_SEC);
251
+ [self scheduleNextScreenshotWithInterval:backoffInterval timeStarted:timeStarted];
252
+ }
253
+
125
254
  - (void)streamScreenshot
126
255
  {
127
256
  if (!self.isStreaming) {
@@ -130,30 +259,47 @@ static NSUInteger FBNormalizedMjpegFramerate(NSUInteger framerate)
130
259
  NSUInteger framerate = FBNormalizedMjpegFramerate(FBConfiguration.sharedInstance.mjpegServerFramerate);
131
260
  uint64_t timerInterval = (uint64_t)(1.0 / (double)framerate * NSEC_PER_SEC);
132
261
  uint64_t timeStarted = clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW);
262
+ NSUInteger epoch;
133
263
  @synchronized (self.listeningClients) {
264
+ epoch = self.listenersEpoch;
134
265
  if (0 == self.listeningClients.count) {
266
+ self.pendingFrame = nil;
135
267
  [self scheduleNextScreenshotWithInterval:timerInterval timeStarted:timeStarted];
136
268
  return;
137
269
  }
138
270
  }
139
271
 
140
272
  NSError *error;
141
- CGFloat compressionQuality = MAX(FBMinCompressionQuality,
142
- MIN(FBMaxCompressionQuality, (double)FBConfiguration.sharedInstance.mjpegServerScreenshotQuality / 100.0));
143
- XCUIScreen *screen = [self currentScreenWithError:&error];
144
- NSData *screenshotData = nil == screen ? nil : [FBScreenshot takeInOriginalResolutionWithScreenID:screen.displayID
145
- compressionQuality:compressionQuality
146
- uti:UTTypeJPEG
147
- timeout:FRAME_TIMEOUT
148
- error:&error];
273
+ FBMjpegPendingFrame *frame = [self takePendingFrame];
274
+ BOOL wasLate = nil != frame;
275
+ frame = frame ?: [self requestFrameWithEpoch:epoch error:&error];
276
+ if (nil != frame) {
277
+ if ([frame waitWithTimeout:self.frameTimeout]) {
278
+ if (![self isFrameCurrent:frame]) {
279
+ // The audience or the display has changed while waiting; this is not a failure.
280
+ [self scheduleNextScreenshotWithInterval:0 timeStarted:timeStarted];
281
+ return;
282
+ }
283
+ error = frame.error;
284
+ } else if (![frame isOlderThan:self.lateFrameLimit]) {
285
+ if (!wasLate) {
286
+ [FBLogger verboseLog:@"The screenshot misses the frame timeout, still waiting for it"];
287
+ }
288
+ // No other request while this one is unanswered; keep waiting for it right away.
289
+ self.pendingFrame = frame;
290
+ [self scheduleNextScreenshotWithInterval:0 timeStarted:timeStarted];
291
+ return;
292
+ } else {
293
+ [[[FBErrorBuilder builder]
294
+ withDescriptionFormat:@"Cannot take a screenshot within %@ s timeout", @(self.lateFrameLimit)]
295
+ buildError:&error];
296
+ frame = nil;
297
+ }
298
+ }
299
+
300
+ NSData *screenshotData = frame.data;
149
301
  if (nil == screenshotData) {
150
- self.cachedScreen = nil;
151
- [FBLogger logFmt:@"%@", error.description];
152
- self.consecutiveScreenshotFailures++;
153
- NSTimeInterval backoffSeconds = MIN(FAILURE_BACKOFF_MAX,
154
- FAILURE_BACKOFF_MIN * (1 << MIN(self.consecutiveScreenshotFailures, 4)));
155
- uint64_t backoffInterval = (uint64_t)(backoffSeconds * NSEC_PER_SEC);
156
- [self scheduleNextScreenshotWithInterval:backoffInterval timeStarted:timeStarted];
302
+ [self handleScreenshotFailure:error timeStarted:timeStarted];
157
303
  return;
158
304
  }
159
305
 
@@ -164,13 +310,13 @@ static NSUInteger FBNormalizedMjpegFramerate(NSUInteger framerate)
164
310
  [self.imageProcessor submitImageData:screenshotData
165
311
  scalingFactor:scalingFactor
166
312
  completionHandler:^(NSData * _Nonnull scaled) {
167
- [weakSelf sendScreenshot:scaled];
313
+ [weakSelf sendScreenshot:scaled forFrame:frame];
168
314
  }];
169
315
 
170
316
  [self scheduleNextScreenshotWithInterval:timerInterval timeStarted:timeStarted];
171
317
  }
172
318
 
173
- - (void)sendScreenshot:(NSData *)screenshotData {
319
+ - (void)sendScreenshot:(NSData *)screenshotData forFrame:(FBMjpegPendingFrame *)frame {
174
320
  if (!self.isStreaming) {
175
321
  return;
176
322
  }
@@ -179,7 +325,7 @@ static NSUInteger FBNormalizedMjpegFramerate(NSUInteger framerate)
179
325
  [chunk appendData:screenshotData];
180
326
  [chunk appendData:(id)[@"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
181
327
  @synchronized (self.listeningClients) {
182
- if (!self.isStreaming || 0 == self.listeningClients.count) {
328
+ if (!self.isStreaming || 0 == self.listeningClients.count || ![self isFrameCurrent:frame]) {
183
329
  return;
184
330
  }
185
331
  NSUInteger clientCount = self.listeningClients.count;
@@ -246,6 +392,9 @@ static NSUInteger FBNormalizedMjpegFramerate(NSUInteger framerate)
246
392
  @synchronized (self.listeningClients) {
247
393
  [self.listeningClients removeObject:client];
248
394
  [self.pendingFrameCounts removeObjectForKey:client];
395
+ if (0 == self.listeningClients.count) {
396
+ self.listenersEpoch++;
397
+ }
249
398
  }
250
399
  // Nothing is owed to a client that stopped listening, so its socket can go right away.
251
400
  nw_connection_cancel(client);
@@ -39,6 +39,22 @@ NS_ASSUME_NONNULL_BEGIN
39
39
  timeout:(NSTimeInterval)timeout
40
40
  error:(NSError **)error;
41
41
 
42
+ /**
43
+ Requests a non-scaled screenshot without waiting for the answer
44
+
45
+ @param screenID The screen identifier to take the screenshot from
46
+ @param compressionQuality Normalized screenshot quality value in range 0..1, where 1 is the best quality
47
+ @param uti UTType... constant, which defines the type of the returned screenshot image
48
+ @param completion Called once, on an arbitrary queue and at any time later, with either the image data or the error
49
+ @param error If the request cannot be made, upon return contains an NSError object that describes the problem.
50
+ @return YES if the request has been sent, in which case the completion will be called
51
+ */
52
+ + (BOOL)requestInOriginalResolutionWithScreenID:(long long)screenID
53
+ compressionQuality:(CGFloat)compressionQuality
54
+ uti:(UTType *)uti
55
+ completion:(void (^)(NSData * _Nullable data, NSError * _Nullable error))completion
56
+ error:(NSError **)error;
57
+
42
58
  @end
43
59
 
44
60
  NS_ASSUME_NONNULL_END
@@ -93,36 +93,52 @@ NSString *formatTimeInterval(NSTimeInterval interval) {
93
93
  error:error];
94
94
  }
95
95
 
96
+ + (BOOL)requestInOriginalResolutionWithScreenID:(long long)screenID
97
+ compressionQuality:(CGFloat)compressionQuality
98
+ uti:(UTType *)uti
99
+ completion:(void (^)(NSData * _Nullable, NSError * _Nullable))completion
100
+ error:(NSError **)error
101
+ {
102
+ id screenshotRequest = [self.class screenshotRequestWithScreenID:screenID
103
+ rect:CGRectNull
104
+ uti:uti
105
+ compressionQuality:compressionQuality
106
+ error:error];
107
+ if (nil == screenshotRequest) {
108
+ return NO;
109
+ }
110
+ id<XCTMessagingChannel_RunnerToDaemon> proxy = [FBXCTestDaemonsProxy testRunnerProxy];
111
+ [proxy _XCT_requestScreenshot:screenshotRequest
112
+ withReply:^(id image, NSError *err) {
113
+ completion(nil == err ? [image data] : nil, err);
114
+ }];
115
+ return YES;
116
+ }
117
+
96
118
  + (NSData *)takeInOriginalResolutionWithScreenID:(long long)screenID
97
119
  compressionQuality:(CGFloat)compressionQuality
98
120
  uti:(UTType *)uti
99
121
  timeout:(NSTimeInterval)timeout
100
122
  error:(NSError **)error
101
123
  {
102
- id<XCTMessagingChannel_RunnerToDaemon> proxy = [FBXCTestDaemonsProxy testRunnerProxy];
103
124
  __block NSData *screenshotData = nil;
104
125
  __block NSError *innerError = nil;
105
126
  dispatch_semaphore_t sem = dispatch_semaphore_create(0);
106
- id screnshotRequest = [self.class screenshotRequestWithScreenID:screenID
107
- rect:CGRectNull
108
- uti:uti
109
- compressionQuality:compressionQuality
110
- error:error];
111
- if (nil == screnshotRequest) {
127
+ BOOL isRequested = [self.class requestInOriginalResolutionWithScreenID:screenID
128
+ compressionQuality:compressionQuality
129
+ uti:uti
130
+ completion:^(NSData *data, NSError *err) {
131
+ screenshotData = data;
132
+ innerError = err;
133
+ dispatch_semaphore_signal(sem);
134
+ }
135
+ error:error];
136
+ if (!isRequested) {
112
137
  return nil;
113
138
  }
114
- [proxy _XCT_requestScreenshot:screnshotRequest
115
- withReply:^(id image, NSError *err) {
116
- if (nil != err) {
117
- innerError = err;
118
- } else {
119
- screenshotData = [image data];
120
- }
121
- dispatch_semaphore_signal(sem);
122
- }];
123
139
  int64_t timeoutNs = (int64_t)(timeout * NSEC_PER_SEC);
124
140
  if (0 != dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, timeoutNs))) {
125
- NSString *timeoutMsg = [NSString stringWithFormat:@"Cannot take a screenshot within %@ timeout", formatTimeInterval(SCREENSHOT_TIMEOUT)];
141
+ NSString *timeoutMsg = [NSString stringWithFormat:@"Cannot take a screenshot within %@ timeout", formatTimeInterval(timeout)];
126
142
  if (nil == error) {
127
143
  [FBLogger log:timeoutMsg];
128
144
  } else if (nil == innerError) {
@@ -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
  {