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

请求协助排查Appium会话创建失败问题及代码优化方案

Appium会话创建失败问题排查与代码调整方案

错误信息汇总

[ERROR] RunParallelTests.setup:46 » SessionNotCreated Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.

环境信息

  • 主机信息:主机名 AMACTGW2L3P2W,IP地址 2406:7400:98:667a:50de:f901:433a:e152%en0
  • 构建信息:版本 4.15.0,修订版本 1d14b5521b
  • 系统信息:Mac OS X 15.5(aarch64架构),Java版本 17.0.15
  • 驱动:io.appium.java_client.android.AndroidDriver
  • 配置Capabilities:
{
  "appium:app": "/Users/chaitra.m.swamy/Documents/GitHub/Business-Mobile-Automation/apps/android/app-debug11220.apk",
  "appium:autoGrantPermissions": true,
  "appium:automationName": "UiAutomator2",
  "appium:deviceName": "Pixel 9 Pro",
  "appium:disableWindowAnimation": true,
  "appium:newCommandTimeout": 300,
  "appium:platformVersion": "15",
  "appium:udid": "4A071FDAP001WM",
  "appium:unlockKey": "5858",
  "appium:unlockType": "pin",
  "platformName": "ANDROID"
}

排查步骤

  1. 验证Appium服务状态

    • 确认Appium服务器已启动,监听地址与测试代码配置一致(默认http://localhost:4723/wd/hub)
    • 执行curl http://localhost:4723/wd/hub/status,返回{"value":{"ready":true,...}}则服务正常
  2. 检查设备连接状态

    • 运行adb devices,确认设备4A071FDAP001WM处于device状态,若离线则重新插拔USB或重启ADB调试
  3. 验证APK有效性

    • 检查APK路径是否存在、文件是否损坏
    • 手动安装APK到设备:adb install /Users/chaitra.m.swamy/Documents/GitHub/Business-Mobile-Automation/apps/android/app-debug11220.apk,确认能正常启动
  4. 兼容性检查

    • 执行appium driver list查看UiAutomator2驱动版本,若与Appium 4.15.0不兼容,更新驱动:appium driver update uiautomator2
    • 确认当前UiAutomator2驱动版本支持Android 15系统
  5. 设备解锁配置验证

    • 确认设备解锁方式为PIN且密码是5858,若有变更同步更新Capabilities
    • 手动解锁设备后再运行测试,排除解锁逻辑问题

代码调整建议

  1. 添加Appium服务连接校验
    在创建会话前先校验服务可用性:
import java.net.HttpURLConnection;
import java.net.URL;

public boolean isAppiumServerRunning(String serverUrl) {
    try {
        URL url = new URL(serverUrl + "/status");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        return connection.getResponseCode() == 200;
    } catch (Exception e) {
        return false;
    }
}

// 在setup方法中加入校验
@BeforeMethod
public void setup() {
    String appiumUrl = "http://localhost:4723/wd/hub";
    if (!isAppiumServerRunning(appiumUrl)) {
        throw new RuntimeException("Appium服务未启动:" + appiumUrl);
    }
    // 后续会话创建代码
}
  1. 优化Capabilities配置
    明确指定应用包名和启动页,减少自动识别开销;若APK已安装,可跳过重复安装:
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "ANDROID");
caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Pixel 9 Pro");
caps.setCapability(MobileCapabilityType.UDID, "4A071FDAP001WM");
caps.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
caps.setCapability("appium:platformVersion", "15");
caps.setCapability("appium:autoGrantPermissions", true);
caps.setCapability("appium:disableWindowAnimation", true);
caps.setCapability("appium:newCommandTimeout", 300);
// 添加appPackage和appActivity(通过aapt dump badging命令获取)
caps.setCapability("appium:appPackage", "com.your.app.package");
caps.setCapability("appium:appActivity", "com.your.app.MainActivity");
// 若APK已安装,注释掉以下行
// caps.setCapability("appium:app", "/Users/chaitra.m.swamy/Documents/GitHub/Business-Mobile-Automation/apps/android/app-debug11220.apk");
  1. 增加会话创建重试机制
    针对偶发失败添加重试逻辑:
import org.testng.Assert;

public AndroidDriver createDriverWithRetry(int maxRetries, String appiumUrl, DesiredCapabilities caps) {
    int retryCount = 0;
    while (retryCount < maxRetries) {
        try {
            return new AndroidDriver(new URL(appiumUrl), caps);
        } catch (SessionNotCreatedException e) {
            retryCount++;
            if (retryCount == maxRetries) throw e;
            try { Thread.sleep(2000); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); }
        } catch (Exception e) {
            throw new RuntimeException("驱动创建失败", e);
        }
    }
    return null;
}

// 在setup中使用
@BeforeMethod
public void setup() {
    DesiredCapabilities caps = new DesiredCapabilities();
    // 配置capabilities...
    String appiumUrl = "http://localhost:4723/wd/hub";
    driver = createDriverWithRetry(3, appiumUrl, caps);
    Assert.assertNotNull(driver, "3次重试后仍无法创建Appium会话");
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 08:13:12