Skip to content

Commit 33d780a

Browse files
feat: Add enforceCustomSnapshots setting (#1087)
1 parent aba09cd commit 33d780a

File tree

8 files changed

+44
-6
lines changed

8 files changed

+44
-6
lines changed

WebDriverAgentLib/Categories/XCUIElement+FBUtilities.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ NS_ASSUME_NONNULL_BEGIN
2626
- Memory-friedly
2727
- `children` property is set to `nil` if not taken from XCUIApplication
2828
- `value` property is cut off to max 512 bytes
29+
- Sometimes `frame` properties may be off.
2930
3031
@return The recent snapshot of the element
3132
@throws FBStaleElementException if the element is not present in DOM and thus no snapshot could be made
@@ -55,7 +56,9 @@ NS_ASSUME_NONNULL_BEGIN
5556
The maximum snapshot tree depth is set by `FBConfiguration.snapshotMaxDepth`
5657
5758
Snapshot specifics:
58-
- Less performant in comparison to the standard one
59+
- Less performant in comparison to the custom one. Internally, it calls same APIs
60+
that fb_customSnapshot does, although this one has some additional logic to ensure
61+
the snapshot is valid. It also may make retries, which slows the call down significantly.
5962
- The `hittable` property calculation is aligned with the native calculation logic
6063
6164
@return The recent snapshot of the element

WebDriverAgentLib/Categories/XCUIElement+FBWebDriverAttributes.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import "XCUIElement+FBWebDriverAttributes.h"
1010

11+
#import "FBConfiguration.h"
1112
#import "FBElementTypeTransformer.h"
1213
#import "FBElementHelpers.h"
1314
#import "FBLogger.h"
@@ -34,6 +35,10 @@ @implementation XCUIElement (WebDriverAttributesForwarding)
3435
if ([name isEqualToString:FBStringify(XCUIElement, isWDHittable)]) {
3536
return [self fb_nativeSnapshot];
3637
}
38+
// https://github.com/appium/WebDriverAgent/issues/1085
39+
if (FBConfiguration.enforceCustomSnapshots) {
40+
return [self fb_customSnapshot];
41+
}
3742
// https://github.com/appium/appium-xcuitest-driver/issues/2552
3843
BOOL isValueRequest = [name isEqualToString:FBStringify(XCUIElement, wdValue)];
3944
if ([self isKindOfClass:XCUIApplication.class] && !isValueRequest) {
@@ -177,7 +182,7 @@ - (CGRect)wdNativeFrame
177182
characteristics of the element such as Button, Link, Image, etc.
178183
You can find the list of possible traits in the Apple documentation:
179184
https://developer.apple.com/documentation/uikit/uiaccessibilitytraits?language=objc
180-
185+
181186
@return A comma-separated string of accessibility traits, or an empty string if no traits are set
182187
*/
183188
- (NSString *)wdTraits

WebDriverAgentLib/Commands/FBSessionCommands.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ + (NSArray *)routes
355355
FB_SETTING_INCLUDE_HITTABLE_IN_PAGE_SOURCE: @([FBConfiguration includeHittableInPageSource]),
356356
FB_SETTING_INCLUDE_NATIVE_FRAME_IN_PAGE_SOURCE: @([FBConfiguration includeNativeFrameInPageSource]),
357357
FB_SETTING_INCLUDE_MIN_MAX_VALUE_IN_PAGE_SOURCE: @([FBConfiguration includeMinMaxValueInPageSource]),
358+
FB_SETTING_ENFORCE_CUSTOM_SNAPSHOTS: @([FBConfiguration enforceCustomSnapshots]),
358359
FB_SETTING_LIMIT_XPATH_CONTEXT_SCOPE: @([FBConfiguration limitXpathContextScope]),
359360
#if !TARGET_OS_TV
360361
FB_SETTING_SCREENSHOT_ORIENTATION: [FBConfiguration humanReadableScreenshotOrientation],
@@ -466,6 +467,9 @@ + (NSArray *)routes
466467
if (nil != [settings objectForKey:FB_SETTING_INCLUDE_MIN_MAX_VALUE_IN_PAGE_SOURCE]) {
467468
[FBConfiguration setIncludeMinMaxValueInPageSource:[[settings objectForKey:FB_SETTING_INCLUDE_MIN_MAX_VALUE_IN_PAGE_SOURCE] boolValue]];
468469
}
470+
if (nil != [settings objectForKey:FB_SETTING_ENFORCE_CUSTOM_SNAPSHOTS]) {
471+
[FBConfiguration setEnforceCustomSnapshots:[[settings objectForKey:FB_SETTING_ENFORCE_CUSTOM_SNAPSHOTS] boolValue]];
472+
}
469473
if (nil != [settings objectForKey:FB_SETTING_LIMIT_XPATH_CONTEXT_SCOPE]) {
470474
[FBConfiguration setLimitXpathContextScope:[[settings objectForKey:FB_SETTING_LIMIT_XPATH_CONTEXT_SCOPE] boolValue]];
471475
}
@@ -542,7 +546,7 @@ + (NSString *)deviceNameByUserInterfaceIdiom:(UIUserInterfaceIdiom) userInterfac
542546
}
543547
// CarPlay, Mac, Vision UI or unknown are possible
544548
return @"Unknown";
545-
549+
546550
}
547551

