Creating Automation Base Class in Selenium C#

In previous tutorial we have discussed about how to Read data from App.config in Selenium C# In this tutorial we will learn about the Base class and how to create base class in Selenium C#.

What is the Base class

Base class is very important class for our framework because it is going to take care of launching the browser which is specified in our App.config file.

Create automation Base Class in Selenium C#

For creating the base class in Selenium C# we need to follow some simple steps let’s take a look.

Step 1:

So inside the BaseClass directory we will add a class called BaseClass and after adding it we will make it public so it can be access by other classes in our framework.

Inside this class we will make three methods one for creating the Firefox driver, one for Chrome driver and one for IE driver and we will make them private so the scope of these methods is only inside this class.

The reason for why we will making them static because we need to access these methods inside in static context and the return type is web driver because we know it will return us a driver object.

public class BaseClass 
{
private static IWebDriver GetFirefoxDriver()
 {
IWebDriver driver= new FirefoxDriver ();
return driver;
 }
private static IWebDriver GetChromeDriver() 
 {
IWebDriver driver= new ChromeDriver (); 
return driver;
 } 
private static IWebDriver GetIEDriver() 
{ 
IWebDriver driver= new InternetExplorerDriver(); 
return driver;
 }
}

IWebDriver driver= new FirefoxDriver (); this particular statement will launch the Firefox driver or browser. Same statement with other driver names work for others.

Step 2:

In Settings directory we will add one more class named as ObjectRepository.

Now this class will contain the objects and properties which will be used by other classes in our framework. Make this class public and in order to access to them easily, we will make it static so  by using the class name we can access them.

So in current case we need the property of IConfig to read the data from config file and we need the property of the IWebDriver to launch to the browser based on the  configuration.

public class ObjectRepository 
{
public static IConfig Config {get; set;}
public static IWebDriver Driver {get; set;}
}

Step 3:

Now go to BaseClass and here write the code.

[AssemblyInitialize]
public static void InitWebdriver(TestContext tc) 
{
ObjectRepository.Config = new AppConfigReader();
switch (ObjectRepository.Config.GetBrowser());
 {
case BrowserType.Firefox;
ObjectRepository.Driver= GetFirefoxDriver ();
break;

case BrowserType.Chrome; 
ObjectRepository.Driver= GetChromeDriver (); 
break; 

case BrowserType.IExplorer;
ObjectRepository.Driver= GetIEDriver (); 
break; 
 
default: 
 throw ("Driver Not Found:", ObjectRepository.Config.GetBrowser(),ToString());
 } 
}

Code Explanation:

Because we want our method should be first to be executed for this  we are using the attribute [AssemblyInitialize].

Here we are using ObjectRepository.Config and  we will create the object AppConfigReader, now inside this we have already a method for getting the browser so we are going to use the same.

In order to differentiate between the browsers we will use switch case here. Write  switch (ObjectRepository.Config.GetBrowser());

So this is going to return us the browser from the App.Config so here  we will use case the first case is BrowserType.Firefox;  as you can see here with the help of enum how easy to access type of browser and here we will use ObjectRepository.Driver and then call GetFirefoxDriver (); method and after that put the break.

Similarly write other cases for other browser type, and we will make one more case that is default case for the condition where the user specify the browser other than the  browsers we have given in the cases.

So in this case we will throw an exception that is NoSuitableDriverFound  in the exception directory create a custom exception named ad NoSuitableDriverFound.

 

In order to create the user defined exception first we need to extend the exception class and then create a constructor for user defined exception class.

Inside this constructor we will take string as an argument and will supply it to our super class and leave this constructor empty.

class NoSuitableDriverFound: Exception
{
public NoSuitableDriverFound (string msg):base(msg)
 {
 }
}

So this exception will come when we specify the name of the browser rather than the browsers we have specified like if we have given Firefox, Chrome and IE in the three cases so if we will specify any other browser name in the App.Config then this exception will be thrown.

In default case write throw keyword with a string (“Driver not found:”, ObjectRepository.Config.GetBrowser(), ToString ());

Step 4:

Once it is done again go back to UnitTest class and just build the project.

public class UnitTest1 
{
 public void TestMethod()
 {
   Console.WriteLine("Test");
 }
}

After writing here the Console.WriteLine(“Test”); statement again build it and in order to use this attribute we need to use Test Class attribute with the class.

[TestClass]
public class BaseClass
{
]

Put the break point on all three private methods.

Step 5:

For Example-  Inside the App.Config first of all i am specifying the Firefox.

<Configuration>
<appSettings>
<add key= "Browser" value="Firefox"/>
<add key="Username" value="xyz123@bugzilla.com"/>
<add key="Password" value="123456"/>
</appSettings>
</Configuration>

Because it has the debug point so we will see browser type it will select the Firefox because of what we have specified inside our App.Config.

If we will do a step over it will go inside the GetFirefoxDriver ()and launch the Firefox browser first  as you can see there the statement where we have mentioned IWebDriver driver = new FirefoxDriver this will launch the Firefox browser.

return driver; this will return the driver back to the calling. Our driver property will be set by this driver object. From now onward everything will be taken care by the framework. All we need to do is to specify the proper configuration in our App.Config file.

I hope you all now understand what is base class and how to create Automation Base class in our Selenium C# project.