Code & QA

Global Variables

The most of applications, especially big and complex ones, need for a kind of variables which must be visible and available all over the application. Sometimes you have to pass data from one module to another, between assemblies, classes, methods, etc. There are different approaches in C# for resolving the problem, and one of them is the use of static variables and methods. In accordance with OOP principles, there are mustn’t be any possibility to create a global variable using object-oriented programming language. And there is no such possibility in C#. Instead, static variables are used.

What is the static member see in MSDN.

In the given framework I use the same variables both in the Mapping and the Tests modules. I’m going to initialize variables in my tests but to use them in the Mapping project passing values of variables there.

For the implementation, I added a new static class to my Extensions.cs file and named it Globals. The class is used for storing those static variables which must be used simultaneously in the Mapping and the Tests modules. Sure, you can keep your static members wherever you wish and it’s not mandatory to have a separate class for your “global” statics. But I think it’s better to have all the mess being collected in one place with user-friendly names.

I added class Globals and put these members into it:

/// <summary>
/// stores global static variables used all over the solution
/// </summary>
public static class Globals {
    public static int timeoutForPageLoading;
    public static int timeoutForWaitingElement;
    public static int timeoutBetweenClicks;
    public static string applicationURL;
}

Initial values of these variables are kept in App.config file in the Tests project. The method which sets the values will be placed in Extensions class in the Mapping project. It is invoked in BaseTest class in the Tests project. Then the initialized variables are used in the Mapping project and may be used in the tests as well. This is the way how data may be passed from one module to another and between classes and instances inside a module.

App.config file:

<?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"/>
		<add key="browserType" value="Android"/>
		<add key="timeoutPageLoading" value="30"/>
		<add key="timeoutElementLoading" value="10"/>
		<add key="timeoutBetweenClicks" value="1"/>
		<add key="doEventLogging" value="true"/>
		<add key="applicationURL" value="http://address.domain"/>
	</appSettings>
. . .
</configuration>

The method for static global variables initialization (to avoid errors you have to add reference to System.Configuration library in the Mapping project):

public static class Extensions
{
    ...
    /// <summary>
    /// sets up global variables values
    /// </summary>
    public static void SetGlobals()
    {
        int temp;
        Globals.timeoutForPageLoading = Int32.TryParse(ConfigurationManager.AppSettings["timeoutPageLoading"], out temp) ? temp : 60;
        Globals.timeoutForWaitingElement = Int32.TryParse(ConfigurationManager.AppSettings["timeoutElementLoading"], out temp) ? temp : 30;
        Globals.timeoutBetweenClicks = Int32.TryParse(ConfigurationManager.AppSettings["timeoutBetweenClicks"], out temp) ? temp : 1;
        Report.doEventLogging = ConfigurationManager.AppSettings["doEventLogging"] == "true";
        Globals.applicationURL = ConfigurationManager.AppSettings["applicationURL"];
    }
}

Usage of the method (don’t forget to rebuild Mapping project before):

[SetUpFixture][TestClass]
public class GlobalSetup
{
    static string testSessionName = ConfigurationManager.AppSettings["testSession"];
    [SetUp]
    public static void SetUpAssembly()
    {
        Mapping.Extensions.SetGlobals();
        Report.StartTestSession(testSessionName);
    }
    ...

    ...

}

So the variables initialized in SetGlobals() may be used in any place which has a reference to the Mapping project.
Extension methods                                                            Table Of Content                            WebApplication class

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