from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
caps = {
"platformName": "Android",
"deviceName": "Android Emulator",
"appPackage": "com.example.mobileapp",
"appActivity": ".MainActivity",
"automationName": "UiAutomator2"
}
# Connect to Appium server
driver = webdriver.Remote('http://localhost:4723/wd/hub', caps)
try:
wait = WebDriverWait(driver, 10)
# Wait for username field and enter username
username_field = wait.until(EC.presence_of_element_located((AppiumBy.ACCESSIBILITY_ID, "username_input")))
username_field.send_keys("testuser")
# Enter password
password_field = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "password_input")
password_field.send_keys("Test@1234")
# Tap login button
login_button = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "login_button")
login_button.click()
# Wait for home screen element
home_screen_element = wait.until(EC.presence_of_element_located((AppiumBy.ACCESSIBILITY_ID, "home_screen")))
# Assert home screen is displayed
assert home_screen_element.is_displayed(), "Home screen is not displayed after login"
# Assert no error message
error_elements = driver.find_elements(AppiumBy.ACCESSIBILITY_ID, "login_error")
assert len(error_elements) == 0, "Error message displayed after login"
finally:
driver.quit()This script uses Appium with Python to automate the mobile login test.
We set capabilities to connect to an Android emulator and specify the app package and activity.
We use explicit waits to wait for the username field to appear before interacting.
We locate elements by accessibility id, which is a best practice for mobile apps.
After entering credentials and tapping login, we wait for the home screen element to confirm successful login.
Assertions check that the home screen is visible and no error message is shown.
Finally, the driver quits to close the session.