diff --git a/WebDriverAgentLib/Commands/FBSessionCommands.m b/WebDriverAgentLib/Commands/FBSessionCommands.m index da01f6bb1..ded0d6691 100644 --- a/WebDriverAgentLib/Commands/FBSessionCommands.m +++ b/WebDriverAgentLib/Commands/FBSessionCommands.m @@ -30,6 +30,7 @@ static NSString* const KEYBOARD_AUTOCORRECTION = @"keyboardAutocorrection"; static NSString* const KEYBOARD_PREDICTION = @"keyboardPrediction"; static NSString* const SNAPSHOT_TIMEOUT = @"snapshotTimeout"; +static NSString* const USE_FIRST_MATCH = @"useFirstMatch"; @implementation FBSessionCommands @@ -220,7 +221,8 @@ + (NSArray *)routes SCREENSHOT_QUALITY: @([FBConfiguration screenshotQuality]), KEYBOARD_AUTOCORRECTION: @([FBConfiguration keyboardAutocorrection]), KEYBOARD_PREDICTION: @([FBConfiguration keyboardPrediction]), - SNAPSHOT_TIMEOUT: @([FBConfiguration snapshotTimeout]) + SNAPSHOT_TIMEOUT: @([FBConfiguration snapshotTimeout]), + USE_FIRST_MATCH: @([FBConfiguration useFirstMatch]), } ); } @@ -258,6 +260,9 @@ + (NSArray *)routes if ([settings objectForKey:SNAPSHOT_TIMEOUT]) { [FBConfiguration setSnapshotTimeout:[[settings objectForKey:SNAPSHOT_TIMEOUT] doubleValue]]; } + if ([settings objectForKey:USE_FIRST_MATCH]) { + [FBConfiguration setUseFirstMatch:[[settings objectForKey:USE_FIRST_MATCH] boolValue]]; + } return [self handleGetSettings:request]; } diff --git a/WebDriverAgentLib/Utilities/FBConfiguration.h b/WebDriverAgentLib/Utilities/FBConfiguration.h index 1a7d46404..522f5fc21 100644 --- a/WebDriverAgentLib/Utilities/FBConfiguration.h +++ b/WebDriverAgentLib/Utilities/FBConfiguration.h @@ -137,6 +137,16 @@ NS_ASSUME_NONNULL_BEGIN + (void)setSnapshotTimeout:(NSTimeInterval)timeout; + (NSTimeInterval)snapshotTimeout; +/** + * Whether to use fast search result matching while searching for elements. + * By default this is disabled due to https://github.com/appium/appium/issues/10101 + * but it still makes sense to enable it for views containing large counts of elements + * + * @param enabled Either YES or NO + */ ++ (void)setUseFirstMatch:(BOOL)enabled; ++ (BOOL)useFirstMatch; + @end NS_ASSUME_NONNULL_END diff --git a/WebDriverAgentLib/Utilities/FBConfiguration.m b/WebDriverAgentLib/Utilities/FBConfiguration.m index 61131b3ba..83abf71f4 100644 --- a/WebDriverAgentLib/Utilities/FBConfiguration.m +++ b/WebDriverAgentLib/Utilities/FBConfiguration.m @@ -38,6 +38,7 @@ static NSUInteger FBScreenshotQuality = 1; static NSUInteger FBMjpegScalingFactor = 100; static NSTimeInterval FBSnapshotTimeout = 15.; +static BOOL FBShouldUseFirstMatch = NO; @implementation FBConfiguration @@ -263,6 +264,16 @@ + (NSTimeInterval)snapshotTimeout return FBSnapshotTimeout; } ++ (void)setUseFirstMatch:(BOOL)enabled +{ + FBShouldUseFirstMatch = enabled; +} + ++ (BOOL)useFirstMatch +{ + return FBShouldUseFirstMatch; +} + #pragma mark Private + (BOOL)keyboardsPreference:(nonnull NSString *)key diff --git a/WebDriverAgentLib/Utilities/FBXCodeCompatibility.m b/WebDriverAgentLib/Utilities/FBXCodeCompatibility.m index e55ce6aeb..6815a658e 100644 --- a/WebDriverAgentLib/Utilities/FBXCodeCompatibility.m +++ b/WebDriverAgentLib/Utilities/FBXCodeCompatibility.m @@ -9,6 +9,7 @@ #import "FBXCodeCompatibility.h" +#import "FBConfiguration.h" #import "FBErrorBuilder.h" #import "FBLogger.h" #import "XCUIElementQuery.h" @@ -82,21 +83,11 @@ - (NSUInteger)fb_state @end -static BOOL FBShouldUseFirstMatchSelector = NO; -static dispatch_once_t onceFirstMatchToken; - @implementation XCUIElementQuery (FBCompatibility) - (XCUIElement *)fb_firstMatch { - dispatch_once(&onceFirstMatchToken, ^{ - // Unfortunately, firstMatch property does not work properly if - // the lookup is not executed in application context: - // https://github.com/appium/appium/issues/10101 - // FBShouldUseFirstMatchSelector = [self respondsToSelector:@selector(firstMatch)]; - FBShouldUseFirstMatchSelector = NO; - }); - if (FBShouldUseFirstMatchSelector) { + if (FBConfiguration.useFirstMatch) { XCUIElement* result = self.firstMatch; return result.exists ? result : nil; }