Starting with version 60, the Chrome browser introduced the ability to run in headless mode. We now have the ability to launch the browser without creating a visual browser window. This will allow you to run tests faster and with fewer resources, and most importantly, it will allow you to run tests on systems without a graphical component. Don’t worry — the ability to take screenshots won’t be affected in any way.

In this article, I want to demonstrate how you can use this functionality.

The beginning of any auto test starts as standard, we specify the path to our chrome driver:

System.setProperty("webdriver.chrome.driver", "/path/to/driver");

After this thing, we have to do is to create a WebDriver object, and set the ChromeDriver path and some arguments :

ChromeOptions options = new ChromeOptions();options.addArguments("--headless");WebDriver driver = new ChromeDriver(options);

That is all. You can run it.

However, there is one problem. The invisible browser window is only 800x600 in size. Therefore, you need to set the desired screen size with an additional argument:

ChromeOptions options = new ChromeOptions();options.addArguments("--headless", "--window-size=1920,1200");WebDriver driver = new ChromeDriver(options);

As a rule, you should also specify disabling GPU rendering, extensions, and disabling pop-up extensions in developer mode. This usually results in very ugly and unreadable code.

ChromeOptions options = new ChromeOptions();options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors","--disable-extensions","--no-sandbox","--disable-dev-shm-usage");WebDriver driver = new ChromeDriver(options);

#google-chrome #selenium #testing #web-development #developer

How to Run a Headless Chrome Browser in Selenium WebDriver
30.15 GEEK