Search code examples
c#.netcmdserverwinappdriver

C# - Process object not running cmd command


I'm using WinAppDriver in order to run some test cases on Excel. I'm trying to start the server through the code so that I don't have to manually do it in command line. I have the following code-

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();
        }

Which is called like this-

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);
        }

This code opens up the CMD but isn't running it. Am I missing something here? I thought the arguments property would've done the trick.


Solution

  • Try adding the /K or /C flag to startInfo.Arguments. This tells cmd.exe to run the following command and then close (in the case of /C) or return to the cmd prompt (in the case of /K)

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

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