How to run Chrome in headless mode
This article will go over step-by-step instructions for running Selenium WebDriver automated tests without opening the Chrome browser–also known as running tests in headless mode. This practice helps your test automation efforts to go a step further as you will learn in this article.
Table of contents
- What is headless Chrome?
- Headless mode pros and cons
- How to run Chrome in headless mode
- Headless mode FAQs
- Conclusion
What is headless Chrome?
Headless Chrome is a modern web browsing solution that allows users to access and interact with web pages without a graphical user interface (GUI) (i.e., headless execution).
Unlike the standard Chrome browser that presents a visual interface to users, headless Chrome operates in the background and performs web-related tasks entirely in a non-graphical, automated manner.
Google Chrome introduced headless mode or headless execution to cater to developers and testers, providing a more efficient and streamlined approach to web automation, testing, and web scraping.
Advantages and disadvantages of running Selenium tests in headless mode
Key Advantages
Here are some of the key advantages of running headless Selenium tests:
- Improved Performance: Headless browsers typically consume fewer system resources than their GUI counterparts since they don’t need to render a graphical interface. This can result in faster test execution times and the ability to run more tests concurrently, improving overall performance.
- Efficient Resource Utilization: Headless browsers don’t require a graphical interface, so they can be run in environments where GUI interactions are not available or necessary, such as servers or virtual machines. This allows for more efficient resource utilization and cost savings, especially in cloud-based testing environments.
- Unattended Testing: Headless browsers enable unattended testing, meaning tests can be executed without human intervention or interaction with a visible browser window. This is particularly useful for running tests overnight or during off-peak hours, maximizing testing coverage and productivity.
- Improved Stability: Headless mode can lead to more stable and reliable test execution by reducing the likelihood of issues related to browser window focus, window size, or unexpected user interactions that can sometimes occur in GUI-based testing environments.
Key Disadvantages
The main disadvantages of headless browser testing during web application testing are as follows:
- Limited Real-World Simulation: Headless browsers do not fully simulate the user experience of a real browser because they lack a graphical user interface. This can lead to scenarios where certain visual aspects or interactions behave differently than in a real browser.
- Debugging Challenges: Debugging tests in headless mode can be more challenging since there’s no visual interface to observe the test execution. Identifying and diagnosing issues related to rendering, layout, or user interactions may require additional logging or debugging techniques.
Overall, headless mode offers several advantages that make it an attractive option for browser automation, particularly in scenarios where performance and scalability are important considerations.
How to run Chrome in headless mode
For the purpose of demoing headless mode, let’s assume that we have a Selenium test scenario where we want to get the title of a web page. Let’s see how we can achieve that using headless mode.
We start by creating an instance of ChromeOptions class. Then, we add the --headless=new
argument. Below are sample code snippets for running Chrome in headless mode in Selenium WebDriver using Java and Python as the programming language.
Please make sure that you are using Chrome 112 or later. Learn why in our article on understanding Selenium headless testing. Also, thanks to Selenium Manager, you don’t need to specify the location of browser drivers if you’re using Selenium 4 or above.
Java:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
// Set Chrome options to run a headless driver
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless=new");
// Initialize Chrome driver
WebDriver driver = new ChromeDriver(options);
// Navigate to automateNow homepage
driver.get("https://automatenow.io");
// Get and print the page title
String pageTitle = driver.getTitle();
System.out.println("Page title: " + pageTitle);
// Close the browser and end the session
driver.quit();
Code language: JavaScript (javascript)
Python:
# Set Chrome options to run a headless driver
options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
# Initialize Chrome driver
driver = webdriver.Chrome(options=options)
# Navigate to automateNow homepage
driver.get('https://automatenow.io')
# Get and print the page title
page_title = driver.titleprint("Page title:", page_title)
# Close the browser and end the session
driver.quit()
Code language: PHP (php)
In the above code examples, the test results will show that the automateNow homepage tile is printed to the console, but at no time does a browser window open during test execution.
Headless mode FAQs
Does Safari support headless mode?
As of this writing, the Safari browser does not natively support headless mode. That does not mean, however, that there may not be some workaround if you must use Safari for automated testing.
Does Firefox support headless mode?
Yes, the Firefox browser does support headless mode.
Does Edge support headless mode?
Yes, the Microsoft Edge browser does support headless mode, similar to Chrome and Firefox browsers.
Does Internet Explorer support headless mode?
No, Internet Explorer does not have a built-in headless mode like Chrome or Firefox. However, there are workarounds to achieve headless testing with Internet Explorer, though they are not as straightforward as with other browsers.
What if I don’t want to use the new headless mode in Chrome?
You can still access the old headless mode via the --headless=old
flag.
Can I still use setHeadless(true) in Selenium?
Yes. But only if you are not using Selenium 4.10.0 or later.
What is the difference between Chrome and headless Chrome?
Regular Chrome uses a GUI, and a headless Chrome browser does not.
What does a headless browser mean?
A browser that can run without opening a browser window.
What is the difference between Selenium and headless Chrome?
Selenium WebDriver is a tool used for the testing of web applications. More specifically, it helps automate actions a user would take in a web browser. Headless is a feature in Chrome that allows an automation engineer or other to run automated tests without opening the browser.
Can you run Selenium without GUI?
Yes, using headless mode in a web browser supports that feature.
Is Selenium faster in headless mode?
Selenium is slightly faster during headless test execution, given that headless testing eliminates the rendering of web pages. Observations are more obvious when running tests at scale.
Why do people use headless Chrome?
Headless mode is useful when the visual representation of a web application is not of great importance during testing.
Final thoughts on headless Selenium testing
Running tests in headless mode offers several advantages, such as faster test execution, reduced resource consumption, and the ability to run tests on servers or in continuous integration and deployment pipelines without requiring a physical display.
Headless testing is particularly useful for running tests more efficiently and scalable, especially when the application’s visual aspects are not the tests’ primary focus.
Follow our blog
Be the first to know when we publish new content.
Related articles
How to run Chrome headless in Selenium Python & Java
- How To Convert int to char in Java - May 3, 2024
- Java 2D array length: 2D array examples - April 17, 2024
- Leveraging Mobile Applications for Small Business Success - March 20, 2024