Running headless chrome in Selenium
What is a chrome headless mode?
Headless Chrome is shipping in Chrome 59. It’s a way to run the Chrome browser in a headless environment. Essentially, running Chrome without chrome! It brings all modern web platform features provided by Chromium and the Blink rendering engine to the command line.
Why is that useful?
A headless browser is a great tool for automated testing and server environments where you don’t need a visible UI shell. For example, you may want to run some tests against a real web page, create a PDF of it, or just inspect how the browser renders a URL.
What use will it provide for a test framework?
When you are using the selenium web driver for automated test frameworks you will probably use firefox driver or the chrome driver for executing the test cases. But did you know that there is a very useful feature that allows you to customize the chrome driver with varies arguments? Yes, the new chrome driver offers a large number of features that will come in handy when you are implementing your automated test framework! Let’s dive right into it.
What are the pre-requisites?
First, you need to import chrome driver and chrome options to your project. Here’s how you do it,
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
Then you need to create a chrome options object in order to add arguments to the chrome driver.
ChromeOptions chromeOptions = new ChromeOptions()
Now you can add multiple arguments for your driver. Let’s see how we can add headless mode.
chromeOptions.addArguments(“ — headless”)
Then simply pass the chrome options object to the web driver instance.
WebDriver Driver = new ChromeDriver(chromeOptions)
Done! Like this, you can run chrome silently in headless mode and still get the logs on your CMD ;) Let me share some useful arguments you can use for chrome options,
chromeOptions.addArguments("-- any-of-the-following")
- start-maximized
- disable-extensions
- verbose
- disable-web-security
- allow-running-insecure-content
- allow-insecure-localhost
- no-sandbox
- disable-gpu
- incognito
- allow-file-access-from-files
I hope this helped you to understand how to use the chrome options feature in selenium.
Also if you have other / more ideas, please share them in the comments, then I can update the article.
Happy Testing.