Partager via


How To: Get same test running under different environments

A while back, I got following question from a user of Coded UI Test 

We want to run the same test across multiple environments to ensure everything is still working correctly.

Dev-> Test -> UAT -> STAGE

We want to run these tests after each deploy to each environment. Each environment has as a different URL, username and password.

How can I achieve this without changing the recording or code everytime?

Well, there are number of different ways to achieve this.  One is to use environment variables to parameterize the playback code.  Let us take an example – my test is to login to Hotmail. Here I want to parameterize 3 variables – the Hotmail server, the username and the password.  I can do so by adding something like the code below to UIMap.cs file.

 public UIMap()
{
    // Bind the parameters using Environment Variables.
    string testServer =
        Environment.GetEnvironmentVariable("TestServer");
    string testUserName =
        Environment.GetEnvironmentVariable("TestUserName");
    string testUserPassword =
        Environment.GetEnvironmentVariable("TestUserPassword");

    if (!string.IsNullOrEmpty(testServer))
    {
        // Change only the server name part of the URL as
        // rest is independent of the environment.
        UriBuilder urlBuilder =
            new UriBuilder(this.LoginToHotmailParams.Url);
        urlBuilder.Host = testServer;
        this.LoginToHotmailParams.Url = urlBuilder.Uri.ToString();
    }

    if (!string.IsNullOrEmpty(testUserName))
    {
        this.LoginToHotmailParams.UIWindowsLiveIDEditText =
            testUserName;
    }

    if (!string.IsNullOrEmpty(testUserPassword))
    {
        // Password is always encrypted by the recorder.
        // So encrypt the clear text password before setting.
        this.LoginToHotmailParams.UIPasswordEditPassword =
            Playback.EncryptText(testUserPassword);
    }
}

Generally, only the server name part of the URL changes across environments and hence I have used just that to parameterize.

For password, you could have clear text & set it via Playback.EncryptText() API shown above OR, you could have encrypted password and use it directly.  For encryption you can write separate program that gives you encrypted one from clear text using the Playback.EncryptText() API. Note that how secure encrypted password is depends on the key used for encryption.  The encryption key can be set using Playback.PlaybackSettings.SetEncryptionKeyLocation() API.  If you don’t call this API, the default key is used which makes encryption not really unbreakable.  Most users are typically ok with using default encryption key because these are test accounts and “everybody already know the password”.  However, if you really want to ensure that the test account password is protected, use different encryption key that is placed on a network share and the access to the network share is controlled via Windows.

Comments

  • Anonymous
    August 25, 2010
    What are recommendations on how to handle multiple roles of an application. e.g. In a scenario a User1 is requesting for a leave (here IE will get opened with first user session). Now during playback we need to get IE opened with Manager user to approve his leave request. So what are the standards set for automating such flows... I would be using Test Agent to play the tests(and agent also runs under one user credential and launches IE also under same user account.

  • Anonymous
    September 07, 2010
    The comment has been removed

  • Anonymous
    September 08, 2010
    It may not be the exact same thing.  If you cannot still figure which one, paste your recording and I will take a look at this. Thanks.

  • Anonymous
    September 08, 2010
    Nice work. I'm finally getting the hang of coded ui architecture and how things work. I needed this post. Thanks

  • Anonymous
    December 26, 2010
    Hi, I had a query here. I can use something like this to generate the encrypted password            String encrypted = Playback.EncryptText("Toral"); But in any case, I will be exposing my password in the code. Is there a way - I dont use the real password at all.

  • Anonymous
    December 27, 2010
    In the above example, password is got from environment variable.  You can similarly read it from some file off a network share.  You can use Windows ACL to control access to that network sharefile. Alternatively, you can also change the encryption key used by the Playback for encrypting the text by PlaybackSettings.SetEncryptionKeyLocation() method.  Here again, you can have the key file on a network share and use Windows to control the access to specified users only. Thanks.

  • Anonymous
    February 08, 2011
    The comment has been removed

  • Anonymous
    February 11, 2011
    I am not sure I follow you completely.  Please make a detailed post at social.msdn.microsoft.com/.../threads and someone from the team will pick it up.

  • Anonymous
    May 09, 2011
    Hi I'm looking for testing a locale application with the CUIT.  I got about 5 physical machines, but with different hardware. Some of my test can only be done on specific hardware. So how can i manage that these tests are only sent to the agents running on this machine. If needed, I can set attributes for the agents in the controllers config. Is there a way?

  • Anonymous
    May 09, 2011
    Check msdn.microsoft.com/.../ff426235.aspx.  Ignore the fact that it says this is for Load Test.

  • Anonymous
    June 16, 2011
    Hi Gowtham, Thanks a lot for the post. The post resembles exactly my situation. I tries you rsolution,but its not working for me. i added code in datadriven test for password it is allwoing to enter in textbox ,but not signing in to website. could you help me out in this.

  • Anonymous
    June 16, 2011
    Share the code - send mail at gautamg at the rate microsoft dot com. Thanks.

  • Anonymous
    July 28, 2011
    I have  a csv file called pwd.csv with column name as passwrd. How can I incorporate password encryption on this one? Thanks in advance!

  • Anonymous
    July 29, 2011
    Check blogs.msdn.com/.../data-driving-coded-ui-tests.aspx and blogs.msdn.com/.../more-on-data-driving-coded-ui-tests.aspx for data driving.  You can use that in conjunction with the above blog to do your work. Thanks.

  • Anonymous
    December 14, 2011
    Hi  Gautam, I understand what you mentioned above, but i am very new to coded ui. Wolud you please help mewith how I can integrate environment variables from environment to coded ui? Thanks

  • Anonymous
    December 14, 2011
    Sure, give it a try and let me know if you need more help by using the "Send Email" link of this blog.