HomeSelenium C#

Selenium C# – Browser Factory Program and the usage of IWebDriver interface

Selenium C# – Browser Factory Program and the usage of IWebDriver interface

<<Previous Post                            << Complete Tutorial>>                              Next Post>>

In our previous articles, I have not used IWebDriver interface for demonstration.

IWebDriver is a predefined interface in Selenium API and we can assign the objects of ChromeDriver, FirefoxDriver, InternetExplorerDriver classes to the same interface if needed.

So far in our previous posts, we have created the object creation statements like below:

ChromeDriver driver = new ChromeDriver();

FirefoxDriver driver = new FirefoxDriver();

InternetExplorerDriver driver = new InternetExplorerDriver();

While the above will work fine, the below will give you error when you are trying to assign the object of ChromeDriver Class to a different FirefoxDriver Class.

ChromeDriver driver = new FirefoxDriver();

The above statement will give you error, as you cannot assign the object of a ChromeDriver Class to a variable of another class say FirefoxDriver class.

But, the following statements will work without any issues:

IWebDriver driver = new ChromeDriver();

IWebDriver diver = new FirefoxDriver();

IWebDriver driver = new InternetExplorerDriver();

The above three statements won’t give any errors, as IWebDriver is the parent interface of ChromeDriver, FirefoxDriver, InternetExplorerDriver classes in Selenium API.

IWebDriver Parent

 

 

 

 

As you have now understood the relationship between, IWebDriver interface and ChromeDriver, FirefoxDriver, InternetExplorerDriver classes and the other possibility of creating object creation statements.

Now, lets findout the need for creating the object creation statement like below:

IWebDriver driver = new ChromeDriver();

Let’s get started with Browser Factory program to understand the usage of IWebDriver in object creation statements.

Selenium C# – Browser Factory Program and the usage of IWebDriver interface

Follow the below steps for understanding the Browser Factory program and the usage of IWebDriver interface:

1) Browser Factory Program is nothing but a logical code, where you can pass the browser name and the logical program code will execute the Selenium C# Automation code on the desired browser (i.e. launch the desired browser based on the input browser name to the logical program code).

Lets follow the below steps for building such logical program code.

2) Launch Visual Studio, open the Project in which Selenium is already installed (Refer previous article) as shown below and also make sure that WebDriverManager is installed and configured already (Refer WebDriverManager configuration):

Selenium C# - Launch

3) Write the program as shown below to launch the desired browser using Selenium C# Automation script:

string browserName = "Chrome";

switch (browserName.ToUpper())
{

     case "CHROME":
        new WebDriverManager.DriverManager().SetUpDriver(new ChromeConfig());
        ChromeDriver cdriver = new ChromeDriver();
        break;

      case "FIREFOX":
         new WebDriverManager.DriverManager().SetUpDriver(new FirefoxConfig());
         FirefoxDriver fdriver = new FirefoxDriver();
         break;

       case "IE":
          new WebDriverManager.DriverManager().SetUpDriver(new InternetExplorerConfig());
          InternetExplorerDriver idriver = new InternetExplorerDriver();
          break;

 }

Screenshot for the above code in Visual Studio:

Selenium C# - Logic One

4) As we know that IWebDriver is the parent interface of ChromeDriver, FirefoxDriver and InternetExplorerDriver, lets replace the object creation statements as shown below:

Selenium C# - IWebDriver

5) Hover the mouse on IWebDriver and select to import ‘using OpenQA.Selenium;’ as shown below:

Selenium C# - Import

6) Observe that the errors will get resolved on the above import as shown below:

Selenium C# - Errors Resolved

7) Now globalize the IWebDriver by moving it outside the switch case by doing the below changes:

Selenium C# - Globalize

8) Now use the driver object to call one of the Selenium API method i.e. navigate().goToUrl() method for navigating to any particular web application as shown below:

Selenium C# - navigate

9) As an error is getting displayed for the driver in the above screenshot, to resolve it, assign null value to the globalized IWebDriver driver as shown below:

Selenium C# - Error Resolved

10) Save and click on the ‘Start’ button to check whether the above program is working or not.

Also experiment, by changing the browser in the browserName variable to ‘Chrome’, ‘Firefox’ & ‘IE’, and observe that the Automation Script will be executed in the desired browser and the User will be navigated to the http://omayo.blogspot.com/ in the launched browser.

You can try the above program and see whether you are able to see the above-said things.

11) Apart from the above, you can also modify the Browser Factory program logic to deal with any input given by the user to this program. i.e. What if the User gives anything other than the browser names mentioned in the switch case?

In this case, we can add a default case to the above switch case logic as shown below:

string browserName = "Chrome"; 

switch (browserName.ToUpper()) 
{ 

     case "CHROME": 
          new WebDriverManager.DriverManager().SetUpDriver(new ChromeConfig()); 
          ChromeDriver cdriver = new ChromeDriver(); 
          break; 

     case "FIREFOX": 
          new WebDriverManager.DriverManager().SetUpDriver(new FirefoxConfig()); 
          FirefoxDriver fdriver = new FirefoxDriver(); 
          break; 

     case "IE": 
          new WebDriverManager.DriverManager().SetUpDriver(new InternetExplorerConfig()); 
          InternetExplorerDriver idriver = new InternetExplorerDriver(); 
          break; 

        default:
          Console.WriteLine("Please give valid input: "+browserName);
          break;
}

Hence, from the next program, we will be using the object creation statements containing IWebDriver interface.

Next Steps:

  • > To learn more about Selenium C#, continue to the next post (Click on Next Post Link Below)
  • > Check complete Selenium C# Tutorial Contents here (Click here)

Please leave your questions/comments/feedback below.

Happy Learning ?

About Me > Naveen Repala

Connect to me on Linked In (Click here)

On a mission to contribute to the Software Testing Community in all possible ways.

<<Previous Post                            << Complete Tutorial>>                              Next Post>>

Comments (0)

Leave a Reply

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

For FREE Testing Tutorials & Videos

X
Open chat
Contact Us on Whatsapp