Saturday, March 23, 2013

tackle the incompatibility of selenium jar file with firefox binary version (120 mcg)

If we don't specify the firefox binary location, Selenium web driver will use the default firefox binary location, on Windows, it is C:\Program Files\Mozilla\Firefox\. When firefox upgrade to a newer version, the web driver may fail to trigger the firefox since some API may be changed. Selenium developers will release a newer version of selenium jar file for you to upgrade, but unless you upgrade you dependency file, the test will not work and it can cause frustration.

There is a way to overcome this, to include firefox binary in a jar file and add a maven step to explode it to file system before starting the tests, and you can also include a customized profile in the jar file so it can support file download of known file formats, for example, CSV, PDF and Excel formats. It also saves each individual developer's time in manipulating profile which is also error prone. You can include firebug plugin with the binary so you can use it to examine web elements when running the test on a debug mode.

if the above step is too difficult to achieve, there is another simpler approach, just copy target firefox binary and customized profiles to a location and pass the location as a JVM option variable to the test, the test class then load the firefox binary from the location,


   public static WebDriver getWebDriver() {
      FirefoxBinary firefox = new FirefoxBinary(new File(System.getProperty("firefox.binary"));
      FirefoxProfile profile = new FirefoxProfile(new File(System.getProperty("firefox.profile")
      return new FirefoxDriver(firefox, profile);
   }
This is the constructor used by the method.

and set the following JVM options variables,

   -Dfirefox.binary=/projects/test/firefox
   -Dfirefox.profile=/projects/test/profile



/projects/test/firefox is the unix style for file system folder, it works for Windows as well, it points to C:\projects\test\firefox, where firefox.exe is located for the tests.

This solved the problem caused by the incompatibility of web driver and firefox browser version and make the test more stable.

This solution has evolved into a more advance form, a browser factory using enum

No comments:

Post a Comment