I created a java class called htmlUnitTest to do a test with Selenium Headless Testing. The class basically goes to the google site and writes a text in the search box of the site, does the search and takes the page title and displays the title in the Eclipse console. When I run the test, Eclipse displays the following error message:
Exception in thread "main" java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/WebWindowListener at htmldriver.htmlUnitTest.main(htmlUnitTest.java:12) Caused by: java.lang.ClassNotFoundException: com.gargoylesoftware.htmlunit.WebWindowListener at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 more
The jar file I used to import the HtmlUnitDriver class for my project was: selenium-htmlunit-driver-2.52.0.jar.
Below is the Java code.
public class htmlUnitTest {
public static void main(String[] args) {
WebDriver driver = new HtmlUnitDriver();
driver.get("https://www.google.com.br/");
WebElement ele = driver.findElement(By.name("q"));
ele.sendKeys("Teste");
ele.submit();
System.out.println("Título da página: " +driver.getTitle());
driver.quit();
}}
The HtmlUnitDriver is only a small wrapper around HtmlUnit itself. So you need all the dependencies of the selenium-htmlunit-driver also (in fact HtmlUnit with all the dependencies).
You can download the whole package here https://github.com/SeleniumHQ/htmlunit-driver/releases