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.
- Support HtmlUnit web browser. HtmlUnit is a pure java headless web browser.
- Support both HTTP and HTTPS protocol.
- Support Cookies, JavaScript, and Proxy server.
- Can interact with Html response page such as submit the form, click links and walk through the page dom tree.
- Can simulate both the GET and POST web request methods.
- Request header names and values can be customized.
2. Html Unit Driver Example.
- This example will use HtmUnitDriver to browse www.bing.com and type search keywords in the search box.
- 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.
- 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.
- Download selenium-server-standalone-3.5.3.jar.
- Add the above jar file in the java project build path.
- Download Firefox Setup 55.0b9 and install.
- 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.
2.2 Example Code.
- 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; } } } }
- 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