Skip to content

Commit

Permalink
feat: Add an endpoint to list active app candidates
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Sep 19, 2024
1 parent 9fd58b6 commit 3da0319
Showing 1 changed file with 24 additions and 36 deletions.
60 changes: 24 additions & 36 deletions WebDriverAgentLib/Commands/FBCustomCommands.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ + (NSArray *)routes
[[FBRoute GET:@"/wda/screen"].withoutSession respondWithTarget:self action:@selector(handleGetScreen:)],
[[FBRoute GET:@"/wda/activeAppInfo"] respondWithTarget:self action:@selector(handleActiveAppInfo:)],
[[FBRoute GET:@"/wda/activeAppInfo"].withoutSession respondWithTarget:self action:@selector(handleActiveAppInfo:)],
[[FBRoute GET:@"/wda/activeAppCandidatesInfo"] respondWithTarget:self action:@selector(handleActiveAppCandidatesInfo:)],
[[FBRoute GET:@"/wda/activeAppCandidatesInfo"].withoutSession respondWithTarget:self action:@selector(handleActiveAppCandidatesInfo:)],
#if !TARGET_OS_TV // tvOS does not provide relevant APIs
[[FBRoute POST:@"/wda/setPasteboard"] respondWithTarget:self action:@selector(handleSetPasteboard:)],
[[FBRoute POST:@"/wda/setPasteboard"].withoutSession respondWithTarget:self action:@selector(handleSetPasteboard:)],
Expand Down Expand Up @@ -186,47 +188,33 @@ + (NSArray *)routes
+ (id<FBResponsePayload>)handleActiveAppInfo:(FBRouteRequest *)request
{
XCUIApplication *app = request.session.activeApplication ?: XCUIApplication.fb_activeApplication;
return FBResponseWithObject(@{
@"pid": @(app.processID),
@"bundleId": app.bundleID,
@"name": app.identifier,
@"processArguments": [self processArguments:app],
});
return FBResponseWithObject([self appInfoWithApp:app]);
}

/**
* Returns current active app and its arguments of active session
*
* @return The dictionary of current active bundleId and its process/environment argumens
*
* @example
*
* [self currentActiveApplication]
* //=> {
* // "processArguments" : {
* // "env" : {
* // "HAPPY" : "testing"
* // },
* // "args" : [
* // "happy",
* // "tseting"
* // ]
* // }
*
* [self currentActiveApplication]
* //=> {}
*/
+ (NSDictionary *)processArguments:(XCUIApplication *)app
+ (id<FBResponsePayload>)handleActiveAppCandidatesInfo:(FBRouteRequest *)request
{
// Can be nil if no active activation is defined by XCTest
if (app == nil) {
return @{};
NSArray<XCUIApplication *> *apps = XCUIApplication.fb_activeApplications;
XCUIApplication *activeApp = request.session.activeApplication ?: XCUIApplication.fb_activeApplication;
pid_t activeAppPid = nil == activeApp ? 0 : activeApp.processID;
NSMutableArray<NSDictionary *> *result = [NSMutableArray array];
for (XCUIApplication *app in apps) {
NSMutableDictionary *info = [self appInfoWithApp:app].mutableCopy;
info[@"isActive"] = @(app.processID == activeAppPid);
[result addObject:info.copy];
}
return FBResponseWithObject(result.copy);
}

return
@{
@"args": app.launchArguments,
@"env": app.launchEnvironment
+ (NSDictionary *)appInfoWithApp:(XCUIApplication *)app
{
return (nil == app) ? @{} : @{
@"pid": @(app.processID),
@"bundleId": app.bundleID,
@"name": app.identifier,
@"processArguments": @{
@"args": app.launchArguments ?: @[],
@"env": app.launchEnvironment ?: @{}
}
};
}

Expand Down

0 comments on commit 3da0319

Please sign in to comment.