杰瑞科技汇

Appium API 如何在 Python 中调用?

Of course! Here is a comprehensive guide to using Python with the Appium API, covering everything from setup to advanced usage with practical code examples.


What is Appium?

Appium is an open-source automation framework for use with native, hybrid, and mobile web apps. It supports automation on iOS, Android, and Windows platforms. The key advantage of Appium is that it uses the WebDriver protocol (Selenium's underlying protocol), which means you can use the same API to automate different platforms.

Prerequisites

Before you start, make sure you have:

  1. Python 3 installed.
  2. Appium Server installed and running. You can download it from the official Appium website or run it via Node.js (npm install -g appium).
  3. Mobile Device/Emulator/Simulator: A physical Android/iOS device, an Android Emulator (via Android Studio), or an iOS Simulator.
  4. Appium Inspector: A useful tool for inspecting the UI elements of your app and getting their locators.

Setting Up Your Python Environment

You'll need the Appium-Python-Client library, which is the official Python client for Appium.

Install it using pip:

pip install Appium-Python-Client

The Core Workflow: A Simple Example

Let's walk through a basic script that launches an app, finds an element, performs an action, and quits.

This example uses the Calculator app on an Android Emulator.

Step 1: Get the Appium Desired Capabilities

Desired Capabilities are a set of key-value pairs that tell the Appium server how to configure the session. This is the most critical part.

  • platformName: The target platform (e.g., "Android", "iOS").
  • deviceName: The name of the device/emulator/simulator.
  • app: The absolute path to the .apk or .app file you want to automate.
  • automationName: The automation engine to use (e.g., "UiAutomator2" for Android, "XCUITest" for iOS).

Step 2: Write the Python Script

Here is a complete, commented script.

# 1. Import necessary libraries
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
import time
# 2. Define the desired capabilities
# NOTE: Replace the values with your own device and app details
caps = {
    "platformName": "Android",
    "deviceName": "Pixel_4_API_30",  # The name of your emulator/device in Appium/Android Studio
    "app": "/path/to/your/app.apk",  # Absolute path to the APK file
    "automationName": "UiAutomator2",
    "autoLaunch": True,  # Let Appium launch the app
    "noReset": True,    # Don't reset the app state after the session
    "fullReset": False  # Don't perform a full reset
}
# 3. Initialize the Appium driver
# The 'appium_server_url' is usually 'http://localhost:4723/wd/hub'
appium_server_url = 'http://localhost:4723/wd/hub'
driver = None
try:
    # Start the session
    driver = webdriver.Remote(appium_server_url, caps)
    print("Appium session started successfully!")
    # 4. Interact with the app
    # --- Find elements using different locator strategies ---
    # Example 1: Find by ID (most reliable)
    # This finds the '5' button on a calculator
    button_5 = driver.find_element(by=AppiumBy.ID, value="com.android.calculator2:id/digit_5")
    button_5.click()
    print("Clicked on button 5")
    # Example 2: Find by Accessibility ID (recommended for text)
    # This finds the '+' button
    button_plus = driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value="plus")
    button_plus.click()
    print("Clicked on plus button")
    # Example 3: Find by XPath (powerful but can be slow)
    # This finds the '9' button
    button_9 = driver.find_element(by=AppiumBy.XPATH, value='//android.widget.Button[@text="9"]')
    button_9.click()
    print("Clicked on button 9")
    # Example 4: Find by Class Name
    # This finds the '=' button
    button_equals = driver.find_element(by=AppiumBy.CLASS_NAME, value="android.widget.Button")
    # Be careful with class name if there are multiple elements. We'd usually add more context.
    # A better way to find '=':
    button_equals = driver.find_element(by=AppiumBy.XPATH, value='//android.widget.Button[@content-desc="equals"]')
    button_equals.click()
    print("Clicked on equals button")
    # 5. Get some result
    result_text = driver.find_element(by=AppiumBy.ID, value="com.android.calculator2:id/result").text
    print(f"The result is: {result_text}")
    # Wait for 5 seconds to see the result on the screen
    time.sleep(5)
except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # 6. Quit the driver
    if driver:
        driver.quit()
        print("Appium session ended.")

Common Appium Python API Methods

Here are the most frequently used methods from the Appium-Python-Client.

Finding Elements

Method Description Example
find_element(by, value) Finds a single element. Raises NoSuchElementException if not found. driver.find_element(AppiumBy.ID, 'my_element_id')
find_elements(by, value) Finds a list of elements. Returns an empty list if none are found. driver.find_elements(AppiumBy.CLASS_NAME, 'android.widget.TextView')

