Skip to content

Commit

Permalink
Make it possible to start a session without starting an app (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Aug 4, 2019
1 parent e5d6c6d commit 0e8adcb
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 25 deletions.
31 changes: 16 additions & 15 deletions WebDriverAgentLib/Commands/FBSessionCommands.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ + (NSArray *)routes
+ (id<FBResponsePayload>)handleCreateSession:(FBRouteRequest *)request
{
NSDictionary *requirements = request.arguments[@"desiredCapabilities"];
NSString *bundleID = requirements[@"bundleId"];
NSString *appPath = requirements[@"app"];
if (!bundleID) {
return FBResponseWithErrorFormat(@"'bundleId' desired capability not provided");
}
[FBConfiguration setShouldUseTestManagerForVisibilityDetection:[requirements[@"shouldUseTestManagerForVisibilityDetection"] boolValue]];
if (requirements[@"shouldUseCompactResponses"]) {
[FBConfiguration setShouldUseCompactResponses:[requirements[@"shouldUseCompactResponses"] boolValue]];
Expand All @@ -106,19 +101,25 @@ + (NSArray *)routes

[FBConfiguration setShouldWaitForQuiescence:[requirements[@"shouldWaitForQuiescence"] boolValue]];

FBApplication *app = [[FBApplication alloc] initPrivateWithPath:appPath bundleID:bundleID];
app.fb_shouldWaitForQuiescence = FBConfiguration.shouldWaitForQuiescence;
app.launchArguments = (NSArray<NSString *> *)requirements[@"arguments"] ?: @[];
app.launchEnvironment = (NSDictionary <NSString *, NSString *> *)requirements[@"environment"] ?: @{};
[app launch];

if (app.processID == 0) {
return FBResponseWithErrorFormat(@"Failed to launch %@ application", bundleID);
NSString *bundleID = requirements[@"bundleId"];
FBApplication *app = nil;
if (bundleID != nil) {
app = [[FBApplication alloc] initPrivateWithPath:requirements[@"app"]
bundleID:bundleID];
app.fb_shouldWaitForQuiescence = FBConfiguration.shouldWaitForQuiescence;
app.launchArguments = (NSArray<NSString *> *)requirements[@"arguments"] ?: @[];
app.launchEnvironment = (NSDictionary <NSString *, NSString *> *)requirements[@"environment"] ?: @{};
[app launch];
if (app.processID == 0) {
return FBResponseWithErrorFormat(@"Failed to launch %@ application", bundleID);
}
}

if (requirements[@"defaultAlertAction"]) {
[FBSession sessionWithApplication:app defaultAlertAction:(id)requirements[@"defaultAlertAction"]];
[FBSession initWithApplication:app
defaultAlertAction:(id)requirements[@"defaultAlertAction"]];
} else {
[FBSession sessionWithApplication:app];
[FBSession initWithApplication:app];
}

return FBResponseWithObject(FBSessionCommands.sessionInformation);
Expand Down
4 changes: 2 additions & 2 deletions WebDriverAgentLib/Routing/FBSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extern NSString *const FBApplicationCrashedException;
@param application The application that we want to create session for
@return new session
*/
+ (instancetype)sessionWithApplication:(nullable FBApplication *)application;
+ (instancetype)initWithApplication:(nullable FBApplication *)application;

/**
Creates and saves new session for application with default alert handling behaviour
Expand All @@ -57,7 +57,7 @@ extern NSString *const FBApplicationCrashedException;
@param defaultAlertAction The default reaction to on-screen alert. Either 'accept' or 'dismiss'
@return new session
*/
+ (instancetype)sessionWithApplication:(nullable FBApplication *)application defaultAlertAction:(NSString *)defaultAlertAction;
+ (instancetype)initWithApplication:(nullable FBApplication *)application defaultAlertAction:(NSString *)defaultAlertAction;

/**
Kills application associated with that session and removes session
Expand Down
8 changes: 4 additions & 4 deletions WebDriverAgentLib/Routing/FBSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ @implementation FBSession
static FBSession *_activeSession;
+ (instancetype)activeSession
{
return _activeSession ?: [FBSession sessionWithApplication:nil];
return _activeSession ?: [FBSession initWithApplication:nil];
}

+ (void)markSessionActive:(FBSession *)session
Expand All @@ -89,7 +89,7 @@ + (instancetype)sessionWithIdentifier:(NSString *)identifier
return _activeSession;
}

+ (instancetype)sessionWithApplication:(FBApplication *)application
+ (instancetype)initWithApplication:(FBApplication *)application
{
FBSession *session = [FBSession new];
session.alertsMonitor = nil;
Expand All @@ -107,9 +107,9 @@ + (instancetype)sessionWithApplication:(FBApplication *)application
return session;
}

+ (instancetype)sessionWithApplication:(nullable FBApplication *)application defaultAlertAction:(NSString *)defaultAlertAction
+ (instancetype)initWithApplication:(nullable FBApplication *)application defaultAlertAction:(NSString *)defaultAlertAction
{
FBSession *session = [self.class sessionWithApplication:application];
FBSession *session = [self.class initWithApplication:application];
session.alertsMonitor = [[FBAlertsMonitor alloc] init];
session.alertsMonitor.delegate = (id<FBAlertsMonitorDelegate>)session;
session.alertsMonitor.application = FBApplication.fb_activeApplication;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ - (void)disabled_testAutoAcceptingOfAlerts
}

self.session = [FBSession
sessionWithApplication:FBApplication.fb_activeApplication
initWithApplication:FBApplication.fb_activeApplication
defaultAlertAction:@"accept"];
for (int i = 0; i < 2; i++) {
[self.testedApplication.buttons[FBShowAlertButtonName] fb_tapWithError:nil];
Expand All @@ -71,7 +71,7 @@ - (void)disabled_testAutoDismissingOfAlerts
}

self.session = [FBSession
sessionWithApplication:FBApplication.fb_activeApplication
initWithApplication:FBApplication.fb_activeApplication
defaultAlertAction:@"dismiss"];
for (int i = 0; i < 2; i++) {
[self.testedApplication.buttons[FBShowAlertButtonName] fb_tapWithError:nil];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ - (void)setUp
{
[super setUp];
[self launchApplication];
self.session = [FBSession sessionWithApplication:FBApplication.fb_activeApplication];
self.session = [FBSession initWithApplication:FBApplication.fb_activeApplication];
}

- (void)testSettingsAppCanBeOpenedInScopeOfTheCurrentSession
Expand Down
2 changes: 1 addition & 1 deletion WebDriverAgentTests/UnitTests/FBSessionTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ - (void)setUp
{
[super setUp];
self.testedApplication = (id)FBApplicationDouble.new;
self.session = [FBSession sessionWithApplication:self.testedApplication];
self.session = [FBSession initWithApplication:self.testedApplication];
}

- (void)testSessionFetching
Expand Down

0 comments on commit 0e8adcb

Please sign in to comment.