What is Headless browser automation

Test Automation

Hello Readers,

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)
4-PhantomJS

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-

String chromepath;
chromepath = "./Resources/chromedriver";
System.setProperty("webdriver.chrome.driver", chromepath+".exe" );
ChromeOptions obj_CO = new ChromeOptions();
obj_CO.addArguments("window-size=1400,800");
obj_CO.addArguments("headless");
driver = new ChromeDriver(obj_CO);
driver.get('http://google.com/')

How to Invoke headless execution in Firefox browser-

options = Options()
options.add_argument("--headless")
driver = webdriver.Firefox(firefox_options=options, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
print("Firefox Headless Browser Invoked")
driver.get('http://google.com/')

How to Invoke headless execution in HtmkUnitDriver browser-

WebDriver driver = new HtmlUnitDriver();
driver.get('http://google.com/')

How to Invoke headless driver in phantomJS

PhantomJS is a headless browser with JavaScript API. PhantomjS needs to be downloaded

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");
System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
WebDriver driver = new PhantomJSDriver();
driver.get("http://www.google.com");

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.

Summary-

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 – headless-dev@chromium.org.

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.

Its Prashant Chaudhary, I have 10+ years of experience in software test automation, performance & process automation with various commercial and open source tools. This blog shares my experience on the topic of Automation. The content published on my blog is solely mine, it does not express the views of any of my employers (current or previous).
No Comments

Leave a Reply

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

Selenium Basic
1
Step by step selenium – step 3, what is findelement method

Abstract- So far, we have learned how to launch a web browser, entering values in a text box and clicking button. In this post, we will learn about findelement method. Test automation is all about get and set, which means get from web and set into web. Talking more specifically- …

Test Automation
1
What is ITestListner interface and its Use in Selenium

There are several interfaces available in TestNG that allow you to modify TestNG’s behavior or to give test scripts additional strength. These interfaces are broadly called “TestNG Listeners”. Here are a few listeners which are commonly used: 1- IAnnotationTransformer 2- IAnnotationTransformer2 3- IHookable 4- IInvokedMethodListener 5- IMethodInterceptor 6- IReporter 7- …

Test Automation
How to Capture Screenshot in Selenium Webdriver-3 different ways

Abstract– Capturing screenshot is an essential feature of a test automation framework, They help in defect analyses once after test execution is over. In this blog post we will see how to capture screenshot, there are different ways to capture screenshots. Which includes- 1- Capture full Page 2- Capturng full …

Shares