Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 37 ยท Projects and Practice

Project: A Login Flow Test Suite

Build a complete Appium test suite for a login flow: project layout, config, screen objects, fixtures, data-driven tests, failure evidence and a runnable simulation.

In this project you build a small but complete Appium suite for the login flow of an imaginary shopping app. It brings together what the course has taught: capabilities from configuration, screen objects, a driver fixture, data-driven tests, waits, and evidence on failure. You will write the real Appium code (read-only here, because Appium and a device cannot run on this site) and a runnable simulation of the same app, so you can see the tests pass and fail.

The app, "Shop", has a login screen with an email field, a password field, a Log in button and an error label, and a home screen titled "Welcome". Accessibility IDs: email, password, login-button, error-label, home-title.

Requirements

  1. Valid credentials lead to the home screen.
  2. An empty email shows "Email is required".
  3. An empty password shows "Password is required".
  4. Wrong credentials show "Wrong email or password".
  5. After five wrong attempts the button is disabled and the message says "Too many attempts. Try again later."

Project layout

TEXT
shop-tests/
  config.py
  conftest.py
  pytest.ini
  requirements.txt
  screens/
    __init__.py
    login_screen.py
    home_screen.py
  tests/
    test_login.py
  artifacts/            (created at run time)

requirements.txt:

TEXT
Appium-Python-Client>=5
pytest>=8

pytest.ini:

INI
[pytest]
markers =
    smoke: quick checks run on every pull request
addopts = -ra

config.py

Python
import os
from appium.options.android import UiAutomator2Options

APPIUM_URL = os.environ.get("APPIUM_URL", "http://127.0.0.1:4723")
APP_PATH = os.environ.get("APP_PATH", "builds/shop-debug.apk")
TEST_USER = os.environ.get("TEST_USER", "[email protected]")
TEST_PASSWORD = os.environ["TEST_PASSWORD"]     # no default: secrets are never in code


def build_options():
    options = UiAutomator2Options()
    options.platform_name = "Android"
    options.app = APP_PATH
    options.auto_grant_permissions = True
    options.new_command_timeout = 120
    return options

Screen objects

Python
# screens/login_screen.py
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from screens.home_screen import HomeScreen


class LoginScreen:
    EMAIL = (AppiumBy.ACCESSIBILITY_ID, "email")
    PASSWORD = (AppiumBy.ACCESSIBILITY_ID, "password")
    SUBMIT = (AppiumBy.ACCESSIBILITY_ID, "login-button")
    ERROR = (AppiumBy.ACCESSIBILITY_ID, "error-label")

    def __init__(self, driver):
        self.driver = driver
        self.wait = WebDriverWait(driver, 15)
        self.wait.until(EC.visibility_of_element_located(self.SUBMIT))

    def enter(self, email, password):
        email_field = self.driver.find_element(*self.EMAIL)
        email_field.clear()
        email_field.send_keys(email)
        password_field = self.driver.find_element(*self.PASSWORD)
        password_field.clear()
        password_field.send_keys(password)
        return self

    def submit(self):
        self.driver.find_element(*self.SUBMIT).click()
        return self

    def log_in(self, email, password):
        return self.enter(email, password).submit()

    def log_in_successfully(self, email, password):
        self.log_in(email, password)
        return HomeScreen(self.driver)

    def error_message(self):
        label = self.wait.until(EC.visibility_of_element_located(self.ERROR))
        return label.text

    def is_submit_enabled(self):
        return self.driver.find_element(*self.SUBMIT).is_enabled()
Python
# screens/home_screen.py
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class HomeScreen:
    TITLE = (AppiumBy.ACCESSIBILITY_ID, "home-title")

    def __init__(self, driver):
        self.driver = driver
        WebDriverWait(driver, 15).until(EC.visibility_of_element_located(self.TITLE))

    def title(self):
        return self.driver.find_element(*self.TITLE).text

Notice that each constructor waits for its own signature element; a test can never interact with a screen that has not loaded.

conftest.py with evidence on failure

Python
import pathlib
import pytest
from appium import webdriver

import config

ARTIFACTS = pathlib.Path("artifacts")


@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    setattr(item, "rep_" + call.when, outcome.get_result())


@pytest.fixture
def driver(request):
    ARTIFACTS.mkdir(exist_ok=True)
    drv = webdriver.Remote(config.APPIUM_URL, options=config.build_options())
    yield drv
    report = getattr(request.node, "rep_call", None)
    if report is not None and report.failed:
        name = request.node.name.replace("[", "_").replace("]", "")
        drv.get_screenshot_as_file(str(ARTIFACTS / f"{name}.png"))
        (ARTIFACTS / f"{name}.xml").write_text(drv.page_source, encoding="utf-8")
    drv.quit()


@pytest.fixture
def login(driver):
    from screens.login_screen import LoginScreen
    return LoginScreen(driver)

tests/test_login.py

Python
import pytest
import config
from screens.home_screen import HomeScreen


@pytest.mark.smoke
def test_valid_login_opens_home(login):
    home = login.log_in_successfully(config.TEST_USER, config.TEST_PASSWORD)
    assert home.title() == "Welcome"


@pytest.mark.parametrize("email,password,message", [
    ("", "secret", "Email is required"),
    ("[email protected]", "", "Password is required"),
    ("[email protected]", "wrong-password", "Wrong email or password"),
], ids=["no-email", "no-password", "bad-password"])
def test_login_validation(login, email, password, message):
    login.log_in(email, password)
    assert login.error_message() == message


def test_lockout_after_five_wrong_attempts(login):
    for _ in range(5):
        login.log_in("[email protected]", "wrong-password")
    assert not login.is_submit_enabled()
    assert login.error_message() == "Too many attempts. Try again later."

Run it with TEST_PASSWORD=... pytest -m smoke for the quick set, or pytest for everything.

A concern for the lockout test: it changes server-side state (a locked account). Use a dedicated account created for the test (through an API call in a fixture) or a backend that resets lockouts, never the shared QA account.

Runnable simulation

The next block simulates the Shop app and replays the same five scenarios, so you can practice reading the results. It is a stand-in for the app and the driver, not Appium.

Python Shop login simulation (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Extensions to try

  • Add a "show password" toggle test using get_attribute("password") on Android.
  • Run the same suite on iOS by switching the options class and checking whether accessibility IDs match.
  • Add --platform as a pytest option (see Test Runners and Fixtures).
  • Create the locked-out user with an API call in a fixture, so the test never depends on shared data.
  • Add a test that backgrounds the app after typing the email and checks that the field is still filled after returning.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. Why does each screen constructor wait for a signature element?

    The wait synchronizes the test with the app at every screen transition.
  2. 2. Why is the lockout test risky with a shared account?

    Use a dedicated or API-created account for tests that change account state.
  3. 3. Where does the test password come from in this project?

    Secrets are read from the environment so they never enter source control.

Summary

  • A clean layout separates config, screen objects, fixtures and tests.
  • Screen constructors wait for their signature element; tests read like user stories.
  • Parametrization covers input variations; one fixture handles the session and the failure evidence.
  • Tests that change server state need isolated accounts.

Next: Project: A Shopping Cart Test Suite