Search code examples
javaseleniumtestinghtmlunit-driver

Getting error "java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/html/HtmlElement"


I am using selenium webdriver for the testing I have written code:

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.MarionetteDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
//import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
//import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class sample_google
{

	WebDriver driver;


    @BeforeTest

    public  void setUp(){

       // FirefoxProfile profile = new FirefoxProfile();
    	//System.setProperty("webdriver.gecko.driver", "D:\\ashwini\\geckodriver.exe");
    	
//		  driver= new MarionetteDriver();
    	 driver = new HtmlUnitDriver();

	    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }


    @Test

    public void testLoadingFirstPage() throws Exception{

        driver.get("https://www.google.com/");
        WebElement searchBox = driver.findElement(By.name("q"));
        searchBox.sendKeys("fast cars");
        searchBox.submit();
        System.out.println("Page title : " + driver.getTitle());
        WebElement resultCount = driver.findElement(By.id("resultStats"));
        System.out.println("Result Count : " + resultCount.getText());
       

    }
    @AfterTest
	  public void teardown()
	  {
		  //driver.quit();
	  }

    
}
However I am getting output

FAILED CONFIGURATION: @BeforeTest setUp
java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/html/HtmlElement
	at sample_google.setUp(sample_google.java:31)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
	at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:514)
	at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:215)
	at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142)
	at org.testng.TestRunner.beforeRun(TestRunner.java:656)
	at org.testng.TestRunner.run(TestRunner.java:624)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
	at org.testng.SuiteRunner.run(SuiteRunner.java:268)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
	at org.testng.TestNG.run(TestNG.java:1064)
	at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
	at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152)
	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57)
Caused by: java.lang.ClassNotFoundException: com.gargoylesoftware.htmlunit.html.HtmlElement
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	... 23 more

SKIPPED CONFIGURATION: @AfterTest teardown
SKIPPED: testLoadingFirstPage

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 1
    Configuration Failures: 1, Skips: 1
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 1
===============================================

[TestNG] Time taken by org.testng.reporters.jq.Main@2752f6e2: 142 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 28 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@6e8dacdf: 14 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@2d209079: 48 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@43a25848: 15 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@b684286: 9 ms

I have included the jar of selenium-htmlunit-driver-2.46.0.jar in the code. Still getting the error. So please check what is the issue.

Thanks in advance

I have included the jar of selenium-htmlunit-driver-2.46.0.jar separately in the libraries. still getting the error


Solution

  • Add below dependency to your pom.xml file and it should work fine

    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>htmlunit-driver</artifactId>
    <version>2.20</version>
    </dependency>
    

    Since 2.53 this is no longer shipped with webdriver