Search code examples
javamavenselenium-webdriverrest-assuredhtmlunit-driver

Htmlunitdriver conflicting with Rest Assured API


I am trying to use Htmlunitdriver but it looks like it is conflicting with Rest Assured API.

If i remove rest assured library from the pom.xml, the below code works fine HtmlUnitDriver driver = new HtmlUnitDriver();

If i add rest assured library to the pom.xml, an exception is thrown as below:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/cookie/CookieSpecProvider
at com.gargoylesoftware.htmlunit.WebClient.createWebConnection(WebClient.java:1907)
at com.gargoylesoftware.htmlunit.WebClient.<init>(WebClient.java:134)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.newWebClient(HtmlUnitDriver.java:303)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.createWebClient(HtmlUnitDriver.java:277)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(  HtmlUnitDriver.java:148)
at dfbhdfbhdfbh.htmlunittest.main(htmlunittest.java:15)
Caused by: java.lang.ClassNotFoundException: org.apache.http.cookie.CookieSpecProvider
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
... 6 more

Below is the list of dependencies

<dependencies>
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.8.1</version>
 </dependency>        
       <dependency>
    <groupId>com.jayway.restassured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>2.3.1</version>
 </dependency>              
 <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-server</artifactId>
    <version>2.48.2</version>
    <exclusions>
        <exclusion>
            <artifactId>commons-codec</artifactId>
            <groupId>commons-codec</groupId>
        </exclusion>
    </exclusions>
</dependency> 
<dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>selenium-java</artifactId>
   <version>2.48.2</version>
</dependency>  
</dependencies>

Not sure how to get around with this. I tried to use different Selenium version (2.24.1) and it resolved the exception. But it is giving me problems with the webpage i am trying to access. Also if i create a Java project and add the selenium and rest assured jars, i dont see any problem.

I believe something to do with Maven dependency.


Solution

  • I found a resolution for this - Adding below code to the pom file resolved the issue -

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.1</version>
            </dependency>
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.4</version>
            </dependency>
        </dependencies>
      </dependencyManagement>
    

    Basically Selenium HTML Unit required a higher version of the above dependencies so adding this code will serve the purpose.