Using Environment Variables in Coded UI Test with Visual Studio 2012
Posted by: Gouri Sohoni
in Category Visual Studio, VSTS & TFS
Abstract: In this article we will discuss why and how we can use environment variables while working with Coded UI Test (CUIT)
Many a times, the recording of the Coded UI Tests can be done in a development environment. While running the application on production environment or in the testing environment, the path for executable can be different. In these scenarios, the Coded UI Test recording may fail as the path to the windows application or WPF application is now different. It may also happen that the Web URL changes and again we encounter the same problem.
In these cases, using environment variables and launching the application is helpful. With this, we are not hard coding the launch path or the URL in the recording of CUIT. We will have to be careful in setting the environment variables and retrieving them while running the Coded UI Test.
I have created 2 environment variables. The first variable stores the complete path along with the executable file name for windows application. The second variable stores the URL to start with the browser.
It is recommended to create the environment variable from some other application, as it will be required to be done only once. There are 3 targets you can specify while creating the variables.
The targets can be machine, process or user.
With Machine, the environment variable is stored or retrieved from HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment key in the Windows operating system registry. This variable persists in the registry till it is programmatically deleted.
With Process, the variable is set or retrieved from the current process. This environment variable will be deleted with the termination of the process.
With User being the enum value, the environment variable is stored in HKEY_CURRENT_USER\Environment key in the Windows operating system registry. This variable will persist till the user programmatically deletes it.
We need the environment variables created from another application in the Coded UI Test project so we cannot use the target, which deletes the variable with the process stopping.
Here we are going to create a Coded UI Test project which has 2 methods, one to launch windows application and the other for browser.
Step 1: Create a Coded UI Test Project
Step 2: The recording with CUIT builder will not have the path of the windows application. The application will be started by taking the path from the environment variable and using the class ApplicationUnderTest to start the windows form.
ApplicationUnderTest is a class from the namespace Microsoft.VisualStudio.TestTools.UITesting. This class has a Launch method which helps in starting the application.
Step 3: You will observe that the Coded UI Test has recorded the operations after the Windows application has started.
Step 4: Similarly the launching of browser is not recorded with CUIT builder, but further operations are recorded with builder
Step 5: Observe the browser is launched by using BrowserWindow.Launch method and it is also ensured that it displays in maximize mode. The CUIT Builder has recorded actions only in the method DoOperations.
We can also ensure that the browser instance will be stopped even though we run Coded UI Test method many times.
With this we have parameterized certain things which otherwise would have been hard coded. We can use ApplicationUnderTest class to start Windows or WPF Application in multiple Coded UI Test methods.