From 3da03195feca88cde696c611c9a91e37bcc5e3c2 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Thu, 19 Sep 2024 14:50:02 +0200 Subject: [PATCH] feat: Add an endpoint to list active app candidates --- WebDriverAgentLib/Commands/FBCustomCommands.m | 60 ++++++++----------- 1 file changed, 24 insertions(+), 36 deletions(-) diff --git a/WebDriverAgentLib/Commands/FBCustomCommands.m b/WebDriverAgentLib/Commands/FBCustomCommands.m index 2ab039e6b..c9834f35b 100644 --- a/WebDriverAgentLib/Commands/FBCustomCommands.m +++ b/WebDriverAgentLib/Commands/FBCustomCommands.m @@ -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:)], @@ -186,47 +188,33 @@ + (NSArray *)routes + (id)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)handleActiveAppCandidatesInfo:(FBRouteRequest *)request { - // Can be nil if no active activation is defined by XCTest - if (app == nil) { - return @{}; + NSArray *apps = XCUIApplication.fb_activeApplications; + XCUIApplication *activeApp = request.session.activeApplication ?: XCUIApplication.fb_activeApplication; + pid_t activeAppPid = nil == activeApp ? 0 : activeApp.processID; + NSMutableArray *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 ?: @{} + } }; }