如何确认自动化代码已与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:
Then restart Appium.lsof -i :4723 # Find the PID of the process using port 4723 kill -9 <PID> # Replace <PID> with the number from the previous command
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, runadb devicesto 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:
You’ll get output likeadb shell dumpsys window | grep mCurrentFocuscom.example.myapp/.MainActivity— split this intoappPackage: "com.example.myapp"andappActivity: ".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., invalidappPackage, device not found, incompatible automation engine version).
5. Quick Additional Checks
- For Android: Ensure
adb devicesshows your device/emulator asdevice(notoffline), 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-clientfor Python), and automation engine (like UiAutomator2) are compatible with each other and your OS/app version.
内容的提问来源于stack exchange,提问作者BenevolentMan

