Thursday, February 13, 2014

Distributing Coded UI Scripts (with Selenium add-in) for Cross Browser Automation using Visual Studio Lab Management & Environment variables

Author: Ranjit Gupta and Raj Kamal

Background

If you are not already aware, Coded UI now supports cross browser testing using Selenium components. The Visual Studio add-in can be found here, that works if you are running VS 2012 Update 2 or above. There is also an official blog that talk topic in detail, if you are interested.

Customer Story

A large utility service company in United Stated has retained us to implement its internet facing web presence   - USD 7 Million engagement. The website will provide timely, business driven information along with functionality to its customers to do their regular interactions, such as paying bills, checking historical usage data, turning on/off services, and others mentioned in the business requirements section. As it’s an external facing site, it needs to be supported on Firefox, Chrome as well as IE 8 & IE 9. With the size and the criticality of the application to customer’s core business, selective manual testing on non-IE browsers is not an acceptable approach.

The power of Coded UI + Selenium & Visual Studio Lab Management

We wrote Coded UI Tests used this Selenium add-in along with Visual Studio Lab feature to distribute our automated tests on multiple browsers. Our goal was to dedicate each OS + Browser combination a dedicated agent machine, so all our tests run in parallel on different browsers on pre-defined machines.

One challenge was that, there is no way in Visual Studio Lab settings to specify that a particular agent machine should be used for a specific browser e.g. Chrome, Firefox, IE 8, IE 9 etc.  We didn’t want to create multiple copies of coded UI test methods and hardcode them to run against specific configurations. We were looking for a solution that didn’t require us to write custom logic to solve this very problem and fortunately we could achieve this without any additional coding using the below proposed solution.

Solution – Simple yet elegant

To get past this issue, we made use of environment variables. The steps are explained below.

1.      Create environment variable on each agent machine and set its value as the desired browser name (IE8/IE9/Chrome/Firefox) against which you want to run your automated tests.

 

2.      Create a Test Initialize method and read the value of the environment variable before test starts on each agent machine. This will return the name of the browser that needs to be used for playing back automation.

 

 [TestInitialize]

        public  void Init()

        {

           App_Constants.browser= Environment.GetEnvironmentVariable("browser",

           EnvironmentVariableTarget.User);

           

 }

 

 

3.      Inside your test method, set BrowserWindow.CurrentBrowser value as App_Constants.browser which holds the browser name stored in environment variable

[TestMethod]

        public void CodedUITestMethod1()

        {

            BrowserWindow.CurrentBrowser = App_Constants.browser;

            this.UIMap.RecordedMethod1();

            this.UIMap.AssertMethod1();           

           

    }

4.      Depending on the value stored in the environment variable, Coded UI launches the browser and runs the test against launched browser.

 


 


 

You can set these configuration as default = Yes, if you want these to be used for the test cases to bed added to the test plan

 

6.      Associate your automated tests with test cases in MTM (Microsoft Test Manager)

 

7.      Setup test controller and test agents

                       

8.      Run your automated test against each agent

 

Now you can control which OS/Browser you would like to use to run your automated tests without making any changes in your Coded UI Tests or writing any extra code.

 

5 comments:

Meenakshi Garg said...

Is it possible to create automate scripts to test on all browsers using Hard code approach, As we do not want to follow Record & playback approach. it has its many drawbacks. Please suggest me if it possible to create automated script using hard code approach.

Meenakshi Garg said...

Is it possible to create automate scripts to test on all browsers using Hard code approach, As we do not want to follow Record & playback approach. it has its many drawbacks. Please suggest me if it possible to create automated script using hard code approach.

Raj said...

Yes Meenakshi, you can use hand coding approach as well if you like and there are not drawbacks

Anonymous said...

Hand-coded is the only way to go. I blog about it: https://burdettelamar.wordpress.com/tag/cuit

Rakesh Chauhan said...

Does coded ui support execution on mac and devices