This article will show how to run the Chrome browser headless in Selenium WebDriver. Headless means that your tests will run without opening the Chrome browser.
Let’s dive in
- Advantages & disadvantages of using headless mode
- How does headless mode work?
- How to run Chrome in headless mode
Did you know that it is possible to run an automated test without actually opening a browser window? This practice is referred to as running a test in headless mode. A browser driver is still needed, although a browser is not. We will show you how to use ChromeDriver to run tests in headless mode.
But before we move on, you may be wondering about the advantages and disadvantages of running tests in headless mode. Let’s take a look at that next.
Advantages & disadvantages of using headless mode
Believe it or not, there are some advantages to running tests in this way, and of course, disadvantages.
Here are the main advantages.
- Your tests will consume fewer resources from the system they run on, mainly because there won’t be a need to open a browser. It will be more noticeable when you run tests in parallel and use many concurrent threads.
- You may notice a slight improvement in test execution time due to not needing to open a browser.
The main disadvantage is below.
- Tests will become more challenging to debug. You can’t see what is happening when a test runs, making it more difficult during the debugging process.
How does headless mode work?
You may be wondering how WebDriver will be able to find elements and click on them when no browser window opens. What happens is that whatever website you visit is rendered in memory, and this is how WebDriver can locate elements.
Another thing to note here is that you will still be able to take a screenshot of a web page or a web element when running in headless mode!
How to run Chrome in headless mode
In order to run your tests in headless mode, you will need to use the ChromeOptions as follows.
ChromeOptions options = new ChromeOptions(); options.addArguments("--headless");
By using setHeadless() method, you will accomplish the same result as shown below.
ChromeOptions options = new ChromeOptions(); options.setHeadless(true);
Lastly, you need to pass the options as an argument when instantiating the ChromeDriver.
ChromeOptions options = new ChromeOptions(); options.setHeadless(true); WebDriver driver = new ChromeDriver(options);
If you want to see how to run Chrome headless in Selenium, watch the following video.
Running tests in headless mode can be advantageous when system resources are a concern or when you need your tests to run faster. However, you should not expect a significant decrease in test execution times.