27 / 66 · 09 Mobile & Cross-Platform Testing · Appium Fundamentals← prev⊞ allnext →☰ Read as one page
5.3Writing Appium Tests
Python Example: Login Flow
# tests/mobile/test_login_flow.py
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy
import pytest
@pytest.fixture
def driver():
options = UiAutomator2Options()
options.platform_name = "Android"
options.device_name = "Pixel 7"
options.app = "./builds/app-release.apk"
options.automation_name = "UiAutomator2"
options.no_reset = False # Clean state for each test
driver = webdriver.Remote(
command_executor="http://localhost:4723",
options=options,
)
yield driver
driver.quit()
def test_login_with_valid_credentials(driver):
# Wait for splash screen to finish
driver.implicitly_wait(10)
# Enter credentials
email_field = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "email-input")
email_field.send_keys("test@example.com")
password_field = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "password-input")
password_field.send_keys("secureP@ss123")
# Tap login button
login_btn = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "login-button")
login_btn.click()
# Verify navigation to dashboard
dashboard = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "dashboard-screen")
assert dashboard.is_displayed()
def test_login_with_invalid_credentials(driver):
driver.implicitly_wait(10)
email_field = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "email-input")
email_field.send_keys("test@example.com")
password_field = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "password-input")
password_field.send_keys("wrongpassword")
login_btn = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "login-button")
login_btn.click()
# Verify error message is shown
error_msg = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "login-error")
assert error_msg.is_displayed()
assert "Invalid" in error_msg.text
def test_biometric_login_prompt(driver):
"""Verify that biometric authentication is offered when available."""
driver.implicitly_wait(10)
biometric_btn = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "biometric-login")
# Check if biometric is available on this device
is_biometric_available = driver.execute_script(
"mobile: isBiometricEnrolled"
)
if is_biometric_available:
assert biometric_btn.is_displayed()
biometric_btn.click()
# Simulate successful fingerprint
driver.execute_script("mobile: fingerprint", {"fingerprintId": 1})
dashboard = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "dashboard-screen")
assert dashboard.is_displayed()
else:
# Biometric button should be hidden or disabled
assert not biometric_btn.is_enabled()