Code & QA

Configuration

Any application, especially a big project, always needs some data that are used as parameters or settings of the application. Obviously, during the development process and evolution of the application, these data are changed. Also, the data may vary in accordance with external circumstances, for example when an execution environment is changed. Recompiling of the code every time any changes in the application or in the testing environment or somewhere else has happened is not a good idea. In addition, not everyone and not everywhere has a possibility to rebuild the project, besides you should not allow to do it to everyone as well.

Yet the data are changed you have to manage it somehow. The current project is not unique exclusion. For example, when tests run session is over the summary report must be sent to specified email addresses. But what if starting from a day I have to send the email to one more address, or the address of the recipient have changed, or contrariwise I want to exclude someone from recipient’s list?

Recompiling the entire project for such a trifle is breaking a butterfly on the wheel. That’s why all software developers keep such kind of data in external storages where the data may be easily modified by authorized persons. Many people utilize different ways for that – databases, Excel tables, configuration files, and cloud services and so on.

I decided that the simplest and the most effective approach for storing application settings and parameters are using a configuration file. Especially it’s useful for little and middle size applications where you store just a dozen or two of parameters and using a database or a special service for a couple of lines of text is not efficient.

In C# there are special classes for managing configuration files. The classes are located in System.Configuration.dll library (System.Configuration namespace). Working with configuration files, in general, is very easy and plain. There are a lot of tutorials for that on the internet and you can study it just in an hour.

I want to show here how the configuration file is used in the project.

The first step is adding App.config file to the Tests project:

clip_image001

clip_image002

App.config file is added to the project.

clip_image004

I decided to keep in the configuration file the name of the application and the list of summary report email recipients. I added a new section to the configuration file as shown below

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="emailTo" value="me@gmail.com;My_collegue@gmail.com;my_boss@gmail.com"/>
    <add key="testSessionName" value="Tested Application build 001"/>
  </appSettings>
</configuration>  

And made little changes to the code:

[SetUpFixture][TestClass]
public class GlobalSetup
{
  static string testSessionName = ConfigurationManager.AppSettings[“testSession”];
  [SetUp]
  public static void SetUpAssembly()
  {
    Report.doEventLogging = true;
    Report.StartTestSession(testSessionName);
  }
  
. . .

  [TearDown][AssemblyCleanup()]
  public static void TearDownAssembly()
  {
    Report.SaveReport();
    string subject = “Test report – “ + testSessionName;
    string[] addresses = ConfigurationManager.AppSettings[“emailTo”].Split(‘;’);  
    string body = File.ReadAllText(Report.ReportFolderPath + “\\index.html”);
    Report.SendReportByEmail(subject, addresses, body);
  }
}

Do not forget to add the reference to System.Configuration library. As you can see handling the project data with configuration file is very easy. For example, you can define what kind of web browser to use for the tests execution.

Add a new node to the configuration file

<add key="browserType" value="Firefox"/>

Add the following method to BaseTest class

/// <summary>
/// Initializes IWebDriver instance as an instance of specified driver
/// </summary>
/// <param name="browserType">browser type</param>
/// <returns>Initialized instance of IWebDriver</returns>
IWebDriver SetUpDriverInstance(string browserType)
{
  switch (browserType)
  {
    case "Firefox":
      return new FirefoxDriver();
    case "Internet Explorer":
      return new InternetExplorerDriver();
    case "Chrome":
      return new ChromeDriver();
    default:
      Report.AddError(browserType + " web driver instance cannot be initialized. Test will by terminated. Verify configuration parameters.");
      throw new Exception();
  }
}

Invoke the method in Setup() method


[SetUp][TestInitialize]
public void Setup()
{
  driver = SetUpDriverInstance(ConfigurationManager.AppSettings["browserType"]);
}

Now there is no need anymore to make changes to the code and to recompile the project for running tests with different browsers. All the changes are managed in the App.config file.

P.S. Keep in mind that running tests with InternetExplorer or Chome browsers requires having IEDriverServer.exe and chromedriver.exe files in the working directory (Test/bin/Debug).

The files may be downloaded from Seleniumhq site:

http://docs.seleniumhq.org/download/

Event Logging
Table Of Content
Environment adjustments via Registry

Leave a Reply

Your email address will not be published. Required fields are marked *


Notice: Undefined offset: 0 in /home/chyqruxavcln/public_html/learnseleniumtesting.com/wp-content/plugins/cardoza-facebook-like-box/cardoza_facebook_like_box.php on line 924