Skip to content

Commit

Permalink
feat: Add a capability to customize the default state change timeout …
Browse files Browse the repository at this point in the history
…on app startup (#877)
  • Loading branch information
mykola-mokhnach authored Mar 28, 2024
1 parent 1b97df4 commit 98351c3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
71 changes: 52 additions & 19 deletions WebDriverAgentLib/Commands/FBSessionCommands.m
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,25 @@ + (NSArray *)routes
if (app.running) {
[app terminate];
}
NSError *openError;
if (![XCUIDevice.sharedDevice fb_openUrl:initialUrl
withApplication:bundleID
error:&openError]) {
NSString *errorMsg = [NSString stringWithFormat:@"Cannot open the URL %@ with the %@ application. Original error: %@",
initialUrl, bundleID, openError.localizedDescription];
return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:errorMsg traceback:nil]);
id<FBResponsePayload> errorResponse = [self openDeepLink:initialUrl
withApplication:bundleID
timeout:capabilities[FB_CAP_APP_LAUNCH_STATE_TIMEOUT_SEC]];
if (nil != errorResponse) {
return errorResponse;
}
} else {
NSTimeInterval defaultTimeout = _XCTApplicationStateTimeout();
if (nil != capabilities[FB_CAP_APP_LAUNCH_STATE_TIMEOUT_SEC]) {
_XCTSetApplicationStateTimeout([capabilities[FB_CAP_APP_LAUNCH_STATE_TIMEOUT_SEC] doubleValue]);
}
@try {
[app launch];
} @catch (NSException *e) {
return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:e.reason traceback:nil]);
} @finally {
if (nil != capabilities[FB_CAP_APP_LAUNCH_STATE_TIMEOUT_SEC]) {
_XCTSetApplicationStateTimeout(defaultTimeout);
}
}
}
if (!app.running) {
Expand All @@ -178,13 +184,11 @@ + (NSArray *)routes
}
} else if (appState == XCUIApplicationStateRunningBackground && !forceAppLaunch) {
if (nil != initialUrl) {
NSError *openError;
if (![XCUIDevice.sharedDevice fb_openUrl:initialUrl
withApplication:bundleID
error:&openError]) {
NSString *errorMsg = [NSString stringWithFormat:@"Cannot open the URL %@ with the %@ application. Original error: %@",
initialUrl, bundleID, openError.localizedDescription];
return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:errorMsg traceback:nil]);
id<FBResponsePayload> errorResponse = [self openDeepLink:initialUrl
withApplication:bundleID
timeout:nil];
if (nil != errorResponse) {
return errorResponse;
}
} else {
[app activate];
Expand All @@ -193,11 +197,11 @@ + (NSArray *)routes
}

if (nil != initialUrl && nil == bundleID) {
NSError *openError;
if (![XCUIDevice.sharedDevice fb_openUrl:initialUrl error:&openError]) {
NSString *errorMsg = [NSString stringWithFormat:@"Cannot open the URL %@. Original error: %@",
initialUrl, openError.localizedDescription];
return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:errorMsg traceback:nil]);
id<FBResponsePayload> errorResponse = [self openDeepLink:initialUrl
withApplication:nil
timeout:capabilities[FB_CAP_APP_LAUNCH_STATE_TIMEOUT_SEC]];
if (nil != errorResponse) {
return errorResponse;
}
}

Expand Down Expand Up @@ -492,4 +496,33 @@ + (NSDictionary *)currentCapabilities
};
}

+(nullable id<FBResponsePayload>)openDeepLink:(NSString *)initialUrl
withApplication:(nullable NSString *)bundleID
timeout:(nullable NSNumber *)timeout
{
NSError *openError;
NSTimeInterval defaultTimeout = _XCTApplicationStateTimeout();
if (nil != timeout) {
_XCTSetApplicationStateTimeout([timeout doubleValue]);
}
@try {
BOOL result = nil == bundleID
? [XCUIDevice.sharedDevice fb_openUrl:initialUrl
error:&openError]
: [XCUIDevice.sharedDevice fb_openUrl:initialUrl
withApplication:(id)bundleID
error:&openError];
if (result) {
return nil;
}
NSString *errorMsg = [NSString stringWithFormat:@"Cannot open the URL %@ with the %@ application. Original error: %@",
initialUrl, bundleID ?: @"default", openError.localizedDescription];
return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:errorMsg traceback:nil]);
} @finally {
if (nil != timeout) {
_XCTSetApplicationStateTimeout(defaultTimeout);
}
}
}

@end
4 changes: 3 additions & 1 deletion WebDriverAgentLib/Utilities/FBCapabilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

/** Whether to use alternative elements visivility detection method */
extern NSString* const FB_CAP_USE_TEST_MANAGER_FOR_VISIBLITY_DETECTION;
/** Set the maximum amount of charatcers that could be typed within a minute (60 by default) */
/** Set the maximum amount of characters that could be typed within a minute (60 by default) */
extern NSString* const FB_CAP_MAX_TYPING_FREQUENCY;
/** this setting was needed for some legacy stuff */
extern NSString* const FB_CAP_USE_SINGLETON_TEST_MANAGER;
Expand Down Expand Up @@ -42,3 +42,5 @@ extern NSString* const FB_CAP_ENVIRNOMENT;
extern NSString* const FB_CAP_USE_NATIVE_CACHING_STRATEGY;
/** Whether to enforce software keyboard presence on simulator */
extern NSString* const FB_CAP_FORCE_SIMULATOR_SOFTWARE_KEYBOARD_PRESENCE;
/** Sets the application state change timeout for the initial app startup */
extern NSString* const FB_CAP_APP_LAUNCH_STATE_TIMEOUT_SEC;
1 change: 1 addition & 0 deletions WebDriverAgentLib/Utilities/FBCapabilities.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
NSString* const FB_CAP_ENVIRNOMENT = @"environment";
NSString* const FB_CAP_USE_NATIVE_CACHING_STRATEGY = @"useNativeCachingStrategy";
NSString* const FB_CAP_FORCE_SIMULATOR_SOFTWARE_KEYBOARD_PRESENCE = @"forceSimulatorSoftwareKeyboardPresence";
NSString* const FB_CAP_APP_LAUNCH_STATE_TIMEOUT_SEC = @"appLaunchStateTimeoutSec";

0 comments on commit 98351c3

Please sign in to comment.