Appium Mobile UI Automation Testing Workflow with Python
This guide walks through setting up Appium with the UiAutomator2 driver, verifying the environment, writing a Python script that connects to the Appium server, using weditor or uiautomatorviewer to inspect Android UI elements, and applying ID or XPath locators to automate clicks in the Zepp app.
This article introduces a practical workflow for mobile UI automation testing using Appium, focusing on an Android target (the Zepp app) and Python as the scripting language.
Environment setup
First, install the latest Appium version via npm:
npm i -g appium@nextThen install the UiAutomator2 driver for Android:
appium driver install uiautomator2Make sure the Android SDK and related environment variables are configured (the details are omitted for brevity).
Appium server and verification
Use appium-doctor to verify the environment, then start the Appium server. The server address (e.g., 0.0.0.0:4723 ) will be used later in the test script.
Python test script
Define the desired capabilities for the Android device and the target app:
capabilities = {
'automationName': 'UiAutomator2',
'platformName': 'Android',
'appPackage': "xxx",
'appActivity': "xxx"
}Connect to the Appium server:
driver = webdriver.Remote("0.0.0.0:4723", capabilities)Optionally, start the target activity directly via ADB:
adb shell am start -n packagename/xxxxxx.MainTabActivityIf the activity is not exported, a permission error may occur, indicating that Appium is otherwise functional.
Inspecting UI elements
Use uiautomatorviewer (requires JDK 1.8) or the more convenient weditor tool to view element attributes such as resourceId , class , and package . Install weditor with:
pip install weditorLaunch weditor; it opens a web UI for interactive element inspection.
Locate a switch element by its resourceId (e.g., rainbow_tile_switch ) and perform a click:
# Connect to Appium server
driver = webdriver.Remote("0.0.0.0:4723", capabilities)
# Locate the ringtone switch
ring = driver.find_element(By.ID, "rainbow_tile_switch")
# Click the element
ring.click()If the element is not immediately present, use an implicit wait:
driver.implicitly_wait()Because multiple switches share the same resourceId , XPath may be required for precise targeting.
Locator strategies
Appium 2.0 consolidates older methods (e.g., find_element_by_id ) into the unified find_element(By.ID, ...) syntax. The By class supports strategies such as ID, XPATH, LINK_TEXT, NAME, TAG_NAME, CLASS_NAME, and CSS_SELECTOR:
class By:
"""Set of supported locator strategies."""
ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"Summary
The guide demonstrates how to set up Appium, install necessary drivers, write Python scripts to launch an Android activity, locate UI elements via resource IDs or XPath, and perform click actions, achieving automated UI testing for mobile applications.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.