Headless browser automation through selenium

Shiksha Engineering
Shiksha Engineering
4 min readFeb 3, 2020

Author: Somya Jain

What is headless browser?

A headless browser is a web-browser without a graphical user interface. This program will behave just like a browser but will not show any GUI.Headless browsers are particularly used to test web pages , this is because they can easily understand the HTML pages in the same manner as any other browser, without actually launching them on the screen.

Image Credit: blog.logrocket.com

Popular headless browser automation tools available in market.

There are several headless browser tools available in the market,following are the most popular ones:

  • Headless Chrome
  • PhantomJS
  • SlimerJS
  • ZombieJS
  • HTMLUnitDriver

How is it helpful for QA?

  • Biggest advantage of carrying out headless testing is performance, Since it does not actually open a browser saves the processing power and increase test execution speed.
  • There may be setups where installing a browser is not possible, such as servers. In these cases, headless browsers help run automation tests easily.
  • Helps you multitask, browser can be used to do anything else while the tests run in the background
  • With Headless Testing, you can generate screenshots and PDFs of websites to record test results.

Techniques tried in Shiksha automation suite

Below are the detailed description of techniques tried in Shiksha automation suite.

Headless Chrome

Chrome provides an extra functionality of headless chrome from version 59 and above.

This functionality of chrome can be used directly through selenium for automation.ChromeOptions is a class in Selenium, which is used to set two arguments to ChromeDriver so that it runs in headless mode.

Check below code snippet for the arguments:

Image Credit : Self

With these arguments chromedriver will run chrome in headless mode.

Pro’s

  • Resource usage is reduced
  • Headless browsers are faster
  • Headless is ideal for monitoring your network application performance

Cons

  • It is not open for contribution from all community. Google is the only entity that will contribute to Chrome headless development.

HtmlUnitDriver

HtmlUnitDriver is the most light weight and fastest implementation of headless browser.It is known as Headless Browser Driver. It is same as Chrome, IE, or Firefox driver, but it does not have GUI.

It is platform independent and easier to run several tests concurrently. Ideal for Load Testing.

For executing headless browser through HtmlUnitDriver, you need to instantiate your driver with HtmlUnitDriver and import the libraries for same, as shown in below code snippet:

Image Credit: Self

Now the driver will open chrome in headless mode for any test execution.

Pro’s

  • Fastest implementation of WebDriver
  • Platform Independent
  • Supports Javascript

Cons

  • Emulates other browser’s JS behavior.

PhantomJS

PhantomJS is a webkit which runs headless with an inbuilt JavaScript API.It is quite fast compared to running tests using the Selenium web driver. PhantomJS uses the WebDriver Wire Protocol, which is achieved by GhostDriver .The latest release of PhatomJS has integrated GhostDriver and there is no need to separately install it.

Below are the steps need to be followed to start with phantomJS

  • Download PhantomJS (official URL: https://phantomjs.org/download.html) and extract the files in program files folder.
  • Download the PhantomJS Driver and add the jar to your project and you are ready to start.

We have to set system property of PhantomJS in place of browser driver and instantiate webdriver with phantomJS driver. Below is the code snippet to start with:

Image Credit: Self

Ghost driver will run this code in headless mode.

Pro’s

  • Automated browser testing from the cloud
  • Chrome extension for test recording

Cons

  • No active development and many pending issues.
  • Does not emulate real browser behavior.

Outcome:

Shiksha QA Automation suite has adopted the headless chrome technique for headless automation. Below are the final analyzed results.

Image Credit : Self

References:

https://www.guru99.com/selenium-with-htmlunit-driver-phantomjs.html
https://www.built.io/blog/run-selenium-tests-in-headless-browser
https://github.com/SeleniumHQ/selenium/wiki/HtmlUnitDriver
https://scotch.io/tutorials/what-is-phantomjs-and-how-is-it-used

--

--