Thursday, September 17, 2015

How to use App.Config for configuration variables in selenium (C#) test

Sometimes in your framework, you are required to define some variables (custom information) that you have to use throughout your framework. For such variables we use, app.config file.

Step 1: Create a new visual studio project.

Step 2: In the Add New Item list, click to select XML File.

Step 3: In the Name text box, type App.config(Don’t add “.xml” in the filename), and then click Add.

Step 4: Add following code in your app.config file.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>

    <!--Browser Information-->
    <add key="Browser Name" value="Firefox"/>

    <!--Environments URLs-->
    <add key="POS Upload url" value="http://testing.apps.eco.com/StagingPOSUpload/"/>
    <add key="Salesforce url" value="https://test.salesforce.com/"/>

    <!--Environments Credentials-->
    <add key="POS Username" value="azunjurwad"/>

  </appSettings>
</configuration>

Step 5: Add a new item “Unit test” in the project and add following code to access the variables defined in App.config file

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Configuration;

namespace UnitTestProject2
{
    [TestClass]
    public class UnitTest3
    {
        [TestMethod]
        public void TestMethod1()
        {
            Console.WriteLine(ConfigurationManager.AppSettings["Browser Name"]);
            Console.WriteLine(ConfigurationManager.AppSettings["POS Upload url"]);
            Console.WriteLine(ConfigurationManager.AppSettings["Salesforce url"]);
            Console.WriteLine(ConfigurationManager.AppSettings["POS Username"]);
        }
    }
}

Step 6: Click on references -> Add reference - > Select “System.configuration” and click ok.

Step 7: Execute this test method and it is done.



No comments:

Post a Comment