Read data from App.config in Selenium C#

In the previous tutorial, we have discussed Types of Automation Framework and Structure of Framework in Selenium C# In this tutorial, we will learn about the App.config file, Like what is an app.config and how to read the data from App.config file.

What is App.config File in Selenium C#

App.config file is a file that contains all the configurations required by our framework. Inside the App.config file we need to specify some configuration in a certain format. The configuration should be in a format of “Key:Value” pair, where Key should be unique.

System.Configuration: Now in order to read the configuration from App.config  file Visual studio provide a utility, and that utility is “System.Configuration”, for using this utility we need to refer System.Configuration.dll file in our project.

ConfigurationManager: Inside this System.Configuration.dll file or assembly there is a class called “ConfigurationManager” which help us to read the configuration from App.config file.

appSettings property: Inside the ConfigurationManager class there is a property that name is “appSettings”.

Get Method: The appSettings has a method called “Get”. There is two overloaded version of Get method.

  • The first one takes Index of the configuration as an argument.
  • And the second one take Key as an argument, here Key means the unique key which we have specified for our configuration.

The most important thing is that whatever the configuration you have specify inside the App.config will be treated as string.

Steps to read data from App.config file in Selenium C#

So in my visual studio i have a packages.config file i will copy this and paste it in my project, after copying rename it as App.config and i am going to do some certain modification in it you can do the same.


Step 1:

First remove the package tag from coding and add the configuration tag, inside it add a appSettings tag now whatever the configuration which we are going to specify in the form of key value pair should be present inside the appSettings tag in other words we can considered it as a default template for our configuration and all the configuration should be go inside the appSettings tag.

<add key="Browser" value="Firefox"/>
<add key="Username" value="User"/>
<add key="Password" value="Pass"/>

In this manner we have to specify our configuration inside the appSettings tag. So once when it’s done as i have told you we need to take the help of ConfigurationManager class which is coming from System.Configuration , in order to use it we need to refer it.

Step 2:

Inside the Visual studio do a right click on references in solution explorer and click on add reference in the opening window search for System.Configuration and add the reference to our project.

System.configuration this is the dll file which we need to refer. Inside this dll file or assembly there is a class called ConfigurationManager which is going to read the data from App.config file.

If we go inside the class definition of this class so here we will see appSettings as a class property inside this NameValue collection there is a method called Get in which the first one takes the Index as the configuration and the second one takes the key value for the configuration.

Here the AppSettings is a static property so we will call it using the class name.


Here i am specifying the key instead of specifying the index you can also specify the index but it’s better to specify the key so you will be aware about that which configuration you’re reading.

Step 3:

Run this script in debug mode. Copy this line and paste it inside the immediate window, immediate window is the window which gives the runtime value of a variable or a statement.

So here you can see we are able to read the value as “Firefox” of the key “Browser”. Do similarly for all the keys. When we click on the test output that shows all the values of the keys that we have supplied.

Step 4:

Now moving on we are going to create an interface. The interface has some common method or some basic methods which will be required for our framework.

Suppose in current situation we are using App.config file for reading our configuration, it is also possible that in future you might use comma,separated file which is cs file or a XML file for loading the configuration so all those files which will going to read the data from respective file should have these basic methods because these are very important to initialize for our framework.

That is why we are going to initialize an interface which will contain all the classes and all the classes should have these basic methods.

Inside the visual studio we will go into the interface directory and add a interface and naming it as IConfig.

So here we are going to add some basic method which our class will have to implement which is very important for our framework to initialize and we are making it public so any class can access this.

But before that as you see that the browser can be as a Chrome, Firefox or Internet Explorer. We need a help of some certain constant to represent it so for this we are going to create the “enum“, The enum is enumeration or in word it is used to create the named constant.

So inside the configuration directory we will add a class called BrowserType then i will change it to enum, first we will make this class as public and whatever the entity which we will specify inside this enum it will be termed as a named constant.

public enum BrowserType 

The reason why i am saying them named constant because internally the compiler is going to assign the value to each of them starting from 0.

Step 5:

Now go to UnitTest1.cs and write the code


Run the test script and go to output window it will display 0 and 1 as output.

Because this value is assigned by the compiler to the entity for the named constant.

Inside the configuration directory i am adding a class named as AppConfigReader which is going to read the data from AppSettings. Again make this class as public so any other class can also access this.

And in the Settings directory we are going to add one more class and named it as AppConfigKeys inside this class we will specify all that keys which is present inside the App.config file.

The reason behind this suppose you change any key so all you need to do come back to AppConfigKeys.cs and inside AppConfigKeys class change that key and it will be applicable to all the scripts which are using this key for reading the configuration.

public class AppConfigKeys
public const string Browser="Browser";
public const string Username="Username";
public const string Password="Password";

Here we have used a keyword called const which is used to declare a constant that means once you have used this keyword with a variable that variable become a constant and you cannot change its value during the runtime even not in the compile time.

Step 6:

Again we are going to AppConfigReader.cs file and this class is going to implement the IConfig interface.

public class AppConfigReader:IConfig

If we will go inside this IConfig interface we will specify some common methods which are very important for our framework to initialize.

public class IConfig
BrowserType GetBrowser();
string GetUsername();
string GetPassword();

Step 7:

Now go inside the AppConfigReader.cs file and give their return values.

public BrowserType GetBrowser ()
string browser= ConfigurationManager.AppSettings.Get(AppConfigKeys.Browser);
return (BrowserType)Enum.Parse(typeof(BrowserType),browser);
public string GetUsername ()
return ConfigurationManager.AppSettings.Get(AppConfigKeys.Username);
public string GetPassword ()
return ConfigurationManager.AppSettings.Get(AppConfigKeys.Password);}

In this case we are returning the string value and we know that all the data which is present inside the App.config is of type string so we don’t need to do any additional logic to get this string value.

But here as we know that the config file will have everything inside the string and the return type of this method is an enum so we need to do some additional configuration to convert our string value into enum.

We will use the same statement to read the browser and here we are going to typecast to the enum type for this we have a utility method Enum.Parse.

It takes two arguments, first is the type of enum and second is the value which we need to typecast and the return type of this method is an object. We are typecasting it into BrowserType.

Step 8:

Now come back to test class first we need to create the reference variable for the interface.

IConfig config= new AppConfigReader();

It means we are creating a reference variable of type interface that is IConfig and using that reference variable we are pointing to the derived class object.

run the test script and see the output.

So these are simple ways for reading the configuration from App.config file in Selenium C#, in this manner you all can also read the configuration from App.config file.