2012-09-02 23 views
2

我是Selenium WebDriver的新手,使用EclipseIDE和TestNG。目前,我正在通過TestNG的運行在Eclipse中此示例代碼:

import org.openqa.selenium.By; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.testng.annotations.AfterTest; 
import org.testng.annotations.BeforeTest; 
import org.testng.annotations.Test; 

import java.util.List; 

public class CheesecakeFactory { 

    FirefoxDriver driver; 

    @BeforeTest 
    public void startDriver() { 
     driver = new FirefoxDriver(); 
    } 

    @AfterTest 
    public void stopDriver() { 
     driver.close(); 
    } 

    @Test 
    public void listCheesecakes() { 
     driver.get("http://www.thecheesecakefactory.com/"); 
     driver.findElement(By.linkText("Menu")).click(); 
     driver.findElement(By.linkText("Cheesecake")).click(); 
     List<WebElement> cheesecakes = driver.findElements(By.xpath("id('leftNav_levelTwo')//li")); 

     System.out.println(cheesecakes.size() + " cheesecakes:"); 
     for (int i=0; i<cheesecakes.size(); i++) { 
      System.out.println(i+1 + ". " + cheesecakes.get(i).getText()); 
     } 
    } 

} 

但是Eclipse返回此:

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

FAILED CONFIGURATION: @BeforeTest startDriver 
java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableList$Builder 
    at org.openqa.selenium.os.WindowsUtils.getPathsInProgramFiles(WindowsUtils.java:275) 
    at org.openqa.selenium.firefox.internal.Executable.locateFirefoxBinaryFromPlatform(Executable.java:148) 
    at org.openqa.selenium.firefox.internal.Executable.<clinit>(Executable.java:25) 
    at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:60) 
    at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:56) 
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:78) 
    at CheesecakeFactory.startDriver(CheesecakeFactory.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:80) 
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564) 
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213) 
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138) 
    at org.testng.TestRunner.beforeRun(TestRunner.java:641) 
    at org.testng.TestRunner.run(TestRunner.java:609) 
    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:1197) 
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1122) 
    at org.testng.TestNG.run(TestNG.java:1030) 
    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) 

我不明白爲什麼我得到這個錯誤。我已經做了以下內容:

  1. 新增的番石榴12.0.jar文件(在硒2.25.0 webdriver的其他jar文件一起)作爲在Eclipse外部jar文件。 (這個jar文件包含ImmutableList $生成器類)

  2. 加在CLASSPATH這個jar文件的路徑(環境變量>系統變量)

我缺少的東西?任何幫助是極大的讚賞。

回答

2

我想你使用的是selenium-java-2.25.0.jar。您應該使用selenium-server-standalone-2.25.0.jar,它將處理所有依賴關係(即所需的jar文件)。

另外你不需要明確定義環境變量,如果在Eclipse中添加jar文件,除非你在eclipse外運行測試。

希望這有助於... :)

+0

嗨Prashant。謝謝您的回答。是的,我正在使用Selenium-java-2.25.0.jar。如果我使用selenium-server-standalone-2.25.0.jar,是否需要從命令提示符啓動該服務器,或者我可以像使用Eclipse中的常規API那樣使用它? – Ryan

+0

作爲一個正常的api。很像你添加java-2.25.0.jar的方式。如果您不使用遠程驅動程序,則不需要啓動服務器。 –

+0

嗨Prashant,我終於做到了。非常感謝! :-) – Ryan