Using Environment Variables in Coded UI Test with Visual Studio 2012

Posted by: Gouri Sohoni , on 7/20/2013, in Category Visual Studio
Views: 32250
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.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Gouri is a Trainer and Consultant on Azure DevOps and Azure Development. She has an experience of three decades in software training and consulting. She is a graduate from Pune University and PGDCA from Pune University. Gouri is a Microsoft Most Valuable Professional (MVP)  - Developer Technologies (Azure DevOps), Microsoft Certified Trainer (MCT) and a Microsoft Certified Azure DevOps Engineer Expert. She has conducted over 150 corporate trainings on various Microsoft technologies. She is a speaker with Pune User Group and has conducted sessions on Azure DevOps, SQL Server Business Intelligence and Mobile Application Development. Gouri has written more than 75 articles on Azure DevOps, TFS, SQL Server Business Intelligence and SQL Azure which are published on and You can connect with her on LinkedIn.

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by sakthivel on Tuesday, May 20, 2014 5:04 AM
       I have one doubt in Coded UI. My Requirement is, need to call Coded UI from Windows Application and then need to call Coded UI Test Builder from  that Coded UI Environment. Is It possible?
Comment posted by sakthivel on Tuesday, May 20, 2014 5:35 AM
       I have one doubt in Coded UI. My Requirement is, need to call Coded UI from Windows Application and then need to call Coded UI Test Builder from  that Coded UI Environment. Is It possible?
Comment posted by Gouri Sohoni on Wednesday, May 21, 2014 11:47 PM
Hi sakthivel,

CUIT builder is available only from VS IDE.
Comment posted by sakthivel on Thursday, May 22, 2014 1:32 AM