Headless Browser Testing Using HtmlUnitDriver in Selenium WebDriver
Headless Browser Testing Using HtmlUnitDriver In Selenium WebDriver
In the earlier post, we have given a brief explanation on what is headless browser and in this post, we learn how to perform headless browser testing using HtmlUnitDriver in Selenium WebDriver. HtmlUnitDriver is one of the drivers of Selenium WebDriver. HtmlUnitDriver and PhatomJsDriver are most popular headless browsers.
You could download HtmlUnitDriver Jar file from here. – Selenium HtmlUnit Driver
Html Unit Driver is currently the fastest and most lightweight implementation of WebDriver. As the name suggests, this is based on HtmlUnit. HtmlUnit is a java based implementation of a web browser without a GUI. HtmlUnit Driver is a well known Headless Browser driver. HtmlUnit Driver is similar to the other drivers such as Mozilla Firefox, Google Chrome, Internet Explorer but you couldn’t see the GUI of Html UnitDriver.
You can create a HtmlUnitWebDriver as shown below
WebDriver driver = new HtmlUnitDriver()
Advantages of HtmlUnitDriver are:
- Fastest implementation of WebDriver compared to other browsers
- A pure Java solution and so it is platform independent.
- Supports JavaScript
- It allows you to choose other browser versions to run your scripts.
Usually, to run Selenium Scripts using Firefox, we initialize the Firefox driver.
WebDriver driver = new FirefoxDriver();
Whereas while running Selenium Scripts using HtmlUnitDriver you need to write the below statement
WebDriver driver = new HtmlUnitDriver();
Remaining statements in your script will be as usual.
Let’s see a working example on HtmlUnitDriver in Selenium WebDriver.
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.htmlunit.HtmlUnitDriver; import org.testng.annotations.Test; public class HeadlessBrowser { @Test public void htmlUnitDriver() throws Exception{ // To declare and initialize HtmlUnitDriver WebDriver driver = new HtmlUnitDriver(); //WebDriver driver = new HtmlUnitDriver(BrowserVersion.) // Set implicit wait driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Open "Google.com and search SoftwareTestingMaterial.com" driver.get("https://www.google.com"); WebElement element = driver.findElement(By.name("q")); element.sendKeys("softwaretestingmaterial.com"); element.submit(); //Click on Software Testing Material link driver.findElement(By.linkText("Software Testing Material")).click(); // Get the title of the site and store it in the variable Title String Title = driver.getTitle(); // Print the title System.out.println("I am at " +Title); } }
Output:
I am at Software Testing Material - A Site for Software Testers PASSED: htmlUnitDriver =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 1, Failures: 0, Skips: 0 ===============================================
JavaScript In HtmlUnit Driver:
HtmlUnitDriver uses Rhino JavaScript engine. Other browsers are using separate JavaScript engine. So the test results may differ when compared to other browsers when you test JavaScript applications using HtmlUnit. By default, JavaScript is disabled in HtmlUnitDriver. Don’t worry, there is a way to enable it. J
It’s very easy to enable JavaScript support. We could enable JavaScript in HtmlUnitDriver in two ways
1. Enabling JavaScript while initializing the HtmlUnitDriver
HtmlUnitDriver driver = new HtmlUnitDriver(true);
2. Setting ‘setJavascriptEnabled’ to true
HtmlUnitDriver driver = new HtmlUnitDriver(); setJavascriptEnabled(true);
None of the popular browsers use the JavaScript engine used by HtmlUnit (Rhino). If you test JavaScript using HtmlUnit the results may differ significantly from those browsers.
Different Browser Versions using HtmlUnitDriver:
We could test our scripts on different browser versions using HtmlUnitDriver. Yes, it allows us to choose our preferred browser version. See the below screenshot.
WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3);
I am concluding this post about headless browser testing using HtmlUnitDriver. In the next post, we will learn how to perform headless browser testing using PhanthomJSDriver in Selenium WebDriver.
As always, feel free to share this post with your friends and colleagues on Facebook, Twitter, and Google Plus!
It’s better you also specify full flag dependency like which Selenium version needs to be used.
Hi Nimesh, thanks. Will try to do for the new posts.
Here are the Version of different libraries to run htmlunitDriver-
org.seleniumhq.selenium
selenium-java
3.8.0
org.seleniumhq.selenium
selenium-server
3.8.0
org.seleniumhq.selenium
htmlunit-driver
2.27
net.sourceforge.htmlunit
htmlunit
2.27
org.eclipse.jetty
jetty-maven-plugin
9.4.8.v20171121