首页 > 解决方案 > C# - 进程对象未运行 cmd 命令

问题描述

我正在使用 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 属性可以解决问题。

标签: c#.netcmdserverwinappdriver

解决方案


尝试将/Kor/C标志添加到startInfo.Arguments. 这告诉cmd.exe运行以下命令,然后关闭(在 的情况下/C)或返回到 cmd 提示符(在 的情况下/K

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

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


推荐阅读