Appium से मोबाइल ऐप में फ़ोन वेरिफ़िकेशन कैसे टेस्ट करें

· 5 मिनट में पढ़ें · QuickInbox टीम

  • Appium
  • मोबाइल
  • SMS

मोबाइल ऐप अक्सर "अपना फ़ोन नंबर डालें" से शुरू होते हैं। सिम्युलेटर या डिवाइस फ़ार्म पर कोड पाने के लिए कोई SIM कार्ड नहीं होता। API से पढ़ा जा सकने वाला टेस्ट नंबर इसका हल है।

डिवाइस का अपना नंबर क्यों नहीं?

सिम्युलेटर और एम्युलेटर के पास फ़ोन नंबर नहीं होता। फ़ार्म के असली डिवाइस शेयर होते हैं और आप अपने टेस्ट से उनके मैसेज नहीं पढ़ सकते। पढ़ भी सकें, तो टेस्ट को एक फ़िज़िकल फ़ोन से बाँधना उसे नाज़ुक बनाता है। क्लाउड टेस्ट नंबर हर डिवाइस से एक जैसा काम करता है।

स्टेप 1: 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")

स्टेप 2: Appium टेस्ट (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()

iOS के लिए इसकी जगह XCUITestOptions इस्तेमाल करें; SMS वाला हिस्सा वही रहता है।

मोबाइल के लिए टिप्स

  • ऑटोफ़िल: iOS और Android डिवाइस के अपने मैसेज से कोड सुझा सकते हैं। यहाँ ऐसा नहीं होगा, इसलिए हाथ से डालने वाला रास्ता टेस्ट करें, जिस पर हर यूज़र लौट सकता है।
  • देश चुनना: नंबर US का है, इसलिए अपने ऐप के हिसाब से +1 चुनें या बिना कंट्री कोड के नंबर टाइप करें।
  • Accessibility id: अपने इनपुट को स्थिर id दें; ये टेक्स्ट selector से कहीं कम flaky होते हैं।

इससे जुड़ी गाइड

टेस्ट नंबर पाएँ: QuickInbox के हर पेड प्लान में एक असली US फ़ोन नंबर और हर महीने SMS की तय मात्रा शामिल है, प्लान $9 प्रति माह से। अतिरिक्त पैक $5 प्रति माह से। देखें SMS टेस्टिंग और प्राइसिंग।

← सभी लेख