能够从IntelliJ运行配置中运行JUnitCore测试运行器,但不能运行命令行
我正在编写一个程序,使用selenide(4.4.x)和junit(4.12)来自动化访问者注册。如果我在IDE中将它们作为JUnit测试运行,测试运行良好,但为了更方便的使用,我想从main/commandline运行它。能够从IntelliJ运行配置中运行JUnitCore测试运行器,但不能运行命令行
我设法让这个与IntelliJ运行配置一起工作,但是当我从命令行尝试相同的东西时却没有。基本上,我有一个抽象类来启动和停止Selenium/ide WebDriver,一个包含实际保留逻辑的具体测试类(RegisterVisitorTest.java
),以及RunTest.java
和main
方法。请参阅下面的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();
}
}
这个例外是非常简单的:在类路径缺少的东西。 java无法找到org/openqa/selenium...
类。
而你所有的设置都提到junit,hamcrest ......但不是硒。
长话短说:可能你的IDE在你没有注意到的情况下在classpath中添加了一个硒罐。但当你在命令行上运行的东西,你需要提供所有所需的元素。硒缺失。也可能是您的拥有类。
谢谢!我自己的班级并没有失踪,因为他们都在目标的罐子里;)。让我疯狂的是,它始于缺少junit,然后是hamcrest,然后......我认为Maven会照顾所有这些。我使用Maven assembly插件来解决它,创建一个具有依赖关系的jar,现在我可以运行主类。 –
可能的重复[hadoop在Windows中与cygwin noclassdefinition发现错误](http://stackoverflow.com/questions/23219089/hadoop-in-windows-with-cygwin-noclassdefinition-found-error) –