Sunday, March 27, 2016

Selenium WebDriver : Cross Browser Testing

One of the great features of Selenium WebDriver is Cross browser testing. It allows you to create single test case which can execute across different browsers. That means you write one test case to test one functionality and you can run this test case on all of your supported browsers.
Selenium supports Firefox, Chrome, IE, Opera and Safari. It also supports headless browsers like HTML Unit Driver and PhantomJS.
This article will tell the approach to write scripts in such a way that is supports execution on IE, Chrome and FireFox. You can of course further enhance it to fit your needs.
See the code below.
class MainCall

public IWebDriver WebDriver = null;
   public static void InitializeDriver(string BrowserName)
          if (WebDriver == null)
                    string DRIVER_PATH = @"C:automationdriversFolder";

                    switch (BrowserName)
                        case "IE":
                            WebDriver = new InternetExplorerDriver(DRIVER_PATH);                         

                        case "FF": 

                            WebDriver = new FirefoxDriver();

                        case "CR":
                            WebDriver = new ChromeDriver(DRIVER_PATH);

                            WebDriver = new FirefoxDriver();                           


The code is self explanatory. You will pass the browser name and it will give you the instance of that particular browser.
To use the above method, you can call it like this in your [TestInitialize] method
        public void MyTestInitialize()
    //get the browsername from your app.config file
    string browserName = ConfigurationManager.AppSettings["BrowserName"];     
            if(MainCall.WebDriver == null)
That's it. Before executing any script, you just have to change the browser name in you app.config file. Your tests will will start executing on that particular browser.
See this sample test method.
        public void Login()
This test case can run all three browsers without changing anything in the code.
Thanks for reading this article. Feel free to share your feedback.

1 comment:

  1. I am planning to run the tests from TFS2015 CI build , how can i parameter the browsername to the script?


Testing Challenge

Can you find more than 20 defects in the below image? Write your defects in the comments section.