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

如何确认自动化代码已与Appium建立连接并运行(Linux/Ubuntu环境)

Hey there! Let's walk through how you can verify if your Appium capabilities are properly configured and connected to the service on your Ubuntu environment, plus troubleshoot any potential issues along the way.

1. First, Confirm Appium Service is Running

Since you're working without a GUI, start by launching Appium in your terminal and checking its output:

  • Run the command: appium
  • Look for a line like Appium REST http interface listener started on 0.0.0.0:4723 — this means the service is up and listening for connections.
  • If you get a "port in use" error, kill the conflicting process with:
    lsof -i :4723  # Find the PID of the process using port 4723
    kill -9 <PID>  # Replace <PID> with the number from the previous command
    
    Then restart Appium.

2. Validate Your Capabilities Configuration

Double-check these critical parameters in your capabilities — mismatches here are the most common cause of connection failures:

  • platformName: Must be exactly "Android" or "iOS" (case-sensitive)
  • deviceName: For Android, run adb devices to get your device/emulator ID (e.g., emulator-5554) and ensure it matches your config.
  • appPackage & appActivity (Android only): Use this command to get the correct values for your app:
    adb shell dumpsys window | grep mCurrentFocus
    
    You’ll get output like com.example.myapp/.MainActivity — split this into appPackage: "com.example.myapp" and appActivity: ".MainActivity".
  • automationName: Use "UiAutomator2" for Android (required for modern apps) or "XCUITest" for iOS.

Here’s an example of a valid Android capabilities set for reference:

desired_caps = {
    "platformName": "Android",
    "deviceName": "emulator-5554",
    "appPackage": "com.example.myapp",
    "appActivity": ".MainActivity",
    "automationName": "UiAutomator2",
    "noReset": True,  # Keeps app state between test runs
    "newCommandTimeout": 30  # Prevents session timeout if tests pause
}

3. Test Your Wait Logic & Connection

Your existing wait logic is a good start — use explicit waits instead of hard sleeps to make tests reliable. Example:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# Wait up to 10 seconds for the login button to be present
try:
    login_button = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "com.example.myapp:id/login_button"))
    )
    print("Success! Connected to app and located login button.")
except Exception as e:
    print(f"Failed to locate element or connect: {str(e)}")

If this times out, check the Appium debug logs (see next step) for clues — it could mean the app didn’t launch correctly, or your element locator is wrong.

4. Dig Into Appium Debug Logs

For detailed troubleshooting, start Appium with debug-level logging:

appium --log-level debug

Run your test code, then look for these key log entries:

  • New session created successfully: Confirms your capabilities are valid and a connection was established.
  • Failed to create session: The error message immediately after this will tell you exactly what’s wrong (e.g., invalid appPackage, device not found, incompatible automation engine version).

5. Quick Additional Checks

  • For Android: Ensure adb devices shows your device/emulator as device (not offline), and that USB debugging is enabled on the device/emulator.
  • Verify version compatibility: Check that your Appium version (appium -v), client library version (e.g., pip show appium-python-client for Python), and automation engine (like UiAutomator2) are compatible with each other and your OS/app version.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:32:33