Run selenium test in headless mode with real Chrome and Firefox

Time has always been a factor to measure the efficiency and effectiveness of our automated test scripts. Being CD/CI a crucial need, its very important to run our test quickly. Best way of it is obviously running your test with headless browsers (e.g. GhostDriver, PhantomJs driver), but we have always seen below issues with these browsers-

  1. Same locators(xpaths, CSS selectors) do not work when test executed on headless browsers
  2. Additional line of code to handle cookies and other issues.
  3. Javascript alerts create problems

And there are many more problems apart from above mentioned. These problems happen as Xpath engine and java-script engine implementation varies from browser to browser and specifically for headless browsers. But what if we run our real intended browsers in headless mode during our test execution. That will obviously solve our above problem.

Selenium always surprises us with some cool new features. This time it has just blown the need for headless browsers like phantomjs etc. Now with selenium version 3.6.0 on-wards you can run your real browsers(chrome and Firefox) test in headless mode.

Now lets look at how to make our browser run in headless mode during our test execution. Please make sure to utilize this feature, you are using selenium version 3.6.0 and above. Lets go through a sample code for google test for Firefox and Chrome respectively.

System.setProperty("","Path to chrome driver exe");
ChromeOptions options = new ChromeOptions();
options.setHeadless(true); //this line is actually enables the headless mode
WebDriver driver = new ChromeDriver(options);

Similarly, we can use FirefoxOptions to enable headless mode for Firefox browser

System.setProperty("webdriver.gecko.driver","Path to gecko driver exe");
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(true); //this line is actually enables the headless mode
WebDriver driver = new FirefoxDriver(options);

Also, its very easy to do your selenium Grid configuration for your remote test execution. please refer to below code snippet.

FirefoxOptions ffoptions = new FirefoxOptions();
RemoteWebDriver driver = new RemoteWebDriver(
        new URL("http://localhost:4444/wd/hub"),

I have also done some time comparison with chrome browser headless and non headless mode. Below is time analysis for a simple test which navigate to google and search for some text :

  • Execution time in non-headless mode – 12.193 seconds
  • Execution time in headless mode – 9.321 seconds

This can tremendously reduce execution time when you will execute your large test suites.

Now, its time to say goodbye to your third party headless browsers. Execute the test with real browsers in headless mode.

Cheers, Happy Automating 🙂


Selenium 3: Firefox with Gecko Driver

Simon Stewart had announced Selenium 3 release on 25th May 2013 and it has finally beta released to use on 2nd August 2016. In this blog post I will be discussing the changes happened on implementation level when you will actually be writing code using Selenium 3.0

If you are still not aware with API level changes, then I suggest you to go through my last blog Way to Selenium 3.0 and then get back to this one 🙂

Following are some implementation level changes i have observed after writing Selenium 3.0 first program

  • You need JAVA 8+ to run Selenium 3 test
  • You need Gecko driver (like  chrome and IE driver) to run scripts on Mozilla Firefox.
  • Jar library size is now minimized to 10 MB
  • leg-rc jar is no more bundled in main selenium jar, you need to separately download it.
  • Official support for IE requires version 9 and above
  • Detailed changes in Selenium 3.0.0 can be found at link – Selenium 3 change log

So now when you run your current script with Selenium 3.0 jar files on Firefox browser,you might see below error

java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see The latest version can be downloaded from at at org.openqa.selenium.remote.service.DriverService.findExecutable( at org.openqa.selenium.firefox.GeckoDriverService.access$100( at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable( at org.openqa.selenium.remote.service.DriverService$ at org.openqa.selenium.firefox.FirefoxDriver.createCommandExecutor( at org.openqa.selenium.firefox.FirefoxDriver.<init>( at org.openqa.selenium.firefox.FirefoxDriver.<init>( at org.openqa.selenium.firefox.FirefoxDriver.<init>( at org.openqa.selenium.firefox.FirefoxDriver.<init>(

So now you need Gecko driver to execute scripts on Firefox, Gecko driver exe can be downloaded from – GeckoDriver. Now you need to specify the system property with gecko path

System.setProperty("webdriver.gecko.driver","path of geckodriver.exe");
WebDriver driver = new FirefoxDriver();

What is Geckodriver – A Proxy for using W3C WebDriver-compatible clients to interact with Gecko-based browsers. Geckodriver provides HTTP API described by the WebDriver protocol to communicate with Gecko browsers, such as Firefox (Version after 47). Even if you are working with older versions of Firefox browser, Selenium 3 expects you to set path to the driver executable by the webdriver.gecko.driver.

P.S. If you have automated build system dependent on Selenium scripts(critical cases), I recommend you to wait for official non beta release for selenium and then update the libraries of your build system. Its always better to use stable version of any software.

Happy Coding 🙂


Selenium WebDriver : An ecosystem of browser standards

Yes!.. You read that right, Selenium WebDriver : An ecosystem of browser standards. We says that WebDriver is a tool which automates browser. But looking at its W3C standard being implemented by almost every browser vendor, It seems to me a Creator of  an ecosystem for browser standards. 

Just have a look at the evolution happening around WebDriver –

  1. Selenium 3.0 is soon going to be released with most of the W3C standard
  2. Apple has announced that Safari 10.0 would have official WebDriver support
  3. Microsoft now has official support for WebDriver with Edge browser
  4. Mozilla has released Marionette(an automation driver for Mozilla’s Gecko engine)
  5. WebDriver is not able to launch FF47.0 (but that’s not an issue with WebDriver, that is a Firefox issue 😀 )

All above facts clearly stats that all the browser vendors are focusing so much to implement W3C standard , so that doing cross-browser automation testing is no more a hectic task.

Now lets spot some light on all the points discussed above.

  • Selenium 3.0 – The details about selenium 3 can be found in my previous blog post – Way to Selenium 3.0
  • Apple’s Safari 10.0 release –  WebDriver is mentioned as one of the feature of Safari 10.0 on its official release page –Apple’s Safari 10.0. It says that  – Safari on OS X supports WebDriver, which lets you automate web-content testing. It provides a set of interfaces to manipulate DOM elements and control the browser’s behavior. You can enable Remote Automation in the Develop menu and then launch the server using /usr/bin/safaridriver
  • Microsoft’s Edge release – “The Microsoft Edge implementation of WebDriver supports both the W3C WebDriver specification and the JSON Wire Protocol for backwards compatibility with existing tests”. More details can be found at –Microsoft Edge WebDriver guide
  • Marionette driver by Mozilla – Marionette is an automation driver for Mozilla’s Gecko engine. It can remotely control either the UI or the internal JavaScript of a Gecko platform, such as Firefox. It can control both the chrome (i.e. menus and functions) or the content (the webpage loaded inside the browsing context), giving a high level of control and ability to replicate user actions. In addition to performing actions on the browser, Marionette can also read the properties and attributes of the DOM.
  • Recently one of my colleague had upgraded Firefox 46.0 to 47.0 and existing WebDriver scripts was breaking with error “Firefox 47 – Unable to connect to host on port 7055 after 45000 ms“. After a lot of debugging and going through the link – Issue With FF47.0, I got to know that this is some issue with FF 47.0 and fixed in FF 48.0 which is not yet released. I just felt proud that an automation tool is identifying issues in a Browser. 😀

WebDriver has actually created an ecosystem for all browser. And when this thing would be implemented in all browsers, cross browser testing would be just about changing the browser driver.

Looking forward to see all the browsers implementing this W3C standard and making our cross browser automation work so transparent, error free and simple 🙂