NoClassDefFoundError:org / openqa / selenium / NoAlertPresentException: 我正在执行一段代码,给出了此错误。这是使用硒在Junit上的Eclipse中编写的。通过eclipse可以正常工作,但我需要在共享存储库上与团队共享。它必须通过命令行工作,但是由于该错误而失败。 Z:\ TripPlanner \ lib> java -cp。; junit-4.12.jar; hamcrest-core-1.3.jar; Z:\ TripPlanner \ lib * org.junit.runner.JUnitCore“ com.example.tests.TripPlannerJUnit” JUnit版本4.12 线程“主”中的异常java.lang.NoClassDefFoundError:java.base / java.lang.Class.forName0上的org / openqa / selenium / NoAlertPresentException(本机方法) 在java.base / java.lang.Class.forName(Class.java:398) 在org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:100)在org.junit.runner.JUnitCommandLineParseResult.parseArgs(JUnitCommandLineParseResult.parseArgs(JUnitCommandLineParseResult.parseArgs(JUnitCommandLineParseResult.java:50))在org.junit.runner.JUnitCore.runMain(JUnitCore.java:72)上的org.junit.runner.JUnitCommandLineParseResult.parse(JUnitCommandLineParseResult.java:44) 在org.junit.runner.JUnitCore.main(JUnitCore.java:36) 由以下原因引起:java.lang.ClassNotFoundException:org.openqa.selenium.NoAlertPresentException
我的代码如下:
package com.example.tests;
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.ChromeDriverManager;
public class TripPlannerJUnit {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
ChromeDriverManager.getInstance().setup();
driver = new ChromeDriver();
baseUrl = "https://www.google.com.au/.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testTripPlannerJUnit() throws Exception {
driver.get("https://transportnsw.info/trip#/");
driver.findElement(By.id("search-input-From")).click();
driver.findElement(By.id("search-input-From")).clear();
driver.findElement(By.id("search-input-From")).sendKeys("North Sydney Station");
driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='North Sydney Station'])[1]/following::li[1]")).click();
driver.findElement(By.id("search-input-To")).click();
driver.findElement(By.id("search-input-To")).clear();
driver.findElement(By.id("search-input-To")).sendKeys("Town Hall Station");
driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Town Hall Station'])[1]/following::ul[1]")).click();
driver.findElement(By.id("search-button")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
public boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
答案 0 :(得分:0)
如果它可以在Eclipse中编译并运行,则将向项目添加其他jar文件。它将类似于:
您需要将所有那些依赖项添加到您的类路径中。可以使用Including all the jars in a directory within the Java classpath中所述的通配符。但是,如果要独立运行代码,则将在类路径中需要Eclipse Java构建路径的所有库。