Built.io joins Software AG! Read all about the news.

Built.io Blog

Run Selenium Tests In Headless Browser


Selenium is one of the most popular and efficient browser automation tools available today. With Selenium Web Driver, it automates several (browser) actions, such as opening a webpage, clicking a link, checking the URL, and so on.

But there are cases when you may need to run automation tests in ‘headless’ mode, i.e., when no browser is being displayed. In these cases, you can execute Selenium tests in headless browsers.

What Is A Headless Browser?

A headless browser is a browser simulation program that does not have a user interface. These programs operate like any other browser, but do not display any UI. When Selenium tests are run, it executes in the background.

While there are several such headless browsers available in the market, the following are the most popular ones:

  • Headless Chrome
  • PhantomJS
  • SlimerJS
  • TrifleJS
  • HTMLUnit driver

Three Benefits Of Headless Browser Testing

1. Improves speed and performance

Since this type of testing does not actually open a browser, the system saves the processing power that would otherwise be used in a real browser test. Consequently, the tests are executed faster.

2. Allows testing browserless setups

There may be setups where installing a browser is not possible, such as servers. In these cases, headless browsers help run automation tests easily.

3. Helps you multitask

You can use your browser or your machine to do anything else while the tests run in the background. Save hours of time that is otherwise spent staring at the screen.

How To Run Selenium Tests In Headless Google Chrome

Google recently introduced a headless option for Chrome. It is available from version 59. So, if you are planning to run headless tests, you need Google Chrome browser with version 59 or above.

Before you can begin testing, there are a couple of things you’ll need to install first:

  • Java
  • Selenium
  • ChromeDriver (latest)

ChromeOptions is a class in Selenium to set the arguments to ChromeDriver. In the following example, we will pass the two arguments to ChromeDriver to run in headless mode.

package chrome;
       public class HeadlessTesting {
            public static void main(String[] args) throws IOException{
                ChromeOptions options = new ChromeOptions();
                WebDriver driver = new ChromeDriver(options);
                System.out.println("title is: " + driver.getTitle());
                File scrFile = ((TakesScreenshot) driver)
                FileUtils.copyFile(scrFile, new File("pathTOSaveFile"));

If you run this code, it will print the title of the page and will take the screenshot as well.

Don’t forget to also check out these other related blog posts:

  1. Getting started with Selenium Web Driver
  2. Create HTML reporting in Selenium Web Driver
  3. Run Selenium 3 tests in Firefox using GeckoDriver proxy

Popular Posts

Subscribe to our blog