0

I'm trying to run a very simple code that retrieves the title of a page and uses assertion but in a Headless Browser. I'm using HTML Unit Driver. I will show my code, the libraries included and the result. I know the test case should be a pass but it's failing. Any help?

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.Assert;
import org.testng.annotations.Test;


public class Verify {

    @Test
    public  void verifyGoogle() {
        // TODO Auto-generated method stub


        WebDriver driver = new HtmlUnitDriver();

        driver.get("http://google.com");

        String title = driver.getTitle();

        System.out.println(title);

        Assert.assertTrue(title.contains("Google")); 

    }

}

This is the result i'm getting:

[TestNG] Running:
  C:\Users\ntouma\AppData\Local\Temp\testng-eclipse-1794337486\testng-customsuite.xml

FAILED: verifyGoogle
java.lang.NoSuchFieldError: INSTANCE
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:144)
    at com.gargoylesoftware.htmlunit.HttpWebConnection.configureHttpsScheme(HttpWebConnection.java:601)
    at com.gargoylesoftware.htmlunit.HttpWebConnection.createHttpClient(HttpWebConnection.java:536)
    at com.gargoylesoftware.htmlunit.HttpWebConnection.getHttpClientBuilder(HttpWebConnection.java:498)
    at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:156)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1323)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1240)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:347)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:416)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:519)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:508)
    at Verify.verifyGoogle(Verify.java:16)
    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:85)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
    at org.testng.TestRunner.privateRun(TestRunner.java:774)
    at org.testng.TestRunner.run(TestRunner.java:624)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
    at org.testng.SuiteRunner.run(SuiteRunner.java:261)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
    at org.testng.TestNG.run(TestNG.java:1048)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)


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


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

[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@19bb089b: 42 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@35bbe5e8: 9 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@6bdf28bb: 20 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@1fbc7afb: 70 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@129a8472: 8 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 8 ms

[And these are the Libraries referenced.][1]

project setup

Soren
  • 14,402
  • 4
  • 41
  • 67
  • I also changed the Selenium version to V 2.50 instead of V 2.53 but nothing changed... – Nick Thomas May 11 '16 at 19:37
  • @mukeshOtwani this is my question! Thanks – Nick Thomas May 11 '16 at 19:38
  • it's something with your dependencies in project setup. For example, since you are using testng, and not junit, shouldn't you have testng dependency? But could be something else in dependencies. People who use testng and htmlunit should figure it out easily – timbre timbre May 11 '16 at 22:22
  • Thanks for your reply. It's my first crack at this so hoping for the best. – Nick Thomas May 12 '16 at 15:31

2 Answers2

0

Try this, HtmlUnitDriver driver = new HtmlUnitDriver();

  • 1
    Welcome to StackOverflow! Although this might answer the question, consider adding reasons to why/how this will work with suitable explanation and links! – Rick M. May 18 '18 at 07:01
0

First of all remove all selenium dependencies form .m2 folder : C:\Users\12345\.m2\repository\org\seleniumhq\selenium

then use dependency :

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

I am having TestNg dependency :

<dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.11</version>
        </dependency>

This should work and there is no fault in your code

dangi13
  • 1,275
  • 1
  • 8
  • 11