C#-进程对象未运行cmd命令

时间:2019-02-07 16:31:27

标签: c# .net cmd server winappdriver

我正在使用WinAppDriver以便在Excel上运行一些测试用例。我正在尝试通过代码启动服务器,以便不必在命令行中手动进行操作。我有以下代码-

public static void StartWinAppServer(int port) {
            Process process = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.WindowStyle = ProcessWindowStyle.Normal;
            startInfo.FileName = "cmd.exe";
            startInfo.WorkingDirectory = @"C:\Program Files (x86)\Windows Application Driver\";
            startInfo.Arguments = $"WinAppDriver {port}";
            process.StartInfo = startInfo;
            process.Start();
        }

这样的称呼-

public static WindowsDriver<WindowsElement> GetWindowsAppDriver (AppName appName) {
            string appID = string.Empty;

            StartWinAppServer(4723);
            switch(appName) {

                case AppName.Excel:
                    appID = @"C:\Program Files\Microsoft Office\root\Office16\Excel.exe";
                    break;
            }

            DesiredCapabilities appCapabilities = new DesiredCapabilities();
            appCapabilities.SetCapability("app", appID);

            return new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appCapabilities);
        }

此代码打开CMD,但未运行它。我在这里想念什么吗?我以为arguments属性可以解决问题。

1 个答案:

答案 0 :(得分:1)

尝试将/K/C标志添加到startInfo.Arguments。这告诉cmd.exe运行以下命令,然后关闭(对于/C而言)或返回到cmd提示符(对于/K而言)

startInfo.Arguments = $"/C WinAppDriver {port}";

https://ss64.com/nt/cmd.html