Selenium Headless Browser Testing
Headless browser refers to running tests in browser like simulation without having to invoke a browser or without a GUI. In this example set, we will be using Headless browser to run test cases.
We will be also discussing about the Headless browser in detail, about its importance as well as the caveat to keep in mind while using it.
1.Introduction
Headless browser can be achieved my importing HtmlUnitDriver
class in Selenium. Headless browser is used to perform load test, functional test as well as regression test as it is most light weight and fastest implementation of WebDriver API. These programs behave just like a browser but don’t show any GUI. Some of the headless browsers are NodeJS
, PhantonJS
, HtmlUnit
etc. PhantomJS
can also be implemented in Selenium by importing PhantomJS
jar.
Advantages of HtmlUnitDriver :
- Lightweight and quickest to implement.
- Ideal to perform different tests such as load test, functional test, sanity test as well as regression test in server without having to install browsers.
- To run test cases on simulated multiple browser versions.
- To access different content of the web pages quickly without having to load them
Disadvantages of HtmlUnitDriver :
- Although they support common browser features like (HTML parsing, cookies) however they do not render DOM elements of JavaScript. It uses Rhino JavaScript engine.
- Since Headless Browser uses Java Script engine, which has Java Script configured slightly different than the W3C standard. However, there is an option to run test cases with JavaScript or without the Java Script option.
Testing.java
package javacodegeeks.seleniumHeadlessBrowser; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.htmlunit.HtmlUnitDriver; public class TestingwJS { public static void main (String args[]){ HtmlUnitDriver driver = new HtmlUnitDriver(); driver.setJavascriptEnabled(true); driver.get("http://www.google.com"); System.out.println("Title of the page is" + driver.getTitle()); WebElement java = driver.findElement(By.name("q")); java.sendKeys("Java Code Geeks"); java.submit(); System.out.println("Title of the page now is " + driver.getTitle()); } }
2. Enabling JavaScript
Java Script can be enabled in two ways :
1. As a constructor by passing JavaScript enable flag to HtmlUnitDriver
class
HtmlUnitDriver driver = new HtmlUnitDriver(true);
2. By using setJavaScriptEnabled
method
HtmlUnitDriver driver = new HtmlUnitDriver(); Driver.setJavaScriptEnabled(true);
TestingwJS.java
package javacodegeeks.seleniumHeadlessBrowser; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.htmlunit.HtmlUnitDriver; public class TestingwJS { public static void main (String args[]){ HtmlUnitDriver driver = new HtmlUnitDriver(); driver.setJavascriptEnabled(true); driver.get("http://www.google.com"); System.out.println("Title of the page is" + driver.getTitle()); WebElement java = driver.findElement(By.name("q")); java.sendKeys("Java Code Geeks"); java.submit(); System.out.println("Title of the page now is " + driver.getTitle()); } }
Enabling different types of Browsers and versions.
Types of Browsers can be added by passing BrowserVersion
as constructor
HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_45);
TestingwBrowserversions.java
package javacodegeeks.seleniumHeadlessBrowser; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.htmlunit.HtmlUnitDriver; import com.gargoylesoftware.htmlunit.BrowserVersion; public class TestingwBrowserversions { public static void main(String[] args) { HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_45); driver.get("http://www.google.com"); System.out.println("Title of the page is" + driver.getTitle()); WebElement java = driver.findElement(By.name("q")); java.sendKeys("Java Code Geeks"); java.submit(); System.out.println("Title of the page now is " + driver.getTitle()); } }
3.PhantomJS
PhantomJS
is also a headless browser type which has JavaScript API features available. It provides an optimal solution for headless website testing as it comes with the standard DOM API.
In order to access PhantomJS with Selenium, we need to add PhantomJS in project directory like for chrome driver.
PhantonJs.java
package javacodegeeks.seleniumHeadlessBrowser; 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 PhantomJs { public static void main(String [] args){ File file = new File("/Users/saraddhungel/Downloads/phantomjs"); System.setProperty("phantomjs.binary.path", file.getAbsolutePath()); WebDriver driver = new PhantomJSDriver(); driver.get("http://www.google.com"); WebElement wbelement = driver.findElement(By.name("q")); wbelement.sendKeys("Java Code Geeks"); wbelement.submit(); System.out.println("Title of the page now is " + driver.getTitle()); driver.quit(); } }
4.Conclusion
We explore the concept of the headless browser in this tutorial. We also discussed about different headless browsers like HtmlUnitDrive , PhantomJs
. The advantages and disadvantages of using them.
5. Download the Eclipse Project
This was an example of Selenium Headless Browser Testing
You can download the source code of this example here: SeleniumHeadless Browser
Nice article about headless browser.
bro, jars are missmatched and it is not working now, are we need to install firefox in our laptop