In this post we will read about headless browser automation and how to use or invoke different headless browser using selenium. First of all lets understand What is a headless browser?
A headless browser is a web-browser without a GUI-Graphical User Interface. The major difference with browser having User Interface(UI) is it did not load Web page in UI therefore a lot of time is saved.
Some of the examples of Headless Drivers include-
1-Chrome-Can be accessed using selenium(V3.6 and above)
2-Firefox-Can be accessed using selenium(V3.6 and above)
3-HtmlUnit-Can be accessed using selenium(V3.6 and above)
The Release Notes of Selenium v3.6.0 clearly mentions Add options to start Firefox and Chrome in headless modes. So we need to use the Options Class as follows to invoke the headless argument
How to Invoke headless execution in Chrome browser-
chromepath = "./Resources/chromedriver";
System.setProperty("webdriver.chrome.driver", chromepath+".exe" );
ChromeOptions obj_CO = new ChromeOptions();
driver = new ChromeDriver(obj_CO);
How to Invoke headless execution in Firefox browser-
options = Options()
driver = webdriver.Firefox(firefox_options=options, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
print("Firefox Headless Browser Invoked")
How to Invoke headless execution in HtmkUnitDriver browser-
WebDriver driver = new HtmlUnitDriver();
How to Invoke headless driver in phantomJS
Once after download, same needs to be configured in Buildpath, as an external java class file and can be used as-
File file = new File("C:/Program Filesx86/phantomjs-2.0.1-windows/bin/phantomjs.exe");
WebDriver driver = new PhantomJSDriver();
Since there is no User Interface is loaded in headless browsers therefore it saves a lot of time, It too have several benefits and disadvantages as well. first we can have look where it can be uses-
Where to use Headless browser-
1- Acceptance test
2- For test data preparation
3- For doing Unit testing
4- Cross browser testing
5- Running tests on a headless system like Linux OS etc
When to avoid using headless browser-
1- While doing full regression/end-to-end tests as User Interface has a lot of significance there.
2- While emulating actual user behavior
3- More debugging is required during Unit Testing.
Couple of Questions/With Answers-
1- Can we take screenshot during headless test automation?
Answer-Yes, Your can take screenshot and it will bring exact User Interface of the Interface even though it is running in headless mode
2- Best Recommended usage?
Answer-Use to create test data, doing Acceptance test, Perform sanity test and Unit testing of code.
3- Can this be used in CI/CD pipeline like-Jenkins, Visual studio online and Bamboo etc?
Answer-Yes, Headless browser automation can be used in CI/CD pipeline.
Headless browser was not considered reliable earlier but now selenium has started covering the same in its API’s . At the same time headless browser community is getting stronger day-by-day, one of the dev community of chrome headless group is – email@example.com.
Headless browser automation can be an example of automation of automaition
Even today organisation are not totally relying on headless automation but i believe soon we will have limited set of testing/development can be planned on headless only as the results of headless are much better then browser with User Interface, Saves a lot – lot of time – it saves time 2 times to many times it reaches 10 times then actual driver.