Value of Headless Test Automation
Kushan Shalindra Amarasiri
Director Quality Engineering at Social Catfish
Automated testing is one of the important key elements in QA. Every test department in a software delivery organization now engage in test automation. It is also one of the mandatory skills that a quality engineer should have. There are diverse set of test automation tools available in the industry, Selenium being number 1 out of the rest. Selenium's growth is due to its free and open source nature. Everyone is learning and practicing automation via Selenium due to the support that it gives in terms of multiple platform and multiple browser support.
Most of us execute Selenium via a browser such as Fire Fox, Chrome, IE or Edge. But we tend to use its potential of executing scripts via Headless mode.
Headless mode of script execution is where we are not executing the test scripts visually by opening up any browser session. The scripts are executed by opening up a non visual browser session. It simply reads the HTML DOM (Document Object Model). Verifications are done in the similar manner as with browser based automation.
"In headless test automation, execution is much faster"
One of the objectives of test automate is "Speed". We should always strive to maximize the speed of execution. In headless mode, since there is no visual browser opened and the time taken execute the script has been greatly minimized. Most of the time, web browser takes time to load a page visually and sometimes even takes time to transition between web pages. All these are minimized, and virtually eliminated via headless browser execution.
"Headless browser test automation is great for machines which does not have browsers"
Some of the Linux machines are installed without web browsers and sometimes they have no GUI module installed. Execution of test automation scripts via headless mode is the ultimate solution for these machines.
"Headless mode of script execution helps to minimize machine resource consumption"
When we execute script scripts on host machines, there is a less need for machine resources such as Memory, CPU and even Disk space. We can have low-end execution nodes which saves testing budget of the organization.
" Headless test execution is helps to have multiple browser session in a single test execution node"
As we looked above, headless execution allows saving system resources. This allows us to run more browser session compared to the execution of multiple visual browser sessions. If we need to execute a performance test via JMeter and Selenium grid this would be the ultimate way.
"Setting up test data or doing data driven test automation, headless test automation is the ultimate"
When we need to automate the process of test data setup and carryout data driven test automation, headless automation would be the best choice. We may need to create 1000's of 10000's of test data to carryout a performance test on an environment. The main objective here is to carryout test data creation at a faster rate. So I would suggest automating these scripts in headless mode.
"Would be awesome to render PDF files and also screen shots"
All the headless automation tools have in built libraries to render PDF files and also capture the screen shot of the excution page when there are execution issues.
So we have a great set of advantages of using headless test automation, what are the limitations ?
"Headless mode of test automation is not a great candidate to simulate real user accessing the web site"
If we want to visually see certain functionality is working on a page, such as playing a movie or an animation, headless automation will not be an ideal candidate.
"Its not a candidate to capture client side performance with reference to a particular browser"
If we want to capture client side performance testing with reference to a visual browser, headless automation is not a good candidate.
Headless test automation tools and implementations
- HTML Unit Driver - This is the headless browser mode provided by Selenium test tool.
package htmldriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class htmlUnitYest {
public static void main(String[] args) {
// Creating a new instance of the HTML unit driver
WebDriver driver = new HtmlUnitDriver();
// Navigate to Google
driver.get("https://www.google.com");
// Locate the searchbox using its name
WebElement element = driver.findElement(By.name("q"));
// Enter a search query
element.sendKeys("Linkdin");
// Submit the query. Webdriver searches for the form using the text input element automatically
// No need to locate/find the submit button
element.submit();
// This code will print the page title
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
}
When we use HTML Unit Driver, Selenium executes the script in headless mode. The main limitation that is evident in HTML Unit Driver is that it supports less for Java Script and also web sites that uses AJAX.
- Phantom JS Driver - This is the latest headless test automation driver that is being widely used.
var page = require('webpage').create();
page.open('https://example.com', function(status) {
console.log("Status: " + status);
if(status === "success") {
page.render('example.png');
}
phantom.exit();
});
Similarly we can use Phantom JS with Selenium.
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
public class phantom {
public static void main(String[] args) {
File file = new File("C:/Program Files/phantomjs-2.0.0-windows/bin/phantomjs.exe");
System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
WebDriver driver = new PhantomJSDriver();
driver.get("https://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("Linkdin");
element.submit();
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
}
As Phantom JS is still new and progressing. There are issues existing in the tool, which will be reduced in the future.
- Waitr headless test execution - Waitr is also a popular test automation tool which provides headless execution. Waitr was recently taken over by Selenium. Lets also look how to implement headless execution via Waitr.
require 'watir'
require 'headless'
headless = Headless.new
headless.start
b = Watir::Browser.start 'www.google.com'
puts b.title
b.close
headless.destroy
These are some of the ways we can do headless browser execution. We will look at how each implementation can be done in future articles to come. We shall conclude this article with now better understanding on headless execution and with the set of tools available.
Assistant Manager - QA Automation at Matific | Author of the Thunder Cloud Automation Framework
7 年Thanks for the useful articles. If any issues when doing the headless automation for Ajax loading web pages. How can we wait the loading time in headless mode?
Quality Assurance Manager (Automation) at SHL
7 年Chrome Headless is also a good option and easy to use than other headless drivers.
Software Test Engineer at Qualtrics
7 年Very useful article. Thanks for providing basic code snippets to back up your content.