NoClassDefFoundError: org/openqa/selenium/virtualauthenticator/HasVirtualAuthenticator 在使用 TestNG 和

Posted

技术标签:

【中文标题】NoClassDefFoundError: org/openqa/selenium/virtualauthenticator/HasVirtualAuthenticator 在使用 TestNG 和 Maven 运行测试时出错【英文标题】:NoClassDefFoundError: org/openqa/selenium/virtualauthenticator/HasVirtualAuthenticator error while running test using TestNG and Maven 【发布时间】:2021-12-31 07:18:36 【问题描述】:

我正在尝试使用 Appium 为移动自动化创建一个演示。 创建 Selenium-TestNG-Maven 框架。 安装以下自动化软件: 1.JDK版本- 1.8.0_202 2. Appium 版本- 1.22.1 3.安卓工作室 4 Eclipse - 版本:2020-06 (4.16.0)

Setup Environment variables:
JAVA_HOME: C:\Program Files\Java\jdk1.8.0_202
android_HOME : C:\Users\Name\AppData\Local\Android\Sdk
PATH
   - JRE : C:\Program Files\Java\jre1.8.0_202
   - Tools : C:\Users\Name\AppData\Local\Android\Sdk\tools
   - Tools/bin : C:\Users\Name\AppData\Local\Android\Sdk\tools\bin
   - Platform-tools : C:\Users\Name\AppData\Local\Android\Sdk\platform-tools

Created a Maven project.Below is POM.XML

4.0.0 com.Login.Demo 登录DemoProject 0.0.1-快照

<dependencies>
    <!-- https://mvnrepository.com/artifact/io.appium/java-client -->
    <dependency>
        <groupId>io.appium</groupId>
        <artifactId>java-client</artifactId>
        <version>7.6.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.0.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.testng/testng -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.4.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

下面是BaseClass.Java

import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.remote.MobileCapabilityType;

public class BaseClass 
    
    AppiumDriver<MobileElement> driver;
    @BeforeTest
    public void setup()
    
        try
        
            DesiredCapabilities caps = new DesiredCapabilities();
            //caps.setCapability(CapabilityType.PLATFORM_NAME, "ANDROID");
            caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "ANDROID");
            caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "10");
            caps.setCapability(MobileCapabilityType.DEVICE_NAME, "OnePlus 7T");
            caps.setCapability(MobileCapabilityType.UDID, "52a7cc1a");
            caps.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 60);
            caps.setCapability(MobileCapabilityType.APP, "");
            caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome");

            URL url = new URL("http://0.0.0.1:4723/wd/hub");
            driver = new AppiumDriver<MobileElement>(url, caps);
        
    
        catch (Exception e) 
        
            //System.out.println("" +e.getCause());
        
    
    @Test
    public void sampleTest()
    
        System.out.println("Test running");
    
        @AfterTest
        public void teardown()
        

        
    

使用 RunAs 运行 BaseClass.Java 文件 -> TestNG 测试但出现以下错误。

[RemoteTestNG] detected TestNG version 7.4.0
FAILED CONFIGURATION: @BeforeTest setup
java.lang.NoClassDefFoundError: org/openqa/selenium/virtualauthenticator/HasVirtualAuthenticator
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    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)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    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)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    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)
    at tests.BaseClass.setup(BaseClass.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
    at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:62)
    at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:385)
    at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:321)
    at org.testng.TestRunner.invokeTestConfigurations(TestRunner.java:637)
    at org.testng.TestRunner.beforeRun(TestRunner.java:627)
    at org.testng.TestRunner.run(TestRunner.java:589)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:377)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332)
    at org.testng.SuiteRunner.run(SuiteRunner.java:276)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)
    at org.testng.TestNG.runSuites(TestNG.java:1063)
    at org.testng.TestNG.run(TestNG.java:1031)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.virtualauthenticator.HasVirtualAuthenticator
    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)
    ... 61 more

【问题讨论】:

【参考方案1】:

此错误消息...

java.lang.NoClassDefFoundError: org/openqa/selenium/virtualauthenticator/HasVirtualAuthenticator

...暗示找不到 HasVirtualAuthenticator 类定义,可能是因为 Maven 无法加载该类。


解决方案

根据Class Loading and Forking in Maven Surefire,调整以下任一设置可能会起作用:

useSystemClassLoader:设置'useSystemClassLoader=false'如下:

  <configuration>
    <useSystemClassLoader>false</useSystemClassLoader>
  </configuration>

forkCount

useManifestOnlyJar

PS:确保JDK升级到当前级别JDK 8u311。

【讨论】:

我添加了&lt;useSystemClassLoader&gt;false&lt;/useSystemClassLoader&gt;,但仍然遇到同样的错误【参考方案2】:

这个问题的原因与“pom.xml”中的selenium版本有关。您可以将版本编辑为 3.141.59。你的 pom.xml 文件代码更新:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>

【讨论】:

【参考方案3】:

面临以下问题:

 Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: 
    Unable to create a new remote session. Original error: No such host is known (127.0.0.1.4723)

【讨论】:

如果您有新问题,请点击 按钮提出问题。如果有助于提供上下文,请包含指向此问题的链接。 - From Review

以上是关于NoClassDefFoundError: org/openqa/selenium/virtualauthenticator/HasVirtualAuthenticator 在使用 TestNG 和 的主要内容,如果未能解决你的问题,请参考以下文章

java.lang.NoClassDefFoundError: org/hibernate/QueryTimeoutException

出现错误 java.lang.NoClassDefFoundError: org/mockito/MockitoAnnotations$Mock

错误 500 - NoClassDefFoundError: org/hsqldb/lib/LongValueHashMap

java.lang.NoClassDefFoundError:org/json/JSONObject [重复]

java.lang.NoClassDefFoundError: org/springframework/util/MultiValueMap

异常java.lang.NoClassDefFoundError: org/apache/curator/framework/CuratorFrameworkFactory