How to test phone verification in mobile apps with Appium
Mobile apps often start with "Enter your phone number". On a simulator or a device farm there's no SIM card to receive the code. A test number you can read through an API solves it.
Why not use the device's own number?
Simulators and emulators have no phone number. Real devices in a farm are shared and you can't read their texts from your test. Even when you can, tying a test to one physical phone makes it fragile. A cloud test number works the same from any device.
Step 1: the helper
# sms.py - read texts sent to your QuickInbox test number
import os, re, time, requests
BASE = "https://business.quickinbox.net"
HEADERS = {"X-API-Key": os.environ["QI_KEY"]}
def get_number():
numbers = requests.get(f"{BASE}/api/business/sms/numbers", headers=HEADERS).json()["numbers"]
return numbers[0] # {"id": ..., "phone_number": "+1...", ...}
def list_messages(number_id):
r = requests.get(f"{BASE}/api/business/sms/numbers/{number_id}/messages", headers=HEADERS)
r.raise_for_status()
return r.json()["messages"] # newest first
def snapshot(number_id):
return {m["id"] for m in list_messages(number_id)}
def wait_for_code(number_id, seen, timeout=90, pattern=r"\b(\d{4,8})\b"):
deadline = time.time() + timeout
while time.time() < deadline:
for m in list_messages(number_id):
if m["id"] not in seen and re.search(pattern, m["body"]):
return re.search(pattern, m["body"]).group(1)
time.sleep(3)
raise TimeoutError("No new SMS code arrived in time")Step 2: the Appium test (Python)
# test_onboarding.py
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy
import sms
def test_phone_onboarding():
number = sms.get_number()
seen = sms.snapshot(number["id"])
opts = UiAutomator2Options()
opts.app = "/path/to/app-debug.apk"
driver = webdriver.Remote("http://127.0.0.1:4723", options=opts)
try:
local = number["phone_number"].removeprefix("+1")
driver.find_element(AppiumBy.ACCESSIBILITY_ID, "phone-input").send_keys(local)
driver.find_element(AppiumBy.ACCESSIBILITY_ID, "send-code").click()
code = sms.wait_for_code(number["id"], seen)
driver.find_element(AppiumBy.ACCESSIBILITY_ID, "code-input").send_keys(code)
driver.find_element(AppiumBy.ACCESSIBILITY_ID, "verify").click()
assert driver.find_element(AppiumBy.ACCESSIBILITY_ID, "home-screen")
finally:
driver.quit()For iOS use XCUITestOptions instead; the SMS part stays the same.
Tips for mobile
- Autofill: iOS and Android can suggest codes from the device's own messages. That won't happen here, so test the manual entry path, which every user can fall back to.
- Country picker: the number is a US number, so select +1 or type the number without the country code, as your app expects.
- Accessibility ids: give your inputs stable ids; they make tests much less flaky than text selectors.
Related guides
- A US phone number for SMS testing
- Why public "receive SMS online" sites break your tests (and what to use instead)
- How to test SMS 2FA login in CI with GitHub Actions
- SMS verification API for testing OTP codes
- SMS testing overview
Get a test number: every paid QuickInbox plan includes a real US phone number and a monthly SMS allowance, with plans from $9 a month. Extra packs start at $5 a month. See SMS testing and pricing.