What Is HtmlUnit Driver or Headless browser driver?
HtmlUnit Driver or by too large It Is known as Headless browser driver Is based on HtmlUnit. HtmlUnit Driver Is same every bit Firefox or Chrome or IE driver Instance but HtmlUnit driver create non accept GUI thus y'all tin non run across your exam execution on your screen. It Is something similar virtual browser Instance. Generally nosotros are using Firefox driver or chrome driver or IE driver Instance to run our
WebDriver automation tests. But from all these driver Instances, Headless driver Instance Is almost lightweight too fastest version to execute exam using selenium.Also y'all tin larn how to execute webdriver exam inward FIREFOX, CHROME too IE browsers.
Advantages of using HtmlUnit Driver
There are few advantages every bit bellow of using HtmlUnit driver.
- It Is non using whatsoever GUI to exam thus your tests volition run In background without whatsoever visual Interruption.
- Execution Is faster compared to all other driver Instances.
- It Is platform Independent every bit It Is pure Java solution. It Is using Rhino javascript engine.
- You tin every bit good choose other browser versions to run your tests through HtmlUnit driver.
WebDriver exam Example using HtmlUnit Driver
I accept created uncomplicated Google search example which volition impress page title, search on google too thus print all google search outcome strings In console.
When y'all run bellow test, You volition notice that whatsoever browser volition non open, exam volition endure executed virtually too y'all volition run across results conduct In your eclipse execution console.
package Testing_Pack; import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.htmlunit.HtmlUnitDriver; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; world flat htmlDriver { HtmlUnitDriver driver; String pagetitle; @BeforeTest world void setup() throws Exception { //Initializing HtmlUnitDriver. driver = novel HtmlUnitDriver(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //Opening URL In HtmlUnitDriver. driver.get("http://www.google.com"); } @AfterTest world void tearDown() throws Exception { //Closing HtmlUnitDriver. driver.quit(); } @Test world void googleSearch() { //Get too impress page championship earlier search. pagetitle = driver.getTitle(); System.out.println("Page championship earlier search : "+pagetitle); //Search alongside Hello World on google. WebElement Searchbox = driver.findElement(By.xpath("//input[@name='q']")); Searchbox.sendKeys("Hello World"); Searchbox.submit(); //Get too impress page championship afterwards search. pagetitle = driver.getTitle(); System.out.println("Page championship afterwards search : "+pagetitle); //Get listing of search outcome strings. List<WebElement> allSearchResults=driver.findElements(By.cssSelector("ol li h3>a")); //Iterate the inward a higher house listing to acquire all the search titles & links from that page. for(WebElement eachResult : allSearchResults) { System.out.println("Title : "+eachResult.getText()+", Link : "+eachResult.getAttribute("href")); } } }
When execution completed, You volition run across outcome In console every bit bellow. There volition endure or thus alert messages similar "com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error" In console. We volition run across how to take away those warnings In my adjacent post.