10m. Advanced WebDriver – Running tests in headless mode

I am sure you would have heard of headless by now because Google introduced headless option for their Chrome browser eons back (from version 59)! Firefox is not far behind. It can be run in headless mode too!! Let’s execute some automated selenium tests in Firefox, without Firefox! πŸ˜‰ In other words, tests run in the background and there is no display (i.e. the browser UI is not displayed to the user). The code will be provided for the Chrome browser as well.

The first question that might pop up in your brain is, why would I ever need a headless browser in the first place? I hear from you. Don’t worry! I have got answers and trust me, these are good answers. So, without further ado, below are a few reasons why you would want to go headless,

  1. If your goal is to achieve better speed and performance, then look no further! Because there is no need to start up the browser UI for every test, bypassing all the time required to load JavaScript, CSS and rendering HTML, tests run faster.
  2. In CI (Continuous Integration) pipeline, usually, it is required to run on servers or on systems like Linux OS without an actual GUI. Headless mode saves the day!
  3. What if all your tests run for hours? In this case, the user must literally keep looking at the screen, letting the system do its job until all tests are completed running as they take up the whole screen. Instead, if executed in headless mode, then the user can continue working on other tasks as the tests are run in the background.
  4. When the scripts are developed and are stable, there is no real need to see them running. Running them in headless mode in a much faster way seems meaningful. With the help of logs, required debugging can be performed.
  5. Parallel execution – headless prevents opening multiple browsers and lets you multitask.
  6. Taking screenshots is possible with headless mode too. So, upon failure, a snapshot can always be obtained and stored away in whichever way required.

But, behold! There are times when headless might not be very useful. For instance,

  1. While developing scripts, running them in a traditional browser would help us to see visually what exactly is going on, making debugging easy at initial phases of automation.
  2. When real users of the applications are to be mimicked
  3. Might not catch some bugs like images crashing or not loading

With all that said, let us see some working code!

Chrome in headless mode,

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class HelloWorld_ChromeHeadless {
	public static void main(String[] args) {
		// System property set up
		System.setProperty("webdriver.chrome.driver", "browser-drivers\\chromedriver.exe");
		// Add options to Google Chrome
		ChromeOptions options = new ChromeOptions();
		// Setting headless argument
		// To test responsive websites
		// Create a new instance for the class ChromeDriver
		// that implements WebDriver interface
		WebDriver driver = new ChromeDriver(options);
		// Assign the URL to be invoked to a String variable
		String baseUrl = "https://www.google.com";
		// Open baseUrl in IE browser window
		// Print messages to console
		System.out.println("Hello World!"); 	
		// Close the IE browser

Firefox in headless mode,

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
public class HelloWorldFirefox_Headless {
	public static void main(String[] args) {
		//Selenium version 3 beta releases require system property set up
		System.setProperty("webdriver.gecko.driver", "browser-drivers\\geckodriver.exe");
		FirefoxOptions options = new FirefoxOptions();
		//Create a new instance for the class FirefoxDriver
		//that implements WebDriver interface
		WebDriver driver = new FirefoxDriver(options);
		//Assign the URL to be invoked to a String variable
		String baseUrl = "https://www.google.com";
		String pageTitle = "";
		String expectedTitle = "Google";
		//Open baseUrl in Firefox browser window
		//Get the page title and assign to a String variable
		pageTitle = driver.getTitle();
		//Check if obtained page title matches with the expected title
		//and print the console output accordingly
			System.out.println("Hello World! Result is as expected.");
			System.out.println("Hello World! Assertion failed!");
		//Close the Firefox browser

Execution Result:

Comments are provided for each line of code making it self-explanatory.

The console window shows the absence of any errors. It also shows that the test is running in headless mode and all the messages are printed as expected.

Eclipse console output – Firefox in headless mode

Good luck with experimenting with today’s skills. I strongly believe that these will be useful at some point in your automation journey.

Have a great day!

Lakshmi Chandana is a Software professional + passion-fueled blogger + novel-reader + artist + tutor to make your day a little brighter than it was before! She is thrilled you are here! She is on a mission to make sure learning sticks but with the fun part kept intact. She uses certain tricks called BrainBells (inspired from barbells and dumbbells used for workout) to achieve this and she says, β€œthis is not the hardest job! As once minions said, it for sure is working in a bubble wrap factory. Imagine the self-control needed!” So, dive in to explore the fun-filled World of Learning!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top Search