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);                         
                           
                            break;

                        case "FF": 

                            WebDriver = new FirefoxDriver();
                            break;


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

                        default:
                            WebDriver = new FirefoxDriver();                           
                            break;

                    }

                }
            
        }
}
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
[TestInitialize()]
        public void MyTestInitialize()
        {     
    //get the browsername from your app.config file
    string browserName = ConfigurationManager.AppSettings["BrowserName"];     
              
            if(MainCall.WebDriver == null)
                MainCall.InitializeDriver(browserName);
          
            
        }
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.
 [TestMethod]
        public void Login()
        {           
            MainCall.WebDriver.Manage().Window.Maximize();
            MainCall.WebDriver.Navigate().GoToUrl(URL);           
            MainCall.LoginPage.FillUserName(id);
            MainCall.LoginPage.FillPassword(password);
            MainCall.LoginPage.ClickSignInButton();
        }
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?

    ReplyDelete