测试 运行 失败后终止所有 iexplore 进程

Killing all iexplore processes after test run fails

当我们 运行 我们的 CodedUI 测试和测试用例失败时,我们通过调用下面的 Kill() 终止 Internet Explorer 进程:

private static readonly HashSet<string> ProcessesToKill = 
    new HashSet<string>(new[] { "iexplore" });

public static void Kill()
{
    var runningProcessesToKill = (from p in Process.GetProcesses()
        where ProcessesToKill.Contains(p.ProcessName, 
            StringComparer.OrdinalIgnoreCase)
        select p).ToArray();

    // First try to close the process in a friendly way
    CloseProcess(runningProcessesToKill);

    // Then wait for a while to give the processes time to terminate
    WaitForProcess(runningProcessesToKill);

    // If not closed kill the process.
    KillProcess(runningProcessesToKill);
}

先在进程上调用CloseMainWindow()Close(),然后等待一段时间,然后在进程上调用Kill()来完成杀戮。

不幸的是,这不会关闭 JavaScript 警报弹出窗口。当测试 运行 完成后,它会保留在屏幕中以阻止下一个测试,如下所示:

为什么它不关闭警报,我们该如何解决这个问题?

您可以使用命令行中的 taskkill 命令进行暴力破解:

C:\>taskkill /F /IM iexplore.exe

当然,这对您的测试没有帮助。相反,使用 Process.Start(...):

public static void Kill()
{
    System.Diagnostics.Process.Start("taskkill", "/F /IM iexplore.exe");
}

这将关闭所有 Internet Explorer 进程,无论它们是否显示警告或确认对话框。

参考文献: