Welcome to this tutorial on learning how to run test automation without opening a browser window.
In this tutorial, we will cover the following topics.
- Advantages & disadvantages of using headless mode
- How does headless mode work?
- Putting it to use
Did you know that it is possible to run an automated test without actually opening a browser window? This practice is refered to as running a test in headless mode. Although no browser is needed, we still need a browser driver and I’m going to show you how to use ChromeDriver to run tests in headless mode.
But before we move on, you may be wondering what are 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 they are listed below.
- Your tests will consume less resources from the system that they run on. Mainly because there won’t be a need to open a browser. This may be more noticeable when you are running tests in parallel and are using a significant number of concurrent threads.
- You may notice a slight improvement in test execution time. This is also due to the fact that no browser needs to open and the UI elements to not need to be displayed on the screen.
With advantages one can also expect disadvantages. Below is a list of some of those.
- Tests will become more difficult to debug. The fact that you can’t see what is happening when a test runs, makes it more difficult during the debugging process.
How does headless mode work?
You may be wondering how WebDriver is going to be able to find elements and click of them when no browser window will open. The reality is that whatever website you are visiting is rendered in memory and this is how WebDriver is able to 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!
Putting it to use
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");
The same result may be accomplished by using the setHeadless() method as follows.
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);
The following video will show you how things look when a test is run in headless mode.
Running tests in headless mode can be advantageous when system resources are a concern or when you need your tests to run a little bit faster.