HTMLUnitDriver Example To Run Selenium Webdriver Test Cases

HTMLUnitDriver is not like other web browser drivers, it is lightweight and headless. It will not display a graphic user interface at run time, this can save a lot of system resources and make your test application run faster. But it is a full feature browser driver.

1. HTML Unit Driver Featrures.

  1. Support HtmlUnit web browser. HtmlUnit is a pure java headless web browser.
  2. Support both HTTP and HTTPS protocol.
  3. Support Cookies, JavaScript, and Proxy server.
  4. Can interact with Html response page such as submit the form, click links and walk through the page dom tree.
  5. Can simulate both the GET and POST web request methods.
  6. Request header names and values can be customized.

2. Html Unit Driver Example.

  1. This example will use HtmUnitDriver to browse and type search keywords in the search box.
  2. Then parse out the search result data list on the first search result page and print the result title and description in the Eclipse console.
  3. It will use selenium-server-standalone-3.5.3.jar because it supports both HtmlUnitDriver and FirefoxDriver. It executes well with the latest Firefox browser such as Firefox Setup 55.0b9.

2.1 Example Steps.

  1. Download selenium-server-standalone-3.5.3.jar.
  2. Add the above jar file in the java project build path.
  3. Download Firefox Setup 55.0b9 and install.
  4. Because WebDriver 3 will use GeckoDriver to initiate Firefox Driver, so you need to download also.
  5. You can read How to Correctly Use GeckoDriver To Run Webdriver Automation Test Script to learn more.

2.2 Example Code.

    public class TestHtmlUnitDriver {
    	public static void main(String[] args) {
    		TestHtmlUnitDriver example = new TestHtmlUnitDriver();
    	public void testHtmlUnitDriver()
    		WebDriver driver = null;
    			// Initiate HtmlUnitDriver object.
    			driver = new HtmlUnitDriver();
    			/* If you want to see the browser interaction, you can uncomment below code to use Firefox to run this test script.
    			//assign webdriver executable file path to the value of system variable
    			System.setProperty("webdriver.gecko.driver", "C:\\Workspace\\\\Lib\\geckodriver-v0.16.1-win64\\geckodriver.exe");
    			//Initiate a browser
    			driver = new FirefoxDriver();
    			// Get input search text box.
    			By byIdSearchInput ="sb_form_q");
    			WebElement searchInput = driver.findElement(byIdSearchInput);
    				// Type search keyword in search text box.
    			// Get search form submit button.
    			By byIdSubmitBtn ="sb_form_go");
    			WebElement submitBtn = driver.findElement(byIdSubmitBtn);
    				// Click submit button to search.;
    			// Get search result list in first result page by XPath.
    			By byXPathResultList = By.xpath("//*[@id=\"b_results\"]/li[@class=\"b_algo\"]");
    			List resultElementList = driver.findElements(byXPathResultList);
    				// Loop the search result list.
    				int size = resultElementList.size();
    				for(int i=0;i<size;i++)
    					WebElement resultElement = resultElementList.get(i);
    					// Get the result title from the current result item by XPath.
    					By byXPathTitle = By.xpath(".//a");
    					WebElement titleElement = resultElement.findElement(byXPathTitle);
    					String title = titleElement.getText();
    					// Get the result description from the current result item by XPath.
    					By byXPathDesc = By.xpath(".//p");
    					WebElement descElement = resultElement.findElement(byXPathDesc);
    					String description = descElement.getText();
    		}catch(Exception ex)
  2. If you want to know why use the XPath in the above example, you can read How To Select The Effective XPath For Web Element In Webdriver.


  1. org.openqa.selenium.htmlunit.HtmlUnitDriver

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.