Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add a capability to customize the default state change timeout on app startup #877

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
Loading