我尝试使用带有 Selenium 的 Eclipse 新设置来执行简单的 TestNG 脚本。 虽然使用相同的设置我可以运行其他 Java 类,但是当尝试运行 TestNG 套件时我收到此错误。
我在添加 slf4j jars 后尝试过,但没有成功。
public class NewTest {
WebDriver driver;
@BeforeSuite
public void openBrowser() {
System.setProperty("webdriver.chrome.driver",
"D:\\Eclipse\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
}
}
我得到了:
[RemoteTestNG] detected TestNG version 7.10.1
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at org.testng.log4testng.Logger.lambda$getLogger$0(Logger.java:30)
at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1710)
at org.testng.log4testng.Logger.getLogger(Logger.java:30)
at org.testng.TestNG.<clinit>(TestNG.java:111)
at org.testng.remote.support.RemoteTestNGFactory7_9.createRemoteTestNG(RemoteTestNGFactory7_9.java:16)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:67)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
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:525)
... 6 more
这是为什么?我该如何解决?
您遇到错误是因为您使用了两个不同版本的 SLF4J 库,
对于 SLF4J 1.7.x:
Maven:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.36</version>
</dependency>
摇篮:
implementation 'org.slf4j:slf4j-api:1.7.36'
implementation 'org.slf4j:slf4j-jdk14:1.7.36'
对于 SLF4J 2.x:
Maven:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.9</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>2.0.9</version>
</dependency>
摇篮
implementation 'org.slf4j:slf4j-api:2.0.9'
implementation 'org.slf4j:slf4j-jdk14:2.0.9'