info@pragmatictesters.com +94 11 253 8512


Introduction

 

Headless browser testing very important when you want run your Selenium test suites efficiently. Popular headless browsers supported by Selenium WebDriver are taken off from v3.6.0. It is time to switch to recently included headless browsers.

We have discuss What, Why, When and How of the headless testing. Our recommendations for the testers are discussed.

Sample code snippets are provided at the end of the article. We have included few references for your additional reading.


What is headless testing ?

Executing the web applications’ UI tests without opening a browser’s graphical user interface is called headless testing. Headless browser acts similar to a normal web browser. Testers have full control over the web pages loaded into the headless browsers.  Only difference is you will not see a graphical user interface.


Why headless testing is required?

Headless testing is useful when you don’t want to see the real interaction with a web browser when test cases are  executed.

  1. Increase the test execution speed
  2. Run the test in parallel without causing heavy resource consumption at the client side
  3. Run the tests on a machine without GUI
  4. During continuous integration where you don’t need to see GUI

Usually web browsers are consuming most of the RAM.

Chrome may be the best browser around, but it eats up your PC’s RAM like turkey on Thanksgiving.

 


Selenium WebDriver support for headless testing

Selenium WebDriver  used to support HTMLUnit Driver and PahntomJS until recently. They were popular among the testers.

Following is extract from official release notes v3.6.0

Removed direct dependency on HTMLUnit and PhantomJS from Maven Artifacts

Added options to start Firefox and Google Chrome in headless modes.


What are the supported browsers?

 

  1. Google Chrome
  2. Mozilla Firefox
  3. HTMLUnit Driver
  4. PhantomJS
  5. TrifleJS
  6. ZombieJS

There are number of headless browsers available to the testers in addition to the above list. HTMLUnit Driver and PhantomJS have been very popular browsers for Selenium headless testing.

Since Chrome and Firefox are supporting headless mode it is recommended to switch to them considering support for the browsers in future. It is noted that the PhantomJS will no longer be supported as the project lead stepped down.

 

“I think people will switch to it (Google Chrome), eventually. Chrome is faster and more stable than PhantomJS. And it doesn’t eat memory like crazy.

I don’t see any future in developing PhantomJS. Developing PhantomJS 2 and 2.5 as a single developer is a bloody hell.
Even with recently released 2.5 Beta version with new and shiny QtWebKit, I can’t physically support all 3 platforms at once (I even bought the Mac for that!). We have no support.

 

Vitaly Slobodin, the former maintainer of PhantomJS


When to use headless testing ?

We can use headless testing once the cross browser testing is completed and want to run regression test cases in subsequent releases and with continuous integration.

You have no option other than using headless testing when your machine does not have a GUI.

It is recommended to use headless browser when tests are executed in parallel as we know GUI based browsers consumes a lot of resources. Hence headless browsers can be used for server side performance testing too.


How to do headless testing ?

Browser initialization section of your Selenium code will need following code changes based on your headless browser.

Your should be able to switch between the browsers.


Using Google Chrome

System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");

ChromeOptions options = new ChromeOptions();

options.addArguments("headless");

WebDriver driver = new ChromeDriver(options);

driver.get("http://pragmatictestlabs.com");

 


Using Firefox

FirefoxBinary firefoxBinary = new FirefoxBinary();

firefoxBinary.addCommandLineOptions("--headless");

System.setProperty("webdriver.gecko.driver","drivers/geckodriver.exe");

FirefoxOptions firefoxOptions = new FirefoxOptions();

firefoxOptions.setBinary(firefoxBinary);

FirefoxDriver driver = new FirefoxDriver(firefoxOptions);


Using HTMLUnitDriver

  • Without Javascript support

WebDriver driver;

 driver = new HtmlUnitDriver();
  • With Javascript support

WebDriver driver;

driver = new HtmlUnitDriver(true);

Working with HTMLUnitDriver after Selenium WebDriver 3.6.0

If you want to continue with the HTMLUnitDriver you will  have couple of options.

  1. Stick to the lastly supported version of Selenium WebDriver 3.5.3
  2. Work with the latest version of Selenium WebDriver (e.g 3.7.1 at the time of writing) with following dependencies
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/htmlunit-driver -->
<dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>htmlunit-driver</artifactId>
   <version>2.28</version>
</dependency>


Using PhantomJS

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "drivers/phantomjs.exe");
WebDriver driver = new PhantomJSDriver(capabilities);


Testing behind a proxy

If you are behind a proxy server additional configuration has to be done along with the headless browser configuration.

  • HTMLUnitDriver
HtmlUnitDriver driver = new HtmlUnitDriver(); 
Proxy proxy = new Proxy();
proxy.setHttpProxy("PROXY_HOST, PROXY_PORT"); 
driver.setProxySettings(proxy);
  • PhanthomJSDriver
ArrayList<String> cliArgsCap = new ArrayList<String>();
cliArgsCap.add("--proxy=127.0.0.1:1024");
cliArgsCap.add("--proxy-type=socks5");
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability("takesScreenshot", true);
caps.setCapability("screen-resolution", "1280x1024");
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:\\phantomjs.exe");
caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);
driver = new PhantomJSDriver(caps);


Download

 

  1. Presentation + Sample code for Headless Browser Testing

 


References

 

 

 

 

 

 

 

Author: Janesh Kodikara

Performance Tester | JMeter Trainer | Software Testing Service Provider

Your Turn To Talk

Leave a reply:

Your email address will not be published.