Thursday, September 8, 2011

Invoking *.bat file from Coded UI

Problem Statement: Need to invoke a "xyz.bat" file from a Coded UI test. The xyz.bat file runs as a child process with Visual Studio as its parent. As soon as the test ends, this child process is killed. Below we will see how can we keep this "xyz.bat" file from getting killed even after the test ends.

Solution: Instead of using "ApplicationUnderTest.Launch() " to invoke the "xyz.bat" file we can use a workaround. There are plenty of tools available on the web to convert a bat file to an exe. One of such tools can be used to convert the "xyz.bat" to "xyz.exe". After we get this  "xyz.exe" we can use the code snippet below to invoke this exe:

/***** Invoke the exe *****/
Process.Start("<path_to_exe>\\xyz.exe");
/**********************/

In this case even when the test has ended executing, our "xyz.exe" will still be running.
:)