Locator Strategies (by):

  • AppiumBy.ID: The unique resource ID of the element. (Most Recommended)
  • AppiumBy.ACCESSIBILITY_ID: The content-desc property. (Highly Recommended)
  • AppiumBy.XPATH: A path to the element in the XML structure. Very powerful.
  • AppiumBy.CLASS_NAME: The class name of the UI element.
  • AppiumBy.ANDROID_UIAUTOMATOR: Uses Android's UIAutomator library for complex queries (e.g., new UiSelector().text("Settings")).
  • AppiumBy.IOS_PREDICATE: Uses iOS's NSPredicate format for finding elements.

Interacting with Elements

Method Description
.click() Taps the element.
.send_keys(text) Types text into a text field.
.clear() Clears the text from a text field.
.is_displayed() Checks if the element is visible on the screen.
.is_enabled() Checks if the element is enabled.
.get_attribute(name) Gets an attribute of the element (e.g., text, content-desc, resource-id).
.text Gets the visible text of the element.

Driver and Session Management

Method Description
driver.get(url) Navigates to a web URL (for hybrid or web apps).
driver.current_context Gets the current context (e.g., 'NATIVE_APP', 'WEBVIEW_1').
driver.contexts Gets a list of all available contexts.
driver.switch_to.context(context_name) Switches between contexts (e.g., from native app to a web view).
driver.back() Simulates pressing the device's back button.
driver.orientation Gets or sets the screen orientation ('LANDSCAPE' or 'PORTRAIT').
driver.get_screenshot_as_file(filename) Takes a screenshot and saves it to a file.
driver.quit() Closes the driver and ends the Appium session. (Essential in finally block)

Advanced Topics

Gestures (Touch Actions)

Appium can simulate complex touch gestures like swiping, tapping with multiple fingers, etc. You use the TouchAction class.

Example: Swiping from the bottom of the screen to the top (like opening the app drawer on Android).

from appium.webdriver.common.touch_action import TouchAction
# ... inside your try block ...
# Get the size of the screen
size = driver.get_window_size()
width = size['width']
height = size['height']
# Define start and end points for the swipe
start_x = width / 2
start_y = height * 0.8  # Start from 80% from the top
end_x = width / 2
end_y = height * 0.2  # End at 20% from the top
# Perform the swipe
actions = TouchAction(driver)
actions.press(x=start_x, y=start_y).wait(500).move_to(x=end_x, y=end_y).release().perform()
print("Swiped up!")

Working with Web Views

Hybrid apps contain a web view component. To automate web elements inside it, you must first switch the context.

# ... after driver is initialized ...
# 1. Get the list of available contexts
contexts = driver.contexts
print("Available contexts:", contexts)
# 2. Switch to the web view context
# The name might be 'WEBVIEW_com.example.myapp' or similar
webview_context = None
for context in contexts:
    if 'WEBVIEW' in context:
        webview_context = context
        break
if webview_context:
    driver.switch_to.context(webview_context)
    print("Switched to context:", webview_context)
    # 3. Now you can use Selenium-like commands to find web elements
    # Make sure you have Selenium installed: pip install selenium
    from selenium.webdriver.common.by import By
    web_element = driver.find_element(By.ID, 'username_field')
    web_element.send_keys("my_user")
    # 4. Switch back to the native app context when done
    driver.switch_to.context('NATIVE_APP')
    print("Switched back to NAPP context")
else:
    print("Could not find a WEBVIEW context.")

Best Practices

  1. Use Explicit Waits: Never use time.sleep(). It makes your tests slow and unreliable. Use WebDriverWait to wait for an element to be present, visible, or clickable.

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    # Wait up to 10 seconds for the element to be clickable
    wait = WebDriverWait(driver, 10)
    element = wait.until(EC.element_to_be_clickable((AppiumBy.ID, "com.example.app:id/submit_button")))
    element.click()
  2. Use Page Object Model (POM): Organize your code by creating classes for each screen/page of your app. This makes your tests more readable and maintainable.

  3. Centralize Capabilities: Store your desired capabilities in a separate configuration file (e.g., config.py or a .json file) instead of hardcoding them.

  4. Always Quit the Driver: Use a try...finally block to ensure driver.quit() is always called, even if your test fails. This prevents orphaned Appium sessions.

This guide should give you a solid foundation for automating mobile applications with Python and Appium. Happy testing

分享:
扫描分享到社交APP
上一篇
下一篇