Selenium C# Tutorial: Developing App Configurations

Updated: Jan 18

In this tutorial, I will cover APP configurations as part of the automation framework. A configuration file is a single place to configure and control the test framework configuration. A configuration file should have the information required to manage the framework or add information that we can use as an audit for the test execution (which is very handy when working with large teams or complex automation projects).


A simple configuration file may include App settings such as:

  • Technology (e.g., Web, DB, UI, etc.)

  • Browser type used during tests

  • Log path

  • Test Type (e.g. integration, component, etc.)

Configurations using App.config

We can create our framework configuration using the inbuilt App.config, which is available in visual studio:

  1. Open Visual Studio

  2. Solution Explorer -> Add -> "Application Configuration File".

Now that we have created the config file, let's add some simple code to it to demonstrate how can we use it :


In the following code example, I will add App config that will replace the site URL I use in the framework:

        static string Url = "https://www.agilequalitymadeeasy.com/";

Here is the code if you need to copy it:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings> <add key="SiteUrl" value ="https://www.agilequalitymadeeasy.com/"/></appSettings>  
</configuration>

Now let's add a new class, "ConfigReader," that we will use to read data from the App. Config file:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;

namespace SeleniumExcel.AppConfiguration
{
    class ConfigurationReader
    {
        public static string InitializeTest()
        {
            ConfigurationManager.AppSettings["SiteUrl"].ToString();
            return "";
        }
    }
}

And now we can call it from the Test class:

static string Url = ConfigurationReader.InitializeTest();

Configurations Development

We can also create a custom configuration and use it in our framework without using the built-in App.Config file. We will use a System.XML namespace and add Xpathltem and XpathDocument classes.


Step 1: Creating a Properties class

using System;
using System.Collections.Generic;
using System.Text;

namespace SeleniumExcel.AppConfiguration
{
    public class Settings
    {
        public string TestType { get; set; }
        public string SiteUrl { get; set; }
        public string Build { get; set; }
        public string IsLog { get; set; }
        public string LogPath { get; set; }
    }
}


Step 2: Add an XML file

and now add the following code:

<?xml version="1.0" encoding="utf-8" ?>
<SeleniumExcel>
<RunSettings>
  <SiteUrl>https://www.agilequalitymadeeasy.com/</SiteUrl>
  <!--Available Flags Regression, Unit, integration, E2E-->
  <TestType>Unit</TestType>
  <BuildName>SeleniumTraining </BuildName>
  <!--Available Flags Y,N-->
  <IsLog>Y</IsLog>
  <LogPath>F:\LogRecords</LogPath>
</RunSettings>
</SeleniumExcel>


Step 3: Reading XML data

Create a new class "XMLReader" and add the following code:

System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.XPath;

namespace SeleniumExcel.AppConfiguration
{
public class XMLReader
{
public static void SetFrameSettings()
{
XPathItem siteUrl;
XPathItem testType;
XPathItem buidName;
XPathItem isLog;
XPathItem logPath;

string strFileName = Environment.CurrentDirectory.ToString() + "\\AppConfiguration\\FrameworkConfiguration.xml";

//Read File Content
FileStream stream = new FileStream(strFileName, FileMode.Open);
XPathDocument document = new XPathDocument(stream);
XPathNavigator Navigator = document.CreateNavigator();

siteUrl = Navigator.SelectSingleNode("SeleniumExcel/RunSettings/SiteUrl");
testType = Navigator.SelectSingleNode("SeleniumExcel/RunSettings/TestType");
buidName = Navigator.SelectSingleNode("SeleniumExcel/RunSettings/BuildName");
isLog = Navigator.SelectSingleNode("SeleniumExcel/RunSettings/IsLog");logPath = Navigator.SelectSingleNode("SeleniumExcel/RunSettings/LogPath");

Settings.SiteUrl = siteUrl.ToString();
Settings.TestType = testType.ToString();
Settings.BuildName = buidName.ToString();
Settings.IsLog = isLog.ToString();
Settings.LogPath = logPath.ToString();
}

Step 3: Using XML configuration in framework

 [TestMethod]
        public void UsingConfigurationExample()
        {
            Driver = new ChromeDriver(chromeOptions);
            XMLReader.SetFrameSettings();
            Driver.Navigate().GoToUrl(Settings.SiteUrl);
        }


307 views0 comments