Quote

"Between stimulus and response there is a space. In that space is our power to choose our response.
In our response lies our growth and freedom"


“The only way to discover the limits of the possible is to go beyond them into the impossible.”


Thursday 19 January 2012

Comparative analysis of the WebDrivers’ Drivers

While working with selenium webdriver you have an option to choose from multiple webdrivers, such as HtmlUnitDriver, FirefoxDriver, InternetExplorerDriver, ChromeDriver and OperaDriver. Each one of them is a separate implementation of the WebDriver interface provided by Selenium. All of them have unique advantages and disadvantages because of the implementation differences for catering to different requirements. It is good to know the comparative advantages and disadvantages of each one before picking one of them.
The first major point to note is that there are two groups of driver implementations. One of those that invoke the actual browser installed on your system and the other that emulates the behavior of another browser. FirefoxDriver, InternetExplorerDriver, ChromeDriver and OperaDriver invoke the actual browser installed on the machine; however the HtmlUnitDriver emulates other browsers JS behavior. 


HtmlUnitDriver

 

HtmlUnit is a java based framework for testing webApps basically a wrapper around ‘HttpClient’ by Jakarta. HtmlUnit provides UI-Less emulation of browsers to test web applications. The HtmlUnit APIs let you do the typical functions performed in an actual web browser, such as click links, fill forms, invoke web pages, submit values etc. HtmlUnit supports java script and complex AJAX libraries. Javascript is disabled in the HtmlUnitDriver by default, however if it can be enabled if required. The mechanism to enable javascript with the HtmlUnitDriver is as follows:
HtmlUnitDriver MyhtmlDriver = new HtmlUnitDriver();
 MyhtmlDriver.setJavascriptEnabled(true);
OR
HtmlUnitDriver MyhtmlDriver = new HtmlUnitDriver(true);

When enabled, the HtmlUnitDriver emulates the java script behavior of Internet Explorer by default. However we can direct the HtmlUnitDriver to emulate the JavaScript behavior of the browser of our choice by invoking the constructor that accepts the browser version. This can be done as follows:
HtmlUnitDriver MyhtmlDriver = new HtmlUnitDriver(BrowserVersion.Firefox_2);


Internet Explorer Driver

 

The IE driver class ‘InternetExplorerDriver.class’ is located at ‘\org\openqa\selenium\ie\’ directory in the ‘selenium-server-standalone-2.7.0.jar’. To use the InternetExplorerDriver all you need to do is to have the selenium-server-standalone-2.7.0.jar in your CLASSPATH.  The IE driver runs only on windows and supports both 32-bit and 64-bit operations. Depending on the thread that instantiates the InternetExplorerDriver corresponding version of the IE is launched i.e. if the thread instantiating driver is running in 32-bit then the 32-bit version of the IE will be launched and if the thread instantiating driver is running in 64-bit then the 64-bit version of the IE will be launched.
The Internet Explorer driver uses the native or OS-level events to perform various functions on the browser, such as inputs from keyboard and mouse. This approach has both advantages and limitations both. The advantages are that it bypasses the limitations of the Javascript sandbox but there can be issues like the browser window under test might be out of focus.


Firefox Driver

 

‘selenium-server-standalone-X.X.X.jar’ contains all the drivers implementing the WebDriver interface of selenium. Hence the 'FirefoxDriver.class' can be found in the ‘\org\openqa\selenium\firefox directory in the selenium-server-standalone-X.X.X.jar. The driver when instantiated is added as an extension to the firefox profile. You can specify the profile with which you want to load firefox session. If no profile is specified then the driver creates an anonymous profile by default
A profile can be created for the firefox driver as follows:

FirefoxProfile myProfile = new FirefoxProfile();

Multiple operations can be performed on the newly created profile, such as:

myProfile.addExtension(File);
myProfile.clean();
myProfile.getPort();
myProfile.setPort(int port);
myProfile.enableNativeEvents();
myProfile.setPreference(String key, String value); etc...  

After creating the your profile you can pass the profile while creating the driver instance as follows:
WebDriver myFireFoxDriver = new FirefoxDriver(myProfile);


This driver is faster than the InternetExplorerDriver and executes the test in real Firefox web browser.

10 comments:

  1. I like the way , you have explained about the different drivers . Eagerly waiting details of other drivers.
    Definitely webdriver is really powerful for browser automation. It has the capability to automate different type of web application very easily.

    What do you recommend , which driver is best to use for automation? if i have option to choose any?

    ReplyDelete
  2. If GUI experience is not required then I would recommend the HtmlUnitDriver. However amoung GUI based drivers I found FirefoxDriver to be the smoothest and the fastest.
    I would reccomend to use IEDriver only when necessary because it has issues because of the operations using native events lead to the browser being out of focus and commands timing out. I have myself faced this with IEDriver.

    ReplyDelete
  3. Great article !!
    i am facing problem with confirmation popup during onload() event handlers of its pages. please suggest your views, however for the time being i have added visual automation code using sikuli to click on that popup and on some places i have added selenium.keyPressNative(Integer.toString(KeyEvent.VK_ENTER));
    but it works only on active window :(

    ReplyDelete
    Replies
    1. For pop-ups it is recommended to use WebDriver.switchTo().window() method to switch to the popup window and then perform the required operation. WebDriver.getWindowHandles() can be used to get the list of all windows and the switchTo().window() method can be used to switch between different windows.

      Delete
  4. Hi,
    I am using HtmlUnitDriver for automating my web application. I migrated from FirefoxDriver to HtmlUnitDriver and the code that works on Firefox does not work here. I have just tried a simple example and it does not work. The code is as follows :

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.htmlunit.HtmlUnitDriver;


    public class Testing {

    public static void main(String[] args) {


    HtmlUnitDriver = new HtmlUnitDriver ();

    driver.get("http://www.google.com");
    WebElement element = driver.findElement(By.name("q"));

    element.sendKeys("Cheese!");
    element.submit() System.out.println("Page title is: " +driver.getTitle());
    }
    }
    I am stuck up and need some urgent help. Can you please tell me where am i going wrong. I am using Selenium version 2.28.0

    ReplyDelete
  5. Change HtmlUnitDriver = new HtmlUnitDriver (); to the following:
    HtmlUnitDriver driver = new HtmlUnitDriver ();

    The HtmlUnitDriver instance creation was erroneous.

    ReplyDelete
  6. I have tried that as well. Still does not work. Please help.

    ReplyDelete
  7. A semicolon needs to be added after element.submit() as in element.submit();

    Would suggest to learn some basic debugging techniques to get over these kind of issues.

    ReplyDelete
  8. The code is fine and works perfect on Firefox. the semicolon was missed while i was pasting the code here. there is no problem with the code as it runs as expected on Firefox but while using HtmlUnitDriver i get an exception "org.openqa.selenium.NoSuchElementException: Unable to locate element with name: q". Kindly help.

    ReplyDelete
    Replies
    1. After making the above changes code works fine. The output is: 'Page title is: Cheese! - Google Search'

      The code is as follows:
      import org.openqa.selenium.By;
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.WebElement;
      import org.openqa.selenium.htmlunit.HtmlUnitDriver;


      public class Testing {

      public static void main(String[] args) {


      HtmlUnitDriver driver = new HtmlUnitDriver ();

      driver.get("http://www.google.com");
      WebElement element = driver.findElement(By.name("q"));

      element.sendKeys("Cheese!");
      element.submit();
      System.out.println("Page title is: " +driver.getTitle());
      }
      }

      Delete