HTMLUnitDriver Example To Run Selenium Webdriver Test Cases

HTMLUnitDriver is not like other web browser driver, it is light weight and headless. It will not display 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.

HTML Unit Driver Featrures

  1. Support HtmlUnit web browser. HtmlUnit is a pure java headless web browser.
  2. Support both Http and Https protocal.
  3. Support Cookies, JavaScript and Proxy server.
  4. Can interact with html response page such as submit form, click links and walk through the page dom tree.
  5. Can simulate both Get and Post web request method.
  6. Request header name and values can be customized.

Html Unit Driver Example

This example will use HtmUnitDriver to browse www.bing.com and type search keyword in the search box. Then parse out the search result data list in first search result page and print the result title and description in the Eclipse console.

It will use selenium-server-standalone-3.5.3.jar, because it support both HtmlUnitDriver and FirefoxDriver. It execute well with the latest Firefox browser such as Firefox Setup 55.0b9.

Example Steps

  1. Download selenium-server-standalone-3.5.3.jar.
  2. Add above jar file in java project build path.
    add selenium 3.5.3 jar in 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 geckodriver-v0.16.1-win64.zip also. You can read How to Correctly Use GeckoDriver To Run Webdriver Automation Test Script to learn more.

Example Code

public class TestHtmlUnitDriver {

	public static void main(String[] args) {
		TestHtmlUnitDriver example = new TestHtmlUnitDriver();
		example.testHtmlUnitDriver();
	}
	
	public void testHtmlUnitDriver()
	{
		WebDriver driver = null;
		
		try
		{
			// 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\\dev2qa.com\\Lib\\geckodriver-v0.16.1-win64\\geckodriver.exe");
			//Initiate a browser
			driver = new FirefoxDriver();
			*/
			driver.get("http://www.bing.com");
			
			// Get input search text box.
			By byIdSearchInput = By.id("sb_form_q");
			WebElement searchInput = driver.findElement(byIdSearchInput);
			if(searchInput!=null)
			{
				// Type search keyword in search text box.
				searchInput.sendKeys("selenium");
			}
			
			// Get search form submit button.
			By byIdSubmitBtn = By.id("sb_form_go");
			WebElement submitBtn = driver.findElement(byIdSubmitBtn);
			if(submitBtn!=null)
			{
				// Click submit button to search.
				submitBtn.click();
			}
			
			Thread.sleep(3000);
			
			// 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);
			
			if(resultElementList!=null)
			{
				// 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 current result item by xpath.
					By byXPathTitle = By.xpath(".//a");
					WebElement titleElement = resultElement.findElement(byXPathTitle);
					String title = titleElement.getText();
					System.out.println(title);
					System.out.println();
					
					// Get the result description from curent result item by xpath.
					By byXPathDesc = By.xpath(".//p");
					WebElement descElement = resultElement.findElement(byXPathDesc);
					String description = descElement.getText();
					System.out.println(description);
					System.out.println();
					System.out.println();
				}
			}
			
			
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}finally
		{
			if(driver!=null)
			{
				driver.close();
				driver=null;
			}
		}
	}

}

If you want to know why use the xpath in above example, you can read How To Select The Effective XPath For Web Element In Webdriver.

READ :   Selenium WebDriver - Handle Keyboard And Mouse Events

Console Output

html unit driver example console output

Source Code

  1. Download “TestHtmlUnitDriver.zip” TestHtmlUnitDriver.zip – Downloaded 171 times – 1 KB

Reference

  1. org.openqa.selenium.htmlunit.HtmlUnitDriver
(Visited 2,702 times, 1 visits today)

Leave a Reply

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.