selenium - Selenium - NoClassDefFoundError: org/openqa/selenium/NoAlertPresentException
问题描述
NoClassDefFoundError: org/openqa/selenium/NoAlertPresentException: 我正在执行一段给出此错误的代码。这是使用 Selenium 在 eclipse 上用 Junit 编写的。它通过 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 线程“main”中的异常 java.lang.NoClassDefFoundError: org/openqa/selenium/NoAlertPresentException at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName( Class.java:398) 在 org.junit.internal.Classes.getClass(Classes.java:16) 在 org.junit.runner.JUnitCommandLineParseResult。
我的代码如下:
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;
}
}
}
解决方案
如果它在 Eclipse 中编译和运行,您将在项目中添加额外的 jar 文件。它看起来类似于:
您将需要将所有这些依赖项添加到您的类路径中。可以使用通配符,如在Java 类路径内的目录中包含所有 jars 中所述。但是,如果您独立运行代码,则需要在类路径中包含 Eclipse Java 构建路径的所有库。
推荐阅读
- cordova - Cordova 相机插件 DATA_URL 不返回文件名或扩展名
- javascript - 我正在尝试使用 react-redux 存储我的数据,但它没有。我该如何解决这个问题?
- azure-devops - 如何从 azure TFS 2019 日历板上删除休息日
- ios - 从项目 xcode 中删除 git
- c# - Excel 下载后无法读取数据
- c - 在哪里寻找 C 的 write 函数的文档
- git - 如何合并 2 个远程分支?
- python - Caddy 服务器、X-Forwarded-For 和欺骗值
- python - ImportError:没有名为 parse python/node js 的模块
- google-apps-script - 选择谷歌表格中每个指定列的下一个打开行的数据输入表单