2017-05-05 105 views
1

我正在編寫一個程序,使用selenide(4.4.x)和junit(4.12)來自動化訪問者註冊。如果我在IDE中將它們作爲JUnit測試運行,測試運行良好,但爲了更方便的使用,我想從main/commandline運行它。能夠從IntelliJ運行配置中運行JUnitCore測試運行器,但不能運行命令行

我設法讓這個與IntelliJ運行配置一起工作,但是當我從命令行嘗試相同的東西時卻沒有。基本上,我有一個抽象類來啓動和停止Selenium/ide WebDriver,一個包含實際保留邏輯的具體測試類(RegisterVisitorTest.java),以及RunTest.javamain方法。請參閱下面的MWE(剪下不相關的代碼,因此它可能無法執行)。

如果我創建具有的IntelliJ運行配置:

-Dselenide.browser=chrome -Dwebdriver.chrome.driver="C:\downloads\chromedriver.exe" -DlastName="Peeters" -DfirstDay="5-5-2017" 

爲VM選項,則它執行得很好,並打印都「在RunTest.main」,「RegisterVisitorTest」,將填充的形式對我來說。不過,如果我使用mvn install創建一個jar和運行它

java -Dselenide.browser=chrome -Dwebdriver.chrome.driver="C:\downloads\chromedriver.exe" -DlastName="Peeters" -DfirstDay="5-5-2017" -cp %junit_path%;target\name-of-jar.jar x.selenide.RunTest 

其中`%junit_path%包含引用的junit.jar和hamcrest.jar,它並進入主並打印「在RunTest.main」 ,但它並不實際運行測試。好消息是我也沒有得到任何錯誤。

然而,當我直接運行它作爲命令行一個JUnitCore亞軍搭配:

java -Dselenide.browser=chrome -Dwebdriver.chrome.driver="C:\downloads\chromedriver.exe" -DlastName="Peeters" -DfirstDay="5-5-2017" -cp %junit_path%;target\name-of-jar.jar org.junit.runner.JUnitCore nl.ing.selenide.RegisterVisitorTest 

我得到以下輸出:

JUnit version 4.12 
Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/support/events/WebDriverEventListener 
     at java.lang.Class.forName0(Native Method) 
     at java.lang.Class.forName(Unknown Source) 
     at org.junit.internal.Classes.getClass(Classes.java:16) 
     at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:100) 
     at org.junit.runner.JUnitCommandLineParseResult.parseArgs(JUnitCommandLineParseResult.java:50) 
     at org.junit.runner.JUnitCommandLineParseResult.parse(JUnitCommandLineParseResult.java:44) 
     at org.junit.runner.JUnitCore.runMain(JUnitCore.java:72) 
     at org.junit.runner.JUnitCore.main(JUnitCore.java:36) 
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.support.events.WebDriverEventListener 
     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) 
     ... 8 more 

讓我困擾的是,它運行正常,當我從IDE運行它,但是當我從命令行運行main時它不會觸發測試,並且在直接運行測試時失敗。在我的Maven項目中,我確實有一些紅線,但這似乎並不重要......

無論如何,這似乎歸結爲上述WebDriverEventListener,但如果我嘗試添加WebDriverEventListener它顯然可以找到依賴關係並自動添加正確的導入語句,但如果我做另一個mvn install,這不會改變結果。

我錯過了什麼嗎?

編輯:雖然在其他SO問題中找到'類未找到',但它不是上述hadoop問題的重複,因爲我有正確的環境變量集。

我能夠運行其他JAR,而不是這個。

通過使用Maven assembly插件解決以包含所有依賴關係。

MWE(未遂):

package x.selenide; 
//RunTest.java 
import org.junit.runner.JUnitCore; 

public class RunTest { 
    public static void main(String[] args) { 
     System.out.println("In RunTest.main"); 
     JUnitCore junit = new JUnitCore(); 
     junit.run(RegisterVisitorTest.class); 
    } 
} 

//RegisterVisitorTest.java 
public class RegisterVisitorTest extends ClickTest { 

    private static String lastName; 
    private static LocalDate firstDay; 
    private static LocalDate lastDay; 

    private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("d-M-yyyy"); 

    public RegisterVisitorTest() { 
     System.out.println("RegisterVisitorTest"); 
    } 

    @BeforeClass 
    public static void setUp() { 
     // setup properties with System.getProperties(); 
    } 

    @Test 
    public void openRegistrationPage(){ 
     Selenide.$(Selectors.byText("Bezoekers aanmelden")).click(); 
     String parentWindowHandle = WebDriverRunner.getWebDriver().getWindowHandle(); 

     // switch tab/window as it opens a new window 
     Set<String> handles = WebDriverRunner.getWebDriver().getWindowHandles(); 
     for (String handle: handles){ 
      if(!handle.equals(parentWindowHandle)){ 
       Selenide.switchTo().window(handle); 
      } 
     } 

     // method call to fill the actual registration form 
    } 
} 
// ClickTest.java 
public abstract class ClickTest { 
    @BeforeClass 
    public static void openOrderSite() { 
     Configuration.timeout = 10000; 
     Configuration.baseUrl = "https://intranet.net"; 
     Configuration.startMaximized = false; 
     Selenide.open("/subdomain"); 
     waitUntilPageIsLoaded(); 
    } 

    private static void waitUntilPageIsLoaded() { 
     waitUntilPageIsLoaded("Bezoekers aanmelden"); 
    } 

    static void waitUntilPageIsLoaded(String expected){ 
     logger.info(String.format("Waiting for string '%s' to appear...", expected)); 
     Selenide.$(Selectors.byText(expected)).waitUntil(Condition.appears, 20000); 
     logger.info("Page loaded"); 
    } 

    @AfterClass 
    public static void logout() { 
     WebDriverRunner.closeWebDriver(); 
    } 
} 
+0

可能的重複[hadoop在Windows中與cygwin noclassdefinition發現錯誤](http://stackoverflow.com/questions/23219089/hadoop-in-windows-with-cygwin-noclassdefinition-found-error) –

回答

1

這個例外是非常簡單的:在類路徑缺少的東西。 java無法找到org/openqa/selenium...類。

而你所有的設置都提到junit,hamcrest ......但不是硒。

長話短說:可能你的IDE在你沒有注意到的情況下在classpath中添加了一個硒罐。但當在命令行上運行的東西,你需要提供所有所需的元素。硒缺失。也可能是您的擁有類。

+1

謝謝!我自己的班級並沒有失蹤,因爲他們都在目標的罐子裏;)。讓我瘋狂的是,它始於缺少junit,然後是hamcrest,然後......我認爲Maven會照顧所有這些。我使用Maven assembly插件來解決它,創建一個具有依賴關係的jar,現在我可以運行主類。 –

相關問題