Using Appium with Python is a popular and powerful way to automate testing for Android and iOS applications. Below is a practical guide to get you started with Appium in Python using pytest or unittest.

What You Need

ToolPurpose
AppiumMobile automation engine
PythonLanguage for test scripting
Appium-Python-ClientAppium’s official Python bindings
Pytest / UnittestTest runners
Android Studio or XcodeEmulator or real device

1. Install Python & Appium Libraries

pip install Appium-Python-Client pytest

2. Sample Test Script (Android – Python)

from appium import webdriver import unittest class AppiumTest(unittest.TestCase): def setUp(self): desired_caps = { "platformName": "Android", "deviceName": "emulator-5554", # or your device name "app": "/path/to/yourapp.apk", # replace with your APK path "automationName": "UiAutomator2" } self.driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps) def test_login_button_click(self): login_button = self.driver.find_element("id", "com.example:id/login") login_button.click() def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main()

3. Pytest Version (Alternative)

import pytest from appium import webdriver @pytest.fixture(scope="module") def driver(): caps = { "platformName": "Android", "deviceName": "emulator-5554", "app": "/path/to/yourapp.apk", "automationName": "UiAutomator2" } driver = webdriver.Remote("http://localhost:4723/wd/hub", caps) yield driver driver.quit() def test_login(driver): driver.find_element("id", "com.example:id/login").click()

4. Start Appium Server

  1. Install Appium via npm:

     
    npm install -g appium
  2. Start Appium server:

     
    appium
  3. Verify connection:

     
    adb devices

5. Locator Strategies

driver.find_element("id", "com.example:id/email") driver.find_element("accessibility id", "login_button") driver.find_element("xpath", "//android.widget.TextView[@text='Login']")

Optional Enhancements

  • Use Page Object Model for maintainability

  • Integrate with Behave for BDD-style testing

  • Generate reports using Allure or pytest-html

  • Support both Android and iOS with capability switching