Appium Python

0 Comments

Getting Started with Appium in Python

Introduction

Appium is an open-source automation tool for testing mobile applications on Android and iOS. It enables testers to write scripts in Python and interact with mobile apps using the WebDriver protocol. Appium provides cross-platform support, allowing the same test script to be executed on both iOS and Android devices with minimal modifications. In this guide, we will cover how to set up Appium in Python, write your first test script, and explore advanced automation techniques.


1. Installing Appium and Dependencies

Before writing Appium test scripts, you need to set up your environment properly.

Step 1: Install Python

Ensure Python is installed on your system. You can download it from Python’s official site and verify installation with:

python --version

Step 2: Install Appium-Python-Client

Use the following command to install the Appium Python client:

pip install Appium-Python-Client

Step 3: Install Appium Server

You need to install Appium Desktop or use the Appium CLI. Download Appium Desktop from here or install it using Node.js:

npm install -g appium

Start the Appium server with:

appium

Step 4: Install Additional Dependencies

Depending on your mobile OS, you may need the following tools:

  • Android: Install Android SDK, set up an emulator or connect a physical device.
  • iOS: Install Xcode, enable WebDriverAgent, and configure provisioning profiles.

2. Setting Up Appium WebDriver

Once Appium is installed, you need to initialize the WebDriver in your Python script.

from appium import webdriver

desired_caps = {
    "platformName": "Android",
    "deviceName": "emulator-5554",
    "app": "path/to/app.apk"
}

driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
driver.quit()

This script launches the Appium server, connects to an Android emulator, and initializes the test session.

Understanding Desired Capabilities

  • platformName: Specifies the mobile OS (Android or iOS).
  • deviceName: Specifies the connected device or emulator.
  • app: Path to the mobile application (APK for Android, IPA for iOS).
  • Additional capabilities like automationName, appActivity, and appPackage can further customize the setup.

3. Locating Mobile Elements

Appium provides multiple strategies to locate elements in a mobile application.

from appium.webdriver.common.mobileby import MobileBy

element = driver.find_element(MobileBy.ACCESSIBILITY_ID, "loginButton")  # Finding an element by accessibility ID

Other Element Locating Strategies:

  • MobileBy.ID – Locate an element by resource ID.
  • MobileBy.CLASS_NAME – Locate an element by class name.
  • MobileBy.XPATH – Locate an element using an XPath expression.
  • MobileBy.CSS_SELECTOR – Locate elements using CSS selectors (limited support in Appium).

4. Performing Actions on Mobile Elements

Once you locate an element, you can interact with it:

element.send_keys("Appium Python")  # Typing into a text field
element.click()  # Clicking a button

Additional Actions:

  • clear(): Clears text from an input field.
  • is_displayed(): Checks if an element is visible.
  • is_enabled(): Checks if an element is enabled.
  • is_selected(): Checks if an element is selected.

5. Handling Waits in Appium

Using waits ensures that elements are loaded before interaction, preventing NoSuchElementException errors.

Explicit Waits:

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

wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.ID, "search")))

Implicit Waits:

driver.implicitly_wait(10)

Implicit waits apply to all elements and are useful for dynamic applications.


6. Handling Gestures and Touch Actions

Scrolling

driver.execute_script("mobile: scroll", {"direction": "down"})

Swiping

driver.swipe(start_x=100, start_y=500, end_x=100, end_y=100, duration=800)

Tapping

from appium.webdriver.common.touch_action import TouchAction

touch = TouchAction(driver)
touch.tap(x=200, y=400).perform()

7. Interacting with WebViews in Hybrid Apps

Some mobile applications use WebView components. To interact with them, switch to the WebView context:

contexts = driver.contexts  # Get available contexts
driver.switch_to.context(contexts[1])  # Switch to WebView context

8. Automating Mobile Browser Testing

Appium can automate mobile web browsers like Chrome (Android) and Safari (iOS). Example for Chrome:

desired_caps = {
    "platformName": "Android",
    "deviceName": "emulator-5554",
    "browserName": "Chrome"
}

driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
driver.get("https://www.google.com")

9. Running Tests on Real Devices

For real device testing:

  • Android: Enable Developer Mode and USB Debugging.
  • iOS: Set up provisioning profiles and use a valid Apple Developer account.

Use adb devices to check connected devices:

adb devices

For iOS, list connected devices:

instruments -s devices

Conclusion

Appium in Python is a powerful tool for mobile automation testing. With these basic and advanced concepts, you can build robust automation scripts. In future posts, we will cover advanced topics like:

  • Data-driven testing with Appium.
  • Integrating Appium with CI/CD pipelines.
  • Parallel execution of mobile tests.
  • Handling complex gestures and mobile actions.