locked
Working on Cmd Prompt window using C# RRS feed

  • Question

  • Hi,

    As a part pf my project, I need to work provide some commands in command prompt.

    I could open the cmd Prompt in UI but couldnot identify the objects inside the cmd prompt.

    Kindly help me how to send commands to command prompt using coded UI

    Monday, January 25, 2016 2:10 PM

Answers

  • Thanks for mihcom90's reply.

    Hi Isnsuresh,

    According to your description, you want to open browser from Command Prompt window with C# code in Coded UI Test, is it right?

    After testing your sample code, I modify it as below code show, it can open the browser from Command Prompt windows now. Please have a try. If any questions, please feel free to let me know.

    ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", @"cd c:\");
    procStartInfo.WorkingDirectory = @"C:\";
    procStartInfo.Arguments = "\"" + @"C:\Program Files (x86)\Internet Explorer" + "\"";
    procStartInfo.RedirectStandardOutput = true;
    procStartInfo.RedirectStandardInput = true;
    procStartInfo.UseShellExecute = false;
    Process proc = new Process();
    proc.StartInfo = procStartInfo;
    proc.Start();
    proc.StandardInput.WriteLine("\"" + @"C:\Program Files (x86)\Internet Explorer\iexplore.exe" + "\"");
    proc.WaitForExit();


    Best Regards,
    Weiwei

    • Marked as answer by lsnsuresh Wednesday, January 27, 2016 2:07 PM
    Wednesday, January 27, 2016 3:00 AM

All replies

  • You don't need to use coded ui tests to run cmd commands. Just start a cmd process: http://stackoverflow.com/questions/1469764/run-command-prompt-commands
    Tuesday, January 26, 2016 6:36 AM
  • Hi,

    I used similar kind of code to open Cmd Prompt and trying to open IE browser.On exceuting the code, the TC getting passed  but IE is not getting opened. Also cmd Prompt window getting closed within a fraction of second.So I couldnoyt check whether the command is executed or not. Can you Please help me on this.

    ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/D");
           procStartInfo.WorkingDirectory = @"C:\Program Files (x86)\Internet Explorer";
           procStartInfo.Arguments = "/c"+"iexplore.exe{ENTER}";
           procStartInfo.RedirectStandardOutput = true;
           procStartInfo.RedirectStandardInput = true;
           procStartInfo.UseShellExecute = false;
           Process proc = new Process();
           proc.StartInfo = procStartInfo;
           proc.Start();
          // proc.StandardInput.WriteLine( @"C:\Program Files (x86)\Internet Explorer\iexplore.exe");
          // proc.StandardInput.WriteLine("+/c"+"ENTER");
           proc.WaitForExit();
           string result = proc.StandardOutput.ReadToEnd();
             Console.WriteLine(result);

    Tuesday, January 26, 2016 7:58 PM
  • I feel like I'm missing something. I don't understand why you have to test the command prompt which is NOT your application or part of your application. Maybe you want to test the effects of the command, which in this case would be checking if a Internet Explorer process is running (to get all the running processes use Process.GetProcesses()). Additionally, if you need to open the browser window you could start the Internet Explorer process directly.
    Tuesday, January 26, 2016 8:04 PM
  • Thanks for mihcom90's reply.

    Hi Isnsuresh,

    According to your description, you want to open browser from Command Prompt window with C# code in Coded UI Test, is it right?

    After testing your sample code, I modify it as below code show, it can open the browser from Command Prompt windows now. Please have a try. If any questions, please feel free to let me know.

    ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", @"cd c:\");
    procStartInfo.WorkingDirectory = @"C:\";
    procStartInfo.Arguments = "\"" + @"C:\Program Files (x86)\Internet Explorer" + "\"";
    procStartInfo.RedirectStandardOutput = true;
    procStartInfo.RedirectStandardInput = true;
    procStartInfo.UseShellExecute = false;
    Process proc = new Process();
    proc.StartInfo = procStartInfo;
    proc.Start();
    proc.StandardInput.WriteLine("\"" + @"C:\Program Files (x86)\Internet Explorer\iexplore.exe" + "\"");
    proc.WaitForExit();


    Best Regards,
    Weiwei

    • Marked as answer by lsnsuresh Wednesday, January 27, 2016 2:07 PM
    Wednesday, January 27, 2016 3:00 AM
  • @mihcom90 Thanks for your reply. I am trying to explore coded UI and C#.As Weiwei Cai said,I want to open browser from Command Prompt window with C# code in Coded UI Test.

    @WeiWei. Thanks For Your help. the code ran sucessfully and IE got opened. really thank You for your immediate reply.

    I have few queries here.

    1.Do we need the ProcessStartInfo.Arguments(3rd line in the code) ?.The standardInput also does the same functionality. I commented the 3rd line and executed the code. It worked . Can You help me in understanding the difference between the two.

    2.WaitForExit() waits for so long time. Whats the maximum time does it waits for?

    3.Also I want to execute some commands in command window.I sent the commands through standardIndput.WriteLine method. I am not sure whether the cmd got executed or not as the cmd Window closes very soon

    proc.StandardInput.WriteLine("\""+ @"cmmast -u"+ "\"");.





     
    • Edited by lsnsuresh Wednesday, January 27, 2016 7:26 PM
    Wednesday, January 27, 2016 2:22 PM
  • Hi Isnsuresh,

    >>1.Do we need the ProcessStartInfo.Arguments(3rd line in the code) ?

    If you just one command line needed to run, we needn't the ProcessStartInfo.Arguments. ProcessStartInfo.Arguments is used to get or set the set of command-line arguments to use when starting the application. StandardInput is used to get a stream used to write the input of the application.

    >>2.WaitForExit() waits for so long time. Whats the maximum time does it waits for?

    In my opinion, if you don't assign how long it waiting, it will be closed by users. If you want it closed by itself, please set a number. For example, if you want it exit after 5 seconds, please set the parameter as 5000.

    proc.WaitForExit(5000);

    >>I am not sure whether the cmd got executed or not as the cmd Window closes very soon

    To verify whether command executed, I'm afraid you need to check by your selft what's the function to do based on your command. If you want to open some services, please check the services on Services window to check whether opened.

    If any other questions, please create a new thread since we often one question in one thread, which can let more communities join into to help you resolve your problem.

    Best Regards,
    Weiwei


    Thursday, January 28, 2016 2:52 AM
  • Thanks.. That helps a lot..
    Thursday, January 28, 2016 1:15 PM