Headless Browser-Selenium,HTMLUnitDriver
HtmlUnit
is a “GUI-Less browser for Java programs”. It has fairly good JavaScript support (which is constantly improving) and is able to work even with quite complex AJAX libraries, simulating Chrome, Firefox or Internet Explorer depending on the configuration used.
It is typically used for testing purposes or to retrieve information from web sites. HtmlUnit is not a generic unit testing framework. It is specifically a way to simulate a browser for testing purposes and is intended to be used within another testing framework such as JUnit or TestNG.
Refer to the document “Getting Started with HtmlUnit” for an introduction.
Selenium WebDriver Example:
1. Add the “htmlunit-driver-xxx-jar” from maven repository using the Download Link.
http://central.maven.org/maven2/net/sourceforge/htmlunit/htmlunit/2.13/htmlunit-2.13.jar
2. Add the downloaded jar to the build path of the project.
3. Execute the below program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import org.openqa.selenium.By; import org.openqa.selenium.htmlunit.HtmlUnitDriver; import com.gargoylesoftware.htmlunit.BrowserVersion; public class HeadlessBrowser { public static void main(String[] args) { HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME); driver.get("https://www.indeed.co.in"); String title = driver.getTitle(); System.out.println("Title:"+ title); //Identify the element using id "what" and clear it driver.findElement(By.id("what")).clear(); driver.findElement(By.id("what")).sendKeys("Selenium"); //Identify the Element using id "where" and clear it driver.findElement(By.id("where")).clear(); driver.findElement(By.id("where")).sendKeys("Bangalore"); driver.findElement(By.id("fj")).submit(); title = driver.getTitle(); System.out.println("Title:"+ title); } } |
Console Ouput:
Title:Job Search India | Indeed
Title:Selenium Jobs in Bengaluru, Karnataka – June 2018 | Indeed.co.in
Pingback: Headless Browser in Selenium using PhantomJS-Pros and Cons - Total-QA