Search code examples
visual-studio-2015coded-ui-tests

Coded UI - How to have configuraable URL's?


I created simple Coded UI test where I do the following:

  1. Open a browser
  2. Go to a web address
  3. Click on a hyperlink (link within the webapp's domain).

After the UIMapping is generated I see a bunch of auto-generated mapping code. Within the UIMapping file (class) I see URL's which are essentially hard-coded.

Example:

this.FilterProperties[HtmlDocument.PropertyNames.PageUrl] = "http://mytesturl:8000";
this.mUIItem50006598Hyperlink.FilterProperties[HtmlHyperlink.PropertyNames.Href] = "http://mytesturl:8000/link"

How can I make my automated UI tests configurable so that I can run the same tests against different environments (dev, test, prod...etc)?


Solution

  • As Adrian mentioned this is an oft-repeated question with no clear answer. You can find a little information on my approach HERE.

    The short story is that you won't be able to dynamically configure an environment if you're relying on the CodedUI mapping functionality unless your test methods:

    • Call a helper method to navigate to a base URL, which in turn is decided on some sort of data driven configuration (An XML/app.config file, a CSV, a spreadsheet). You can find info on how to do that on my question in the link above.
    • Call your mapper-generated method. When creating it be sure to assume that you're only navigating from the base URL, not opening your browser etc.

    So your code will look like:

    [TestMethod]
    public static void GenericTestMethod() {
        //get browserWindow from your test setup method etc.
        GoToEnvironmentBaseUrl(browserWindow);
        MapperGeneratedCodedUiMethod();
        AssertStuff();
    }
    
    public static void GoToEnvironmentBaseUrl(BrowserWindow browserWindow) {
        browserWindow.NavigateToUrl(new Uri("http://www."
                + ConfigurationManager.AppSettings.Get("EnvironmentURLMod")
                + ".com"));
    }