Config Transformation – For Selenium Test.

Disclaimer this article is still about c# and .NET Framework in case net core config works little different. This time we will be talking about config transformation.

The Problem

So if you are using c# for writing automation high chances are you are using app.config for storing, for example, to store environment URL.

I have seen 3 „strategies” for that:

Simplest is to have values for each environment and comment/uncomment section as you need. This is a really barebones solution and it has a lot of problems from committing by mistake of being really hard to maintain.

Example:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <!-- Dev -->
        <add key="url" value="[URLHERE]"/>
        <add key="user" value="[LOGIN HERE]"/>
        <add key="password" value="[PASSWORD HERE]"/>

        <!-- Test -->
        <!--
                <add key="url" value="[TESTURLHERE]"/>
                <add key="user" value="[TESTLOGIN HERE]"/>
                <add key="password" value="[TESTPASSWORD HERE]"/>
        -->

        <!-- PrePROD -->
        <!--
        <add key="url" value="[PPURLHERE]"/>
        <add key="user" value="[PPLOGIN HERE]"/>
        <add key="password" value="[PPPASSWORD HERE]"/>
-->
        
        
    </appSettings>
    
</configuration>

The second one is still keeping all stuff in one config but also having a master „switch” that decides which version to use.
Looking back this is the solution I have seen quite a lot. There are many different variants of it. Its good strategy but it still requires you to check if files are in the proper state when you are committing. And a little more code to maitain.

Example

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <!-- option DEV  TEST PPROD -->
        <add key="enviroment" value="DEV"/>

        <!-- dev -->
        <add key="devurl" value="[URLHERE]"/>
        <add key="devuser" value="[LOGIN HERE]"/>
        <add key="devpassword" value="[PASSWORD HERE]"/>

        <!-- Test -->
        <add key="testurl" value="[URLHERE]"/>
        <add key="testuser" value="[LOGIN HERE]"/>
        <add key="testpassword" value="[PASSWORD HERE]"/>

        <!-- PrePROD -->
        <add key="pprodurl" value="[URLHERE]"/>
        <add key="pproduser" value="[LOGIN HERE]"/>
        <add key="pprodpassword" value="[PASSWORD HERE]"/>
        
        
    </appSettings>
    
</configuration>

The 3rd strategy is Config Transformation.

In the visual studio, there is a wonderful mechanism that allows changing some web.config setting depending. on build config.

Simplifying you have a master web.config which has all default and files for each build configuration for example web.PreProd.config this one keeps only info of the values that are specific for PreProd environment. For example, it’s URLs.

There is one problem. You probably noticed I wrote web.config, not app.config. By default, config transformations are working only for web config.
A long time ago to enable them for app config required changes to the csproj file.

Fortunately, there are few visual studio extensions that make it work for app.config. (for example SlowCheetah and Configuration Transform)

First, you will need to prepare Build Configuration for all your environments. Let’s say, Dev, Test, and PreProd. – if you don’t know how to do it check my previous article (note, you won’t need to do the build variable step).

For this article, I will be using Configuration Transform Extension.

Let’s start

1. Right click on app config and select „add config transformation”


2. You will see files for each configuration.

So how does it work?
There is XSLT (eXtensible Stylesheet Language Transformations ) which in simplification is a way of describing how to make changes to XML.

I won’t go into whole detail here. but what you need to know are three things:

  • You can change only some values – for example, the only URL without changing timeout or paths to drivers.
  • You need to reproduce structure so if the value you want to change is in the section you will have to wrap it in app setting.

Another important thing worth remembering it after typical stays like key/name and value. you will need to add this
if you want to match by key attributes:
xdt:Transform=”SetAttributes” xdt:Locator=”Match(key)”
if you want to match by name attibute:
xdt:Transform=”SetAttributes” xdt:Locator=”Match(name)”

Example

Base app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>

        <add key="url" value="[URLHERE]"/>
        <add key="user" value="[LOGIN HERE]"/>
        <add key="password" value="[PASSWORD HERE]"/>
        
    </appSettings>
    
</configuration>

Base app.dev.config

We will only change url.

<?xml version="1.0"?>
<!-- For more information on using app.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<!-- In case configuration is not the root element, replace it with root element in source configuration file -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="url" value="localhost:8080" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
  </appSettings>
</configuration>

You can preview if your changes work by right clicking on the config file and selecting preview option (make sure you saved changes first!)

only url was changes


Now, all it is left is build the solution.

Next, go to the bin folder config you will have config file named sth like .dll.config – it will have values accurate for selected config.

That all!

In short Config, the transformation is a great way to manage your environment specific variables. And this is the tip of the iceberg. there is lots more to it that you want to do further reading.

That all for today, next post will be a short update!

Dodaj komentarz