Skip to content
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
182 changes: 73 additions & 109 deletions src/TestUtils/src/UITest.Appium/Actions/AppiumLifecycleActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,95 +221,71 @@ CommandResponse ForceCloseApp(IDictionary<string, object> parameters)
{
var appId = _app.GetAppId();
var testDevice = _app.GetTestDevice();
var udid = testDevice == TestDevice.iOS
? _app.Config.GetProperty<string>("Udid")
: null;

if (testDevice == TestDevice.iOS)
{
var udid = _app.Config.GetProperty<string>("Udid");
if (!string.IsNullOrEmpty(udid))
{
Debug.WriteLine($">>>>> ForceCloseApp: xcrun simctl terminate {udid} {appId}");
using var process = Process.Start(new ProcessStartInfo
{
FileName = "xcrun",
ArgumentList = { "simctl", "terminate", udid, appId },
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
});
if (process is not null && !process.WaitForExit(10000))
{
Debug.WriteLine(">>>>> ForceCloseApp: xcrun simctl terminate timed out, killing process");
try { process.Kill(); } catch { }
}
return CommandResponse.SuccessEmptyResponse;
}
else
{
Debug.WriteLine(">>>>> ForceCloseApp: iOS UDID not available, cannot force-terminate via simctl");
}
}
else if (testDevice == TestDevice.Android)
{
Debug.WriteLine($">>>>> ForceCloseApp: adb shell am force-stop {appId}");
using var process = Process.Start(new ProcessStartInfo
{
FileName = "adb",
ArgumentList = { "shell", "am", "force-stop", appId },
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
});
if (process is not null && !process.WaitForExit(10000))
{
Debug.WriteLine(">>>>> ForceCloseApp: adb force-stop timed out, killing process");
try { process.Kill(); } catch { }
}
return CommandResponse.SuccessEmptyResponse;
}
else if (testDevice == TestDevice.Mac)
{
// Use pkill -9 instead of osascript "tell app to quit" because a hung app
// won't process the cooperative Apple Event from osascript.
Debug.WriteLine($">>>>> ForceCloseApp: macOS force-kill for {appId}");
using var process = Process.Start(new ProcessStartInfo
{
FileName = "pkill",
ArgumentList = { "-9", "-f", appId },
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
});
if (process is not null && !process.WaitForExit(10000))
{
Debug.WriteLine(">>>>> ForceCloseApp: pkill timed out, killing process");
try { process.Kill(); } catch { }
}
return CommandResponse.SuccessEmptyResponse;
}
else if (testDevice == TestDevice.Windows)
{
Debug.WriteLine($">>>>> ForceCloseApp: Windows taskkill for {appId}");
using var process = Process.Start(new ProcessStartInfo
{
FileName = "taskkill",
ArgumentList = { "/F", "/IM", $"{appId}.exe" },
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
});
if (process is not null && !process.WaitForExit(10000))
{
Debug.WriteLine(">>>>> ForceCloseApp: taskkill timed out, killing process");
try { process.Kill(); } catch { }
}
return CommandResponse.SuccessEmptyResponse;
}
return ForceClose(testDevice, appId, udid, AppiumProcessRunner.Run);
}
catch (Exception ex)
{
Debug.WriteLine($">>>>> ForceCloseApp failed: {ex.Message}");
Debug.WriteLine($">>>>> ForceCloseApp failed: {ex.GetType().Name}");
}

return CommandResponse.FailedEmptyResponse;
}

internal static CommandResponse ForceClose(TestDevice testDevice, string appId, string? udid,
Func<AppiumProcessCommand, AppiumProcessResult> runProcess)
{
AppiumProcessCommand command;
var timeout = TimeSpan.FromSeconds(10);

switch (testDevice)
{
case TestDevice.iOS when !string.IsNullOrWhiteSpace(udid):
command = new AppiumProcessCommand(
"xcrun",
["simctl", "terminate", udid, appId],
timeout,
"xcrun simctl terminate");
break;
case TestDevice.iOS:
Debug.WriteLine(">>>>> ForceCloseApp: iOS UDID not available, cannot force-terminate via simctl");
return CommandResponse.FailedEmptyResponse;
case TestDevice.Android:
command = new AppiumProcessCommand(
"adb",
["shell", "am", "force-stop", appId],
timeout,
"adb shell am force-stop");
break;
case TestDevice.Mac:
// A hung app will not process the cooperative Apple Event sent by osascript.
command = new AppiumProcessCommand(
"pkill",
["-9", "-f", appId],
timeout,
"pkill");
break;
case TestDevice.Windows:
command = new AppiumProcessCommand(
"taskkill",
["/F", "/IM", $"{appId}.exe"],
timeout,
"taskkill");
break;
default:
Debug.WriteLine($">>>>> ForceCloseApp: unsupported test device {testDevice}");
return CommandResponse.FailedEmptyResponse;
}

Debug.WriteLine($">>>>> ForceCloseApp: {command.Description}");
var result = runProcess(command);
if (result.Succeeded)
return CommandResponse.SuccessEmptyResponse;

LogProcessFailure(command.Description, result);
return CommandResponse.FailedEmptyResponse;
}

Expand Down Expand Up @@ -392,38 +368,26 @@ static void RunAdb(params string[] args)
{
try
{
var psi = new ProcessStartInfo
{
FileName = "adb",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
};
foreach (var a in args)
psi.ArgumentList.Add(a);

using var process = Process.Start(psi);
if (process is not null)
{
// Drain stdout/stderr so a chatty command (e.g. `adb shell monkey`) can't fill
// the pipe buffer and deadlock WaitForExit. The output is unused, so discard it.
process.OutputDataReceived += static (_, _) => { };
process.ErrorDataReceived += static (_, _) => { };
process.BeginOutputReadLine();
process.BeginErrorReadLine();
if (!process.WaitForExit(15000))
{
Debug.WriteLine($">>>>> RunAdb timed out: adb {string.Join(' ', args)}");
try { process.Kill(); } catch { /* best effort */ }
}
}
var command = new AppiumProcessCommand(
"adb",
args,
TimeSpan.FromSeconds(15),
"RunAdb");
var result = AppiumProcessRunner.Run(command);
if (!result.Succeeded)
LogProcessFailure(command.Description, result);
}
catch (Exception ex)
{
Debug.WriteLine($">>>>> RunAdb failed (adb {string.Join(' ', args)}): {ex.Message}");
Debug.WriteLine($">>>>> RunAdb failed: {ex.GetType().Name}");
}
}

static void LogProcessFailure(string command, AppiumProcessResult result)
{
Debug.WriteLine($">>>>> {command} failed: {result.DescribeFailure()}");
}

CommandResponse Back(IDictionary<string, object> parameters)
{
if (_app?.Driver is null)
Expand Down
Loading
Loading