我正在尝试使用Htmlunitdriver,但它似乎与Rest Assured API冲突。
如果我从pom.xml中删除了放心库,那么下面的代码可以正常工作HtmlUnitDriver driver=新HtmlUnitDrive();
如果我将放心库添加到pom.xml中,则会抛出如下异常:
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
以下是依赖项列表
<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>
不知道该怎么处理。我尝试使用不同的Selenium版本(2.24.1),它解决了异常。但这让我在尝试访问的网页上遇到了问题。此外,如果我创建一个Java项目并添加selenium和放心的jar,我看不到任何问题。
我认为这与Maven依赖性有关。
我找到了一个解决方案-在pom文件中添加以下代码解决了问题-
<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>
基本上,SeleniumHTMLUnit需要更高版本的上述依赖项,因此添加此代码将达到目的。