Mobile: emulated & real devices
Mobile web versus native, Appium sessions and locators, gestures and device-only scenarios.
Mobile testing is three different problems
Emulated mobile web, real-device mobile web and native apps have different costs, tools and findings.
Three distinct activities get called mobile testing, and conflating them is the most common mistake in this area. Emulated mobile web runs your responsive site at a phone viewport with a mobile user agent and touch enabled; it costs about a second per test and finds layout, touch-target, overflow and breakpoint defects, which is the majority of mobile web problems. Real-device mobile web drives an actual browser on actual hardware through a device automation server; it costs tens of seconds per test and finds what emulation structurally cannot, because emulation is desktop rendering wearing a phone-shaped viewport. Native app testing drives an installed application through a platform driver and is the only way to reach anything that is genuinely part of the app. The strategy that works runs emulation on every commit, real devices nightly, and native smoke tests nightly or at release. Most teams invert this, build an expensive device suite first, and end up with a slow flaky job nobody runs.
Key points
- Emulation is fast and finds layout, touch-target, overflow and breakpoint defects
- Real devices find engine bugs, real keyboard and viewport behaviour, and true performance
- Run emulation every commit and real devices nightly, not the other way round
Code
Run the code to see the result.
Write the limitations into the test plan; a stakeholder who reads "mobile covered" will assume more than is true.
See it in the framework
- Implementation · Mobile driver factory:
framework/mobile/driver_factory.py— live · reviewed - Used by · Mobile web (emulated):
tests/web/test_mobile_web.py— live · reviewed - Used by · Native mobile:
tests/mobile/test_native_app.py— live · reviewed
Reviewed against commit f9ada16 (2026-08-14).
Common pitfalls
- Claiming iOS coverage from emulation — emulated mobile web is desktop Chromium, not WebKit
- Building the device suite first, so the pipeline is slow and flaky before it has found anything
Practice exercise
For a product you know, write a one-page mobile plan that states which of the three activities you would run, at what cadence, and — explicitly — what each cannot find.
Emulated mobile web: the checks worth writing first
Breakpoints, horizontal overflow, touch-target size, readable text and one full journey per device.
Device descriptors set viewport, device pixel ratio, user agent and touch support together, which is far more reliable than guessing viewport numbers by hand. Parametrising the fixture rather than each test means adding a device to the matrix is a one-line change that immediately covers the whole file. Five checks give most of the value. First, that the responsive breakpoint actually fires at real device widths. Second, that the page does not scroll horizontally, which is the signature mobile defect, usually caused by one element wider than the viewport and completely invisible on a desktop screen. Third, that interactive controls meet a minimum touch-target size. Get the number right before you write the assertion: WCAG 2.2's actual Level AA minimum (2.5.8 Target Size Minimum) is 24x24 CSS pixels, with exceptions; 44x44 is the enhanced Level AAA target (2.5.5), which is where the widely quoted "44px" figure actually comes from, and which platform guidelines (Apple's Human Interface Guidelines, Android's Material Design) commonly adopt as their own baseline. Test against whatever number your product has committed to, and cite it correctly — either way, a control that sets a minimum height but no minimum width will quietly fail it. Fourth, that text is large enough to read without zooming. Fifth, one complete journey, using the same page objects as the desktop suite.
Key points
- Use device descriptors instead of hand-picked viewport numbers, and parametrise the fixture not the tests
- Horizontal overflow is the signature mobile defect and is invisible on a desktop screen
- Touch targets need a minimum width as well as a minimum height — that asymmetry hides real failures
Code
@pytest.fixture(params=["iPhone 15", "Pixel 7", "Galaxy S9+", "iPad (gen 7)"], ids=lambda d: d)
def mobile_page(request, playwright, browser, base_url, storage_state):
descriptor = playwright.devices[request.param]
context = browser.new_context(**descriptor, base_url=base_url, storage_state=storage_state)
page = context.new_page()
yield page
context.close()
def test_no_horizontal_scrolling(mobile_page):
App(mobile_page).products.open()
overflow = mobile_page.evaluate(
"() => document.documentElement.scrollWidth - document.documentElement.clientWidth"
)
assert overflow <= 1, f"Page scrolls horizontally by {overflow}px on this device"
def test_touch_targets_meet_the_minimum_size(mobile_page):
products = App(mobile_page).products.open()
if mobile_page.viewport_size["width"] > 640:
pytest.skip("Desktop-width layout - touch sizing does not apply")
box = products.page.locator("[data-testid=menu-toggle]").bounding_box()
# 44px is this product's chosen baseline (platform HIG / WCAG 2.5.5 AAA
# enhanced target), not the WCAG AA minimum -- that's 24px (2.5.8).
assert box["width"] >= 44 and box["height"] >= 44, f"Touch target is {box}"This exact test found a real 40x46 pixel hamburger button — min-height was set, min-width was not.
See it in the framework
- Used by · Mobile web checks:
tests/web/test_mobile_web.py— live · reviewed - Used by · Accessibility/visual checks:
tests/web/test_accessibility_and_visual.py— live · reviewed
Reviewed against commit f9ada16 (2026-08-14).
Common pitfalls
- Duplicating the desktop page objects for mobile when only one collapsed menu actually differs
- Including a tablet in the matrix and then asserting mobile-only behaviour, so the test fails on layout rather than on a defect
Practice exercise
Add the overflow check to a responsive site you work with and run it across four device descriptors. If it passes, add a wide table or a long unbroken string and confirm the check catches it.
Appium sessions, capabilities and driver factories
Typed options objects replaced capability dictionaries; a factory hides whether you run locally or on a farm.
Most Appium material online describes a version that is several major releases out of date, so it is worth knowing what changed. Capability dictionaries have been replaced by typed options objects per platform, which serialise to the standard protocol format and catch a misspelt capability at construction rather than at session start. Vendor-specific capabilities need an explicit namespace prefix, and in the current major version the prefixes for server security features are mandatory rather than optional. Drivers are installed separately from the server, which still surprises people. The old touch-action API is gone, replaced by platform gesture commands executed as scripts, and those run natively on the device and are far more reliable than hand-assembled pointer sequences. Wrap all of this in a driver factory so a test never knows whether it is running against a local emulator or a device cloud — that indifference is the entire reason the factory exists.
Key points
- Typed options objects replaced capability dictionaries and catch typos at construction time
- Drivers install separately from the server; vendor capabilities need an explicit namespace prefix
- Use platform gesture commands rather than the removed touch-action API
Code
from appium.options.android import UiAutomator2Options
def create_driver(*, platform=Platform.ANDROID, browser=None, app=None, build="local"):
opts = UiAutomator2Options()
opts.platform_name = "Android"
opts.device_name = settings.mobile.device_name
opts.automation_name = "UiAutomator2"
if browser: # mobile web on a real device
opts.set_capability("browserName", "Chrome")
opts.set_capability("appium:chromedriverAutodownload", True)
else: # native app
opts.app = app
opts.set_capability("appium:autoGrantPermissions", True)
opts.set_capability("appium:disableWindowAnimation", True)
if settings.mobile.cloud_user: # device farm, same call site
opts.set_capability("bstack:options", {"buildName": build, ...})
driver = webdriver.Remote(settings.mobile.appium_url, options=opts)
driver.implicitly_wait(0) # explicit waits only; never mix the two
return driverOne factory, three session shapes: native app, real-device mobile web, and local or cloud execution.
See it in the framework
- Implementation · Driver factory:
framework/mobile/driver_factory.py— live · reviewed - Used by · Mobile conftest:
tests/mobile/conftest.py— live · reviewed
Reviewed against commit f9ada16 (2026-08-14).
Common pitfalls
- Following a tutorial written for an old major version, then debugging capability errors for an afternoon
- Hardcoding cloud credentials in the test rather than the factory, which couples every test to where it runs
Practice exercise
Install the current Appium server and one platform driver, then start a session against an emulator using a typed options object. Deliberately misspell a capability and observe when the error appears.
Mobile locators, waits and device-only scenarios
Accessibility ids first, XPath as a last resort; one driver per test; automate what a browser can't reach.
Locator choice matters more on mobile than on the web because every query serialises the view hierarchy across a network connection, so a structural path is not merely brittle but genuinely slow. Accessibility identifiers come first: when an app actually exposes the same identifier on both platforms, they map to a platform attribute on both systems and give one locator that works everywhere — but that cross-platform reuse is a property of how the app was built, not something Appium guarantees for you, so confirm the id is really there on both before assuming it. Asking developers to add them is still the highest-return request a mobile tester can make. Platform-specific identifiers come next, then the native selector engines, and structural paths (XPath) last — they work, and are sometimes the only option on a screen with no other hooks, but every extra hop the driver has to serialise makes them the slowest and most brittle choice, so reach for one only once the cheaper options are unavailable. On waiting, set the implicit wait to zero and never touch it again — mixing implicit and explicit waiting produces compounding timeouts that are documented nowhere. Give each test its own session: a mobile session is the least stable resource in the stack, and one crash or system dialog poisons every later test that shares it. Finally, spend Appium session time on what a browser can't reach at all — permissions, backgrounding, rotation, hardware navigation, connectivity loss — and push business rules down to the service layer; an emulator or simulator can exercise most of these just as well as physical hardware, so "needs an app session" and "needs a physical device" are different, narrower claims.
Key points
- Accessibility identifiers give one locator for both platforms when the app actually exposes matching ids; XPath still works as a last resort, but every hop is serialised over the wire, making it the slowest and most brittle choice here
- Set implicit wait to zero and use explicit waits only — mixing the two compounds unpredictably
- One driver session per test, and capture diagnostics before quitting, not after
Code
@pytest.fixture
def driver(request):
session = create_driver()
try:
yield session
finally:
# Capture BEFORE quitting: a closed session cannot produce a screenshot.
report = getattr(request.node, "rep_call", None)
if report is not None and report.failed:
attach_png("failure-screenshot", session.get_screenshot_as_png())
attach_text("page-source", session.page_source[:200_000])
session.quit()
def test_cart_survives_backgrounding(products_screen):
"""A device-only scenario: this is where mobile apps lose state and crash."""
products_screen.add_to_cart(products_screen.product_names[0])
products_screen.driver.background_app(5)
assert products_screen.cart_count == 1, "Cart was lost when the app was backgrounded"Backgrounding, rotation, permissions and the hardware back button have no web equivalent — automate those.
See it in the framework
- Implementation · Screen object base:
framework/mobile/base_screen.py— live · reviewed - Used by · Concrete screen objects:
framework/mobile/screens.py— live · reviewed - Used by · Native app tests:
tests/mobile/test_native_app.py— live · reviewed
Reviewed against commit f9ada16 (2026-08-14).
Common pitfalls
- Calling quit before capturing a screenshot and page source, which is why mobile failures so often arrive with no evidence
- Automating validation tables on a device, where they take minutes instead of the milliseconds they cost at the API layer
Practice exercise
List every mobile test in a suite you know and mark each as device-only or pushable to the API layer. Move one of the pushable ones down and compare the runtimes.