EVH671
EVH671

Reputation: 251

Coded UI Testing VS2012 - How to get an exit code from a running process?

I'm running a coded UI testing which needs to perform 6 phases. Each phase is run by pressing the compatible button. I'm starting the application which I run the Coded UI Test on , using: System.Diagnostics.Process.Start();

The TestMethod on which the Coded UI Test is run:

/// </summary>
        [TestMethod]
        public void CodedUITestMethod1()
        {
              System.Diagnostics.Process.Start(<Path of the application>);
              `enter code here`
        }

Within the TestMethod, I want to catch Exit Codes or Exceptions from the running process because if one of the 6 phases is failed, I want to stop this automation test.

How can I do it ?

Evh671.

Upvotes: 0

Views: 381

Answers (1)

kida
kida

Reputation: 501

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = <Path of the application>;
p.Start();

//or
//var p = System.Diagnostics.Process.Start(<Path of the application>);

...Do UI testing here...

p.WaitForExit();
if (p.ExitCode != 0)
{
    throw new System.Exception("Something something");
}

p.ErrorDataReceived event might work for catching exceptions. I have no experience with it tho.

void p_ErrorDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
    if (Playback.IsSessionStarted)
    {
        Playback.StopSession();
    }
    throw new System.Exception("Something something");
}

Upvotes: 1

Related Questions