DotNetCurry Logo

Using Environment Variables in Coded UI Test with Visual Studio 2012

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

target-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

codedui-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.

app-under-test

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.

coded-generate-code

Step 4: Similarly the launching of browser is not recorded with CUIT builder, but further operations are recorded with builder

record-operations

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.

Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+
Further Reading - Articles You May Like!
Author
Gouri Sohoni is a Trainer and Consultant for over two decades. She specializes in Visual Studio - Application Lifecycle Management (ALM) and Team Foundation Server (TFS). She is a Microsoft MVP in VS ALM, MCSD (VS ALM) and has conducted several corporate trainings and consulting assignments. She has also created various products that extend the capability of Team Foundation Server.


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
Madam,
       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
Madam,
       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
Thanks...