Tuesday 20 March 2018

How to Read Configuration File in Selenium C#

When we are working on automation framework then we have to follow such strategy so we can avoid hard code value as much as we can.

There are many parameters in every framework we can be changed frequently for running test case on different environment as well as with different configuration. So, we can not use hard code value for such parameters. We have to use configuration file for this scenario.

In that file, we can store all the parameters which are used in framework like browser name, browser type, application URL etc. in Key-Value manner.

First of all you have to 'System.Configuration' assembly reference to access configuration settings, using ConfigurationManager.

You can add that assembly reference from right click in References then go to Assembles section and add it.



There are many methods for read configuration file.

First Method:

Suppose we have below 'app.config' file in project. We have only one section 'appSettings' and very simple app.config file.


<?xml version="1.0" encoding="utf-8" ?>  
<configuration>  
  <startup>  
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />  
  </startup>  
  <appSettings>  
    <add key="BrowserName" value="Chrome"/>  
    <add key="BrowserType" value="local"/>
    <add key="URL" value="www.google.com"/>  
  </appSettings>  
</configuration>  

Now, you need to create one function for reading parameters in 'appSettings' section. I have created below function for that and I have used 'ConfigurationManager' for reading value.


public static void ReadConfigurationFile()  
    {  
        String browserName = ConfigurationManager.AppSettings["BrowserName"];  
        String browserType = ConfigurationManager.AppSettings["BrowserType"];
 String URL = ConfigurationManager.AppSettings["URL"];  
          
    }

You can use above function for reading values from configuration file.

Second Method:

Suppose we have below 'app.config' file in project. We have only one section 'ApplicationSettings' but we have defined that section in 'configSections'.


<?xml version="1.0" encoding="utf-8" ?>  
<configuration>  
   <configSections>  
    <section name="ApplicationSettings" type="System.Configuration.NameValueSectionHandler"/>      
  </configSections>  
    
  <ApplicationSettings>  
    <add key="ApplicationName" value="First Project"/>  
    <add key="Browser" value="Chrome"/>  
    <add key="SecretKey" value="XXXXX"/>  
  </ApplicationSettings>  
  <startup>  
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />  
  </startup>    
</configuration> 

In this method, I have used GetSection() function for read values from sections.
I have created below function for reading value.


public static void ReadConfigurationUsingSection()  
    {  
        var applicationSettings = ConfigurationManager.GetSection("ApplicationSettings") as NameValueCollection;  
        if (applicationSettings.Count == 0)  
        {  
                Console.WriteLine("Application Settings are not defined. Pelase defined first.");  
        }  
        else  
        {  
            foreach (var key in applicationSettings.AllKeys)  
            {  
                Console.WriteLine(key + " = " + applicationSettings[key]);  
            }   
        } 

 String ApplicationName = applicationSettings["ApplicationName"];
 String Browser = applicationSettings["Browser"];
 String SecretKey = applicationSettings["SecretKey"]; 
    } 

In above function, we read all sections values in one variable using GetSection() function and then we read value one by one using 'Key'.

Third Method:

Suppose we have below 'app.config' file in project. We have one main section 'BlogGroup'. There is sub section 'PostSetting' in 'BlogGroup' section.


<?xml version="1.0" encoding="utf-8"?>  
<configuration>  
  <configSections>     
    <sectionGroup name="BlogGroup">  
      <section name="PostSetting" type="System.Configuration.NameValueSectionHandler"/>  
    </sectionGroup>      
  </configSections>  
  
  <BlogGroup>  
    <PostSetting>  
      <add key="PostName" value="Getting Started With Config Section in .Net"/>  
      <add key="Category" value="C#"></add>  
      <add key="Author" value="Sameer"></add>  
      <add key="PostedDate" value="20 March 2018"></add>  
    </PostSetting>  
  </BlogGroup>          
    
  <startup>  
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>  
  </startup>  
</configuration>   


In this method, I have used GetSection() function for read values from sections.
I have created below function for reading value.


public static void GetConfigurationUsingSectionGroup()  
{  
    var PostSetting = ConfigurationManager.GetSection("BlogGroup/PostSetting") as NameValueCollection;  
    if (PostSetting.Count == 0)  
    {  
        Console.WriteLine("Post Settings are not defined. Please define first.");  
    }  
    else  
    {  
        foreach (var key in PostSetting.AllKeys)  
        {  
            Console.WriteLine(key + " = " + PostSetting[key]);  
        }  
    }
 String PostName = PostSetting["PostName"];
 String Category = PostSetting["Category"];
 String Author = PostSetting["Author"];
 String PostedDate = PostSetting["PostedDate"];
 
}  

In above function, we read all sub sections values in one variable using GetSection() function and then we read value one by one using 'Key'.
Please add comment if you have any question.

No comments:

Post a Comment

Popular