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

Appium测试新建会话失败求助:SessionNotCreatedException报错

Appium SessionNotCreatedException 排查求助

我是首次使用Appium,已在Android Studio中创建基础计算器应用,项目包含Instrumented Tests测试包。尝试编写基础测试完成环境搭建时,每次运行都报以下错误:

org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are an invalid address of the remote server or browser start-up failure.

Appium服务器已启动,但运行代码时服务器终端无任何输出。已尝试调整Appium Java Client版本,问题仍未解决,恳请协助排查。

以下是完整测试代码和libs.versions.toml配置文件:

测试代码

package com.example.myapplication

import io.appium.java_client.AppiumDriver
import io.appium.java_client.android.AndroidDriver
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.openqa.selenium.By
import org.openqa.selenium.remote.DesiredCapabilities
import java.net.URL
import org.junit.Assert.assertTrue

@RunWith(JUnit4::class)
class ExampleInstrumentedTest {

    private lateinit var driver: AppiumDriver

    @Before
    fun setUp() {
        val capabilities = DesiredCapabilities().apply {
            setCapability("platformName", "Android")
            setCapability("deviceName", "Androidphone1")
            setCapability("platformVersion", "11")
            setCapability("automationName", "UiAutomator2")
            setCapability("appPackage", "com.example.myapplication")
            setCapability("appActivity", "com.example.myapplication.MainActivity")
        }

        val url = URL("https://127.0.0.1:4723/wd/hub")
        driver = AndroidDriver(url, capabilities)
    }

    @Test
    fun testCalculatorAddition() {
        // Enter first number
        val num1Input = driver.findElement(By.xpath("//android.widget.EditText[1]"))
        num1Input.sendKeys("5")

        // Enter second number
        val num2Input = driver.findElement(By.xpath("//android.widget.EditText[2]"))
        num2Input.sendKeys("3")

        // Click addition button
        val addButton = driver.findElement(By.xpath("//android.widget.Button[@text='+']"))
        addButton.click()

        // Click calculate button
        val calculateButton = driver.findElement(By.xpath("//android.widget.Button[@text='Calculate']"))
        calculateButton.click()

        // Check result
        val resultText = driver.findElement(By.xpath("//android.widget.TextView[contains(@text, 'Result:')]"))
        assertTrue("Addition result is incorrect", resultText.text.contains("8"))
    }

    @After
    fun tearDown() {
        if (this::driver.isInitialized) {
            driver.quit()
        }
    }
}

libs.versions.toml 配置

[versions]
agp = "8.6.0"
javaClient = "8.3.0"
kotlin = "1.9.0"
coreKtx = "1.13.1"
junit = "4.13.2"
junitVersion = "1.2.1"
espressoCore = "3.6.1"
lifecycleRuntimeKtx = "2.8.5"
activityCompose = "1.9.2"
composeBom = "2024.09.00"
testng = "7.7.1"

[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
java-client = { module = "io.appium:java-client", version.ref = "javaClient" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
androidx-ui = { group = "androidx.compose.ui", name = "ui" }
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
testng = { module = "org.testng:testng", version.ref = "testng" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }

排查方案

1. 修正Appium连接协议

代码中使用了https://127.0.0.1:4723/wd/hub,但默认Appium服务器仅启用HTTP协议,改为http://127.0.0.1:4723/wd/hub即可尝试连接。这是导致服务器无输出、连接失败的常见原因。

2. 验证设备配置有效性

  • 执行adb devices命令,确认测试设备/模拟器已被ADB识别,确保代码中deviceName和platformVersion与实际设备完全匹配(比如模拟器的deviceName通常为emulator-5554)。
  • 确认设备已开启开发者选项和USB调试功能。

3. 校验App包名与启动Activity

打开计算器应用后,执行adb shell dumpsys window | grep mCurrentFocus命令,获取当前应用的appPackage和appActivity,确保与代码中配置完全一致(大小写、路径不能出错)。

4. 调整依赖兼容性

  • AGP 8.6.0版本较新,尝试将Appium Java Client版本调整为8.0.0或最新稳定版(如8.12.1),同时确保Appium服务器版本与Java Client版本兼容(建议使用Appium Server 2.5.1及以上)。
  • 确认java-client依赖已添加到androidTestImplementation配置项中,而非implementation。

5. 开启Appium调试日志

启动Appium服务器时添加--log-level debug参数,查看详细日志,排查端口监听、权限等底层问题。

6. 编写最简测试验证连接

先编写仅初始化Driver的最简测试,确认Session能否正常创建:

@Before
fun setUp() {
    val capabilities = DesiredCapabilities().apply {
        setCapability("platformName", "Android")
        setCapability("deviceName", "emulator-5554") // 替换为你的设备名
        setCapability("platformVersion", "11")
        setCapability("automationName", "UiAutomator2")
        setCapability("appPackage", "com.example.myapplication")
        setCapability("appActivity", "com.example.myapplication.MainActivity")
    }
    val url = URL("http://127.0.0.1:4723/wd/hub")
    driver = AndroidDriver(url, capabilities)
}

@Test
fun testBasicConnection() {
    println("Session created successfully")
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 16:49:52