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.

Now lets look at how we can automate a simple google search scenario with Selenium and Firefox headless

package GekoHeadlessPkg;package GekoHeadlessPkg;
import static org.testng.Assert.assertEquals;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
public class GekoHeadlessClass {
 public static void main(String[] args) 
  { 
    System.setProperty("webdriver.gecko.driver", "C:\\gekhodriver\\geckodriver.exe"); 
    FirefoxOptions options = new FirefoxOptions(); 
    options.setHeadless(true); 
    WebDriver driver  = new FirefoxDriver(options); 
    driver.get("http://www.google.lk"); 
    driver.findElement(By.name("q")).sendKeys("Selenium"); 
    driver.findElement(By.name("btnK")).click(); 
    driver.findElement(By.linkText("Selenium - Web Browser Automation")).click(); 
    String strTitle = driver.getTitle(); 
    assertEquals(strTitle, "Selenium - Web Browser Automation"); 
    System.out.println("Test Completed"); 
   }
}

Lets look at how we can now execute a Selenium script with Chrome headless

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.*;
import static org.testng.Assert.assertEquals;
import java.lang.String;
public class testclass { 
   public static void main (String args[]){ 
     System.setProperty("webdriver.chrome.driver", "C:\\chromedrv\\chromedriver.exe");  
     ChromeOptions ch = new ChromeOptions(); 
     ch.addArguments("headless"); 
     ch.addArguments("window-size=1200x600"); 
     WebDriver driver = new ChromeDriver(ch); 
     driver.get("http://www.google.lk"); 
     driver.findElement(By.name("q")).sendKeys("Selenium"); 
     driver.findElement(By.name("btnK")).click(); 
     driver.findElement(By.linkText("Selenium - Web Browser Automation")).click(); 
     String strTitle = driver.getTitle(); 
     assertEquals(strTitle, "Selenium - Web Browser Automation"); 
    System.out.print("test done");}
  }
}

Leave a Reply