Tuesday, September 24, 2019

Selenium - Configuration


  • Read Configuration using ConnectionStrings from App.Config
  • Read Configuration using AppSettings from App.Config
  • Read Configuration from External Config file

using System.Configuration;
ConfigurationManager provides two properties:
AppSettings: Gets the AppSettingsSection data for the current application’s default configuration. 
ConnectionStrings: Gets the ConnectionStringsSection data for the current application’s default configuration. ConnectionString is basically meant to store DataBase connection strings.
App.config File
How to Read Connection String in C# using ConfigurationManager

<configuration>
<connectionStrings>
<add name="DBConnection" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=WingtipToys;Integrated Security=True;Pooling=False"/>
</connectionStrings>
</configuration>

var dbString = ConfigurationManager.ConnectionStrings[“DBConnection”].ConnectionString;

How to Read AppSettings in C# using ConfigurationManager

<configuration>
    <appSettings>
          <add key="URL" value="http://www.store.demoqa.com"/>
  <add key="FolderPath"="C:\Users\lakshay.sharma\Downloads\"/>
    </appSettings>
</configuration>

To read the Url from the above config file, use the below code:

var url = ConfigurationManager.AppSettings[“URL”];

var folderPath = ConfigurationManager.AppSettings[“FolderPath”];

NOte: Folderpath could be any path which is needed in the framework, for example path to save the reports or the screenshot.  

Steps to read ConnectionString from External Config File using ConfigurationManager

App.config File

  <configuration>
<connectionStrings configSource="DBConnectionStrings.config" />
  </configuration>

Create another config file and name it DBConnectionString.config under the same project.

DBConnectionString.config File

<connectionStrings>
<add name="DataBaseName" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=WingtipToys;Integrated Security=True;Pooling=False/>
</connectionStrings>

var connString= ConfigurationManager.ConnectionStrings[“DataBaseName”].ConnectionString;


  • Steps to read AppSettings from External Config File using ConfigurationManager


App.config File

<configuration>
<appSettings configSource="Configurations\Environment.config" />
</configuration>

Create another config file and name it Environment.config under the same project.

Environment.config File

<appSettings>
<add key="URL" value="http://www.store.demoqa.com"/>
</appSettings>

To read the app setting from the above config file, use the below code:

var url = ConfigurationManager.AppSettings[“URL”];


Create Configuration Folder
Right click on the project and create one new folder. Name it as Configurations.

Create Environment Config file
In the configurations folder, create one config file and name it as Environment.config. Paste the below code in the Environment file.

<appSettings>
<add key="URL" value="http://www.store.demoqa.com"/>
</appSettings>

Modify App.config File
<configuration>
               <appSettings configSource="Configurations\Environment.config" />
</configuration>

Add Reference to ConfigurationManager
Right Click on the References and select Add Reference... Now search for System.ConfigurationManager. Select it and this will add to your Project References

Environment file in Visual Studio there is a property called Copy to Output Directory that can be used to copy the configuration file automatically: Change it to Copy Always.

No comments:

Post a Comment

API interview questions

  https://www.katalon.com/resources-center/blog/web-api-testing-interview-questions/ Top 50+ Web API Testing Interview Questions [Ultimate l...