对于自动化以及与 Java 相关的所有事物,我都是新手。
从 IntelliJ 执行用 Java 编写的自动化代码时,我遇到了下面给出的错误:
java.lang.NoClassDefFoundError: net/jodah/failsafe/Policy
at org.openqa.selenium.remote.http.ClientConfig.<clinit>(ClientConfig.java:33)
at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:95)
at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:85)
at org.openqa.selenium.remote.service.DriverCommandExecutor.<init>(DriverCommandExecutor.java:80)
at org.openqa.selenium.chromium.ChromiumDriverCommandExecutor.<init>(ChromiumDriverCommandExecutor.java:35)
at org.openqa.selenium.chrome.ChromeDriver$ChromeDriverCommandExecutor.<init>(ChromeDriver.java:118)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:106)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:93)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:48)
at AssertionsExample2.setup(AssertionsExample2.java:18)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:458)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142)
at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:168)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:105)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)
Caused by: java.lang.ClassNotFoundException: net.jodah.failsafe.Policy
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
... 35 more
我的部分代码:
public final class AssertionsExample2 {
WebDriver driver;
@BeforeClass
void setup()
{
System.setProperty("webdriver.chrome.driver", "C:/Users/tlwri/IdeaProjects/sampleJavaProject/Drivers/chromedriver_win32/chromedriver.exe");
driver=new ChromeDriver();
}
@Test (priority = 1)
void logoTest()
{
WebElement logo=driver.findElement(By.xpath("//*[@id=\"divLogo\"]/img"));
Assert.assertTrue(logo.isDisplayed(),"Logo is not displayed on the page");
}
如何修复这个错误?
我在 Intellij IDEA 中运行 Selenium 时遇到类似的错误。 除了 thMe odule 设置菜单下的“selenium-java-[version]”文件夹之外,我还通过将“selenium-java-[version]/lib”添加到全局库来修复此问题。
我遇到了同样的问题,通过下载failsafe .jar(https://mavenlibs.com/jar/file/dev.failsafe/failsafe)并将其添加到类路径
解决了这个问题我遇到了这个错误,当我添加 selenium lib 文件夹中的所有 jar 时,它消失了。
您的
Maven/Gradle 项目中缺少
failsafe
依赖项/库:
Maven(pom.xml):
<dependency>
<groupId>net.jodah</groupId>
<artifactId>failsafe</artifactId>
<version>2.4.4</version>
</dependency>
Gradle(构建.gradle):
dependencies {
implementation group: 'net.jodah', name: 'failsafe', version: '2.4.4'
}