You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

构建Appium Java框架时遇HasInputDevices类找不到错误求助

问题:Appium Java测试框架运行时报NoClassDefFoundError错误

构建基于Appium的Java测试框架时,运行测试出现以下错误:
java.lang.NoClassDefFoundError: org/openqa/selenium/interactions/HasInputDevices

未添加额外Selenium依赖,即使手动添加selenium-java依赖,该错误仍然存在。以下是项目的pom.xml和测试运行器代码:

pom.xml

<properties>
    <maven.compiler.source>16</maven.compiler.source>
    <maven.compiler.target>16</maven.compiler.target>
    <serenity.version>3.2.5</serenity.version>
    <serenity.cucumber.version>3.2.5</serenity.cucumber.version>
    <cucumber.version>7.3.3</cucumber.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>net.serenity-bdd.maven.plugins</groupId>
            <artifactId>serenity-maven-plugin</artifactId>
            <version>${serenity.version}</version>
            <executions>
                <execution>
                    <id>serenity-reports</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>aggregate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<dependencies>

    <dependency>
        <groupId>net.serenity-bdd</groupId>
        <artifactId>serenity-cucumber</artifactId>
        <version>${serenity.version}</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
    </dependency>

    <dependency>
        <groupId>org.jeasy</groupId>
        <artifactId>easy-random-core</artifactId>
        <version>5.0.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.2.2</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.17.2</version>
    </dependency>

    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
    </dependency>

    <dependency>
        <groupId>com.github.javafaker</groupId>
        <artifactId>javafaker</artifactId>
        <version>1.0.2</version>
    </dependency>

    <dependency>
        <groupId>io.appium</groupId>
        <artifactId>java-client</artifactId>
        <version>8.1.1</version>
    </dependency>

</dependencies>

测试运行器代码

import io.cucumber.junit.CucumberOptions;
import net.serenitybdd.cucumber.CucumberWithSerenity;
import org.junit.runner.RunWith;

@RunWith(CucumberWithSerenity.class)
@CucumberOptions(
    features = {"src/test/resources/features"},
    glue = {"steps"},
    tags = "@appiumApp and not @ignore",
    stepNotifications = true,
    plugin = {
            "json:target/cucumber-report/cucumber.json"
    }
 )
 public class TestRunner {
 }

解决方案

这个错误的核心是依赖版本冲突:

  • HasInputDevices接口在Selenium 4.x版本中已被移除/重构
  • Appium Java Client 8.1.1依赖的是Selenium 4.x版本
  • Serenity BDD 3.2.5依赖的Selenium版本与Appium要求的版本不兼容,导致类找不到

可以通过以下两种方式解决:

方式一:统一Selenium版本,强制所有依赖使用同一版本

在pom.xml中添加dependencyManagement节点,指定兼容的Selenium版本(Serenity 3.2.5适配Selenium 4.4.0):

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.4.0</version>
        </dependency>
    </dependencies>
</dependencyManagement>

方式二:排除Appium依赖中冲突的Selenium模块

修改Appium的依赖配置,排除它自带的Selenium相关模块,让项目使用Serenity提供的版本:

<dependency>
    <groupId>io.appium</groupId>
    <artifactId>java-client</artifactId>
    <version>8.1.1</version>
    <exclusions>
        <exclusion>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-remote-driver</artifactId>
        </exclusion>
    </exclusions>
</dependency>

额外建议

如果上述方法无效,可以尝试升级Serenity BDD版本到3.9.x及以上,新版本对Selenium 4.x的兼容性更好,能减少此类冲突问题。


内容的提问来源于stack exchange,提问作者Bilal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 20:06:22