2012-12-04 76 views
1

要開始使用Selenium2和HtmlUnitDriver,我創建了一個測試項目。只要我使用的是FirefoxDriver,一切都完美無缺。但是,只要我切換到HtmlUnitDriver,我會得到以下異常。我錯過了一些依賴關係嗎?我沒有找到任何文檔告訴我必須包含哪些Maven依賴項,才能使其正常工作。HtmlUnitDriver似乎缺少HTTP組件?

例外:

java.lang.NoClassDefFoundError: org/apache/http/impl/conn/PoolingClientConnectionManager 
    at com.gargoylesoftware.htmlunit.HttpWebConnection.createHttpClient(HttpWebConnection.java:536) 
    at com.gargoylesoftware.htmlunit.HttpWebConnection.getHttpClient(HttpWebConnection.java:500) 
    at com.gargoylesoftware.htmlunit.HttpWebConnection.setUseInsecureSSL(HttpWebConnection.java:711) 
    at com.gargoylesoftware.htmlunit.WebClient.setUseInsecureSSL(WebClient.java:1096) 
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.createWebClient(HtmlUnitDriver.java:263) 
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:129) 
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:172) 
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:168) 
    at GoogleTest.testMe(GoogleTest.java:20) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80) 
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714) 
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) 
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) 
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:128) 
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) 
    at org.testng.TestRunner.privateRun(TestRunner.java:767) 
    at org.testng.TestRunner.run(TestRunner.java:617) 
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) 
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) 
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) 
    at org.testng.SuiteRunner.run(SuiteRunner.java:240) 
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1203) 
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1128) 
    at org.testng.TestNG.run(TestNG.java:1036) 
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111) 
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) 
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175) 
Caused by: java.lang.ClassNotFoundException: org.apache.http.impl.conn.PoolingClientConnectionManager 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247) 
    ... 33 more 

項目設置,的pom.xml

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
<modelVersion>4.0.0</modelVersion> 

<groupId>de.test</groupId> 
<artifactId>ui-tests</artifactId> 
<version>1.0.0-SNAPSHOT</version> 

<properties> 
    <selenium.version>2.26.0</selenium.version> 
    <testNG.version>6.1.1</testNG.version> 
</properties> 

<dependencyManagement> 
    <dependencies> 
     <dependency> 
      <groupId>org.seleniumhq.selenium</groupId> 
      <artifactId>selenium-java</artifactId> 
      <version>${selenium.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.seleniumhq.selenium</groupId> 
      <artifactId>selenium-htmlunit-driver</artifactId> 
      <version>${selenium.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>org.testng</groupId> 
      <artifactId>testng</artifactId> 
      <version>${testNG.version}</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 
</dependencyManagement> 

<dependencies> 
    <dependency> 
     <groupId>org.testng</groupId> 
     <artifactId>testng</artifactId> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.seleniumhq.selenium</groupId> 
     <artifactId>selenium-java</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.seleniumhq.selenium</groupId> 
     <artifactId>selenium-firefox-driver</artifactId> 
     <version>${selenium.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.seleniumhq.selenium</groupId> 
     <artifactId>selenium-htmlunit-driver</artifactId> 
     <version>${selenium.version}</version> 
    </dependency> 
</dependencies> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <configuration> 
       <source>1.5</source> 
       <target>1.5</target> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>selenium-maven-plugin</artifactId> 
      <executions> 
       <execution> 
        <phase>pre-integration-test</phase> 
        <goals> 
         <goal>start-server</goal> 
        </goals> 
        <configuration> 
         <background>true</background> 
        </configuration> 
       </execution> 
       <execution> 
        <id>stop-selenium</id> 
        <phase>post-integration-test</phase> 
        <goals> 
         <goal>stop-server</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 

     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <configuration> 
       <!-- Skip the normal tests, we'll run them in the integration-test phase --> 
       <skip>true</skip> 
      </configuration> 

      <executions> 
       <execution> 
        <phase>integration-test</phase> 
        <goals> 
         <goal>test</goal> 
        </goals> 
        <configuration> 
         <skip>false</skip> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

一個簡單的測試類:

import java.util.concurrent.TimeUnit; 

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

public class GoogleTest { 

    private static final String SEARCH_BUTTON = "gbqfb"; 
    public final static String SEARCH_BAR = "gbqfq"; 
    public final static String RESULT_STATS = "resultStats"; 

    @Test 
    public void testMe() { 
     // does not work: 
     // java.lang.NoClassDefFoundError: 
     // org/apache/http/impl/conn/PoolingClientConnectionManager 
     HtmlUnitDriver wd = new HtmlUnitDriver(); 

//  FirefoxDriver wd = new FirefoxDriver(); 

     wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); 
     wd.get("http://www.google.de/"); 

     WebElement searchBar = wd.findElement(By.id(SEARCH_BAR)); 
     WebElement searchButton = wd.findElement(By.id(SEARCH_BUTTON)); 

     searchBar.click(); 
     searchBar.clear(); 
     searchBar.sendKeys("Selenium Test"); 
     searchButton.click(); 

     WebElement resultsText = wd.findElement(By.id(RESULT_STATS)); 

     String resultsFound = resultsText.getText(); 
     wd.close(); 

     Assert.assertTrue(resultsFound.startsWith("Ungefähr 34.100.000 Ergebnisse")); 
    } 
} 

編輯:修復缺少的依賴關係!

回答

3

嘗試移動塊:

<dependency> 
    <groupId>org.seleniumhq.selenium</groupId> 
    <artifactId>selenium-htmlunit-driver</artifactId> 
    <version>${selenium.version}</version> 
</dependency> 

<dependencyManagement>部進入POM的<dependencies>部分(這是你必須包括在火狐驅動程序)。

+0

謝謝我添加了缺少的依賴項,但仍然是相同的錯誤。 –

+0

我只是試圖通過修復依賴關係defenition來實現它,但仍然是同樣的錯誤。 –

+1

似乎父項目存在相互衝突的問題。在隔離來自其他maven項目的ui-tests後,現在一切正常! –

3

我得到了同樣的錯誤,這裏是我如何解決 - 如果您的httpclient.jar,httpcore,httpmime jar文件版本不匹配父級第三方類,則會出現此錯誤。我有httpclient-4.0.3.jar版本。已下載並將httpclient-4.2.jar添加到我的NetBeans的lib中。同樣增加了httpcore-4.2.jar和httpmime-4.2.jar。該類執行! 從以下存儲庫下載: http://mvnrepository.com/artifact/org.apache.httpcomponents

+0

我正在使用maven,所以沒有手工下載的文物! –

+0

謝謝。我在maven pom.xml文件中寫了錯誤的版本號。看到您的答案並修復版本號後,問題就解決了。 –