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

Serenity3.0.5+Cucumber3.0.5版本实例化AppiumDriver失败如何解决

Serenity 3.0.5版本Appium页面实例化报错解决方案

你遇到的报错核心原因是Serenity 3.x系列对Appium的集成逻辑、依赖要求、配置项都和1.x系列存在不兼容变更,可按照以下步骤逐一修复:

1. 修正依赖版本匹配

Serenity 3.0.5必须配套版本一致的serenity-appium依赖,同时Appium Java Client版本需固定为7.6.0,禁止使用8.x及以上版本避免类结构不兼容。Maven依赖示例如下:

<dependency>
    <groupId>net.serenity-bdd</groupId>
    <artifactId>serenity-appium</artifactId>
    <version>3.0.5</version>
</dependency>
<dependency>
    <groupId>io.appium</groupId>
    <artifactId>java-client</artifactId>
    <version>7.6.0</version>
</dependency>

2. 修正Serenity.properties配置

  • 废弃配置替换:将appium.automationName = Appium改为appium.automationName = UiAutomator2,Android平台从Appium 2.0开始已经移除旧的Appium自动化引擎,必须使用UiAutomator2。
  • 驱动配置优化:将webdriver.driver = appium改为webdriver.driver = android,更符合3.x版本的驱动识别逻辑。
    修正后完整配置参考:
webdriver.driver= android
appium.hub = http://localhost:4723/wd/hub
######## android CAPS ######
appium.automationName = UiAutomator2
appium.platformName= Android
appium.platformVersion = 11.0
appium.deviceName  = emulator-5554
appium.app = serenity-cucumber-starter/src/test/resources/Calculator.apk

3. 简化PageObject实现,移除不兼容初始化逻辑

Serenity 3.x已经内置@AndroidFindBy/@iOSFindBy注解的自动识别支持,不需要自定义Predicate形式的初始化逻辑,也不需要额外封装MobilePageObject基类,直接继承PageObject即可正常使用:

package pageObjects;

import io.appium.java_client.pagefactory.AndroidFindBy;
import net.serenitybdd.core.pages.PageObject;
import org.openqa.selenium.WebElement;

public class LoginPageMobile extends PageObject {

    @AndroidFindBy(xpath="//android.widget.Button[@text='Log In']")
    private WebElement WPLogInButton;
    public void doLogin(){
        // 注:原代码给按钮输入文本属于逻辑错误,按钮不可输入,修正为点击操作
        clickOn(WPLogInButton);
    }
}

如果确需自定义基类,使用兼容3.x的写法即可:

package pageObjects;

import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import net.serenitybdd.core.pages.PageObject;
import net.thucydides.core.webdriver.WebDriverFacade;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;

public class MobilePageObject extends PageObject {
    @Override
    public void initElements(WebDriver driver) {
        if (driver instanceof WebDriverFacade facade) {
            WebDriver proxiedDriver = facade.getProxiedDriver();
            PageFactory.initElements(new AppiumFieldDecorator(proxiedDriver), this);
        } else {
            super.initElements(driver);
        }
    }
}

4. 运行前校验

  • 确认Appium Server已启动,4723端口无占用
  • 执行adb devices确认模拟器/真机emulator-5554已正常连接
  • 确认apk路径为相对于项目根目录的正确路径

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 11:45:08