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 www.bing.com 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 geckodriver-v0.16.1-win64.zip also.
  5. You can read How to Correctly Use GeckoDriver To Run Webdriver Automation Test Script to learn more.

2.2 Example Code.

  1. TestHtmlUnitDriver.java.
    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 the 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 the current 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;
    			}
    		}
    	}
    
    }
  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.

Reference

  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.