Selenium is one of the popular automation tools available today. It automates the testing of web applications against browsers such as Firefox, Chrome, Edge, IE etc. Launching a browser and then executing the test cases on the browsers are an essential part of the web automation testing.
What is Headless browser?
A headless browser works like other browsers, but the only difference is they aren’t visual on a desktop which means there is no graphical user interface (GUI).
A headless browser is used to automate the browser without launching it. While the tests are running, we could not see the browser, but we can see the test results coming on the console.
When the web page is not rendered on the screen, and the tests are executed without UI interaction, obviously, the execution gets faster as compared to real browser automation.
We can use headless testing once the cross browser testing is completed and want to run regression test cases in subsequent releases.
Benefits of headless testing:
Helps in multi tasking
Beneficial in web scraping
Faster automation test execution
Useful in CI pipeline
There are various examples of headless browsers available listed below:
HTMLUnit
Firefox
Chrome
Splash
PhantomJS
Headless Chrome browser
Headless chrome is supported from version 60+ and available for windows,Linux and macOS.
Syntax for chrome in Headless mode:
ChromeOptions options=new ChromeOptions(); options.addArguments("--headless");
ChromeOptions runs the tests on headless mode by passing an argument as "headless" or "--headless" as above .
Passing true as an argument in setHeadless() method can also be used to perform headless browser automation.
Use those options while creating the instance of the ChromeDriver.
ChromeOptions options=new ChromeOptions(); options.setHeadless(true); WebDriver driver = new ChromeDriver(options);
Example for Headless Chrome browser:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
public class HeadlessBrowser {
@Test
public void headless() {
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.setHeadless(true);
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.selenium.dev/");
System.out.println(driver.getTitle());
}
}
Without opening the UI of the chrome browser the output is printed in the console as below:
Headless Firefox browser
It is similar to setting up the chrome options.
FirefoxOptions options = new FirefoxOptions(); options.addArguments("--headless"); WebDriver driver = new FirefoxDriver(options);
Example for Headless Firefox browser:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
public class HeadlessBrowser {
@Test
public void headless() {
WebDriverManager.firefoxdriver().setup();
FirefoxOptions options = new FirefoxOptions();
options.addArguments("--headless");
WebDriver driver = new FirefoxDriver(options);
driver.get("https://www.selenium.dev/");
System.out.println(driver.getTitle());
}
}
In the below output in console we see a statement mentioned as "*** You are running in headless mode".
HTMLUnit Driver
HtmlUnitDriver is headless driver providing non-GUI implementation of Selenium WebDriver. It is based on HtmlUnit, fastest and light-weight browser implemented in Java.It is same as Chrome, IE, or FireFox driver, but it does not have GUI so one cannot see the test execution on screen.
Advantages of HTMLUnit driver
Fastest implementation of WebDriver compared to other browsers.
HtmlUnitDriver is platform-independent.
HtmlUnitDriver supports JavaScript.
It is platform independent and easier to run several tests concurrently.
Add "htmlunit-driver" dependency in pom.xml file. Similar to other browsers we need to create an object for the class to run the code in the headless mode.
HtmlUnitDriver driver =new HtmlUnitDriver();
Example for HTMLUnit Driver:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
public class HeadlessBrowser {
@Test
public void headless() {
HtmlUnitDriver driver =new HtmlUnitDriver();
driver.get("https://www.google.com/");
System.out.println("Title of the web page is:"+driver.getTitle());
System.out.println("Current url is: " + driver.getCurrentUrl());
}
}
The output for the above code as:
Disadvantages of HTMLUnit driver
The use of HtmlUnitDriver is not possible for complex websites.
The generation of screenshots is not possible with HtmlUnitDriver.
While comparing with real browser testing, for headless browsers like HtmlUnitDriver, it becomes very difficult for debugging the script.
Headless Edge browser
Microsoft Edge browser can also be run in headless mode, just like Chrome and Firefox. EdgeOptions class of Selenium is used to manage options specific to the Edge browser. addArguments() method of EdgeOptions helps to run the tests in the headless mode by passing headless or –headless as an argument.
Syntax for Edge browser in Headless mode:
EdgeOptions options=new EdgeOptions();
options.addArguments("--headless");
Advantages of Selenium Headless Testing
It is extremely fast compared to real browser.
Improves test execution performance 2x faster than real browser testing.
It is suitable for parallel testing. UI-based browsers consume a lot of memory and resources. So, here Headless browser is the better option for use.
Disadvantages of Selenium Headless Testing
Hard to debug inconsistent failures on locating elements due to page loading too fast.
In Real browser as functions are performing in front of user and can interact with it , so can detect easily where the test goes fail. And debug if anything goes wrong.
Headless browsers don't mimic the exact user behavior, as the page doesn't render precisely with all the dependencies that it will render in an actual browser.
It doesn't have UI , so it may not report errors related with images.
Taking screenshot is difficult in headless browser testing.
Conclusion
The benefits of using headless browsers is performance and in fact it is faster. Headless Browser testing plays a major role when performance and time are crucial.
In the case where there exists user involvement, Real Browser testing can be chosen. If there are no UI presentation requirements to perform the testing quickly, then one can go for Headless Browser testing.
Comments