Appium with Python
- Home
- Appium with Python
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
Tool | Purpose |
---|---|
Appium | Mobile automation engine |
Python | Language for test scripting |
Appium-Python-Client | Appium’s official Python bindings |
Pytest / Unittest | Test runners |
Android Studio or Xcode | Emulator 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
Install Appium via npm:
npm install -g appium
Start Appium server:
appium
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