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,
- 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.
- 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!
- 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.
- 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.
- Parallel execution – headless prevents opening multiple browsers and lets you multitask.
- 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,
- 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.
- When real users of the applications are to be mimicked
- 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
options.addArguments("--headless");
// To test responsive websites
options.addArguments("window-size=1400,600");
// 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
driver.get(baseUrl);
// Print messages to console
System.out.println(driver.getTitle());
System.out.println("Hello World!");
// Close the IE browser
driver.quit();
}
}
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();
options.setHeadless(true);
//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
driver.get(baseUrl);
//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
if(pageTitle.equals(expectedTitle)){
System.out.println("Hello World! Result is as expected.");
}else{
System.out.println("Hello World! Assertion failed!");
}
//Close the Firefox browser
driver.quit();
}
}
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.
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!
Thank you very much for this perfect selenium guide 🙂
Thank you shilpa