548552
+ (NSDictionary *)currentCapabilities

WebDriverAgentLib/Utilities/FBConfiguration.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,17 @@ typedef NS_ENUM(NSInteger, FBConfigurationKeyboardPreference) {
376376
+ (void)setIncludeMinMaxValueInPageSource:(BOOL)enabled;
377377
+ (BOOL)includeMinMaxValueInPageSource;
378378

379+
/**
380+
* Whether to enforce the use of custom snapshots instead of standard snapshots.
381+
* When enabled, fb_customSnapshot is always invoked instead of fb_standardSnapshot
382+
* for XPath tree building and element attributes fetching.
383+
* Disabled by default.
384+
*
385+
* @param enabled Either YES or NO
386+
*/
387+
+ (void)setEnforceCustomSnapshots:(BOOL)enabled;
388+
+ (BOOL)enforceCustomSnapshots;
389+
379390
@end
380391

381392
NS_ASSUME_NONNULL_END

WebDriverAgentLib/Utilities/FBConfiguration.m

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
static BOOL FBShouldIncludeHittableInPageSource = NO;
6464
static BOOL FBShouldIncludeNativeFrameInPageSource = NO;
6565
static BOOL FBShouldIncludeMinMaxValueInPageSource = NO;
66+
static BOOL FBShouldEnforceCustomSnapshots = NO;
6667

6768
@implementation FBConfiguration
6869

@@ -153,7 +154,7 @@ + (NSInteger)mjpegServerPort
153154
if (self.mjpegServerPortFromArguments != NSNotFound) {
154155
return self.mjpegServerPortFromArguments;
155156
}
156-
157+
157158
if (NSProcessInfo.processInfo.environment[@"MJPEG_SERVER_PORT"] &&
158159
[NSProcessInfo.processInfo.environment[@"MJPEG_SERVER_PORT"] length] > 0) {
159160
return [NSProcessInfo.processInfo.environment[@"MJPEG_SERVER_PORT"] integerValue];
@@ -235,7 +236,7 @@ + (NSUInteger)maxTypingFrequency
235236
if (nil == FBMaxTypingFrequency) {
236237
return [self defaultTypingFrequency];
237238
}
238-
return FBMaxTypingFrequency.integerValue <= 0
239+
return FBMaxTypingFrequency.integerValue <= 0
239240
? [self defaultTypingFrequency]
240241
: FBMaxTypingFrequency.integerValue;
241242
}
@@ -686,4 +687,14 @@ + (BOOL)includeMinMaxValueInPageSource
686687
return FBShouldIncludeMinMaxValueInPageSource;
687688
}
688689

690+
+ (void)setEnforceCustomSnapshots:(BOOL)enabled
691+
{
692+
FBShouldEnforceCustomSnapshots = enabled;
693+
}
694+
695+
+ (BOOL)enforceCustomSnapshots
696+
{
697+
return FBShouldEnforceCustomSnapshots;
698+
}
699+
689700
@end

WebDriverAgentLib/Utilities/FBSettings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,6 @@ extern NSString* const FB_SETTING_AUTO_CLICK_ALERT_SELECTOR;
4242
extern NSString *const FB_SETTING_INCLUDE_HITTABLE_IN_PAGE_SOURCE;
4343
extern NSString *const FB_SETTING_INCLUDE_NATIVE_FRAME_IN_PAGE_SOURCE;
4444
extern NSString *const FB_SETTING_INCLUDE_MIN_MAX_VALUE_IN_PAGE_SOURCE;
45+
extern NSString *const FB_SETTING_ENFORCE_CUSTOM_SNAPSHOTS;
4546

4647
NS_ASSUME_NONNULL_END

WebDriverAgentLib/Utilities/FBSettings.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@
3838
NSString* const FB_SETTING_INCLUDE_HITTABLE_IN_PAGE_SOURCE = @"includeHittableInPageSource";
3939
NSString* const FB_SETTING_INCLUDE_NATIVE_FRAME_IN_PAGE_SOURCE = @"includeNativeFrameInPageSource";
4040
NSString* const FB_SETTING_INCLUDE_MIN_MAX_VALUE_IN_PAGE_SOURCE = @"includeMinMaxValueInPageSource";
41+
NSString* const FB_SETTING_ENFORCE_CUSTOM_SNAPSHOTS = @"enforceCustomSnapshots";

WebDriverAgentLib/Utilities/FBXPath.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,12 @@ + (int)writeXmlWithRootElement:(id<FBXCElementSnapshot>)root
567567
return (id<FBXCElementSnapshot>)root;
568568
}
569569

570+
// https://github.com/appium/appium-xcuitest-driver/pull/2565
570571
if (useNative) {
571572
return [(XCUIElement *)root fb_nativeSnapshot];
572573
}
573-
return [root isKindOfClass:XCUIApplication.class]
574+
// https://github.com/appium/WebDriverAgent/issues/1085
575+
return [root isKindOfClass:XCUIApplication.class] && !FBConfiguration.enforceCustomSnapshots
574576
? [(XCUIElement *)root fb_standardSnapshot]
575577
: [(XCUIElement *)root fb_customSnapshot];
576578
}

0 commit comments

Comments
 (0)