Python के साथ Selenium में ईमेल वेरिफ़िकेशन कैसे टेस्ट करें

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

  • Selenium
  • Python
  • ईमेल टेस्टिंग

Selenium आपका साइन अप फ़ॉर्म भर सकता है, लेकिन ज़्यादातर सूट "अपना ईमेल देखें" पर रुक जाते हैं। डिस्पोज़ेबल इनबॉक्स API के साथ आपका Python टेस्ट ईमेल पढ़कर काम पूरा कर सकता है।

स्टेप 1: इनबॉक्स helper

यह QuickInbox की फ़्री API इस्तेमाल करता है, जिसमें न साइन अप चाहिए न key। requests इंस्टॉल करें और जोड़ें:

# inbox.py - disposable inboxes from QuickInbox's free API (no key needed)
import re, time, requests

BASE = "https://quickinbox.net"

def create_inbox(minutes=10):
    r = requests.post(f"{BASE}/api/generate", json={"minutes": minutes})
    r.raise_for_status()
    address = r.json()["address"]
    return address, address.split("@")[0]

def wait_for_email(local, subject="", timeout=60, interval=2):
    deadline = time.time() + timeout
    while time.time() < deadline:
        r = requests.get(f"{BASE}/api/inbox/{local}")
        if r.ok:
            for m in r.json().get("messages", []):
                if subject in m["subject"]:
                    return m  # id, from, subject, text, html, receivedAt
        time.sleep(interval)
    raise TimeoutError(f"No email with subject {subject!r} in {timeout}s")

def extract_code(email):
    m = re.search(r"\b(\d{4,8})\b", email.get("text") or "")
    if not m:
        raise ValueError("No code found")
    return m.group(1)

def extract_link(email, pattern):
    for url in re.findall(r'href="([^"]+)"', email.get("html") or ""):
        url = url.replace("&amp;", "&")
        if re.search(pattern, url):
            return url
    raise ValueError(f"No link matching {pattern}")

स्टेप 2: वेरिफ़िकेशन लिंक टेस्ट करें

# test_signup.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import inbox

def test_signup_and_verify():
    address, local = inbox.create_inbox()
    driver = webdriver.Chrome()
    try:
        driver.get("https://staging.example.com/signup")
        driver.find_element(By.NAME, "email").send_keys(address)
        driver.find_element(By.NAME, "password").send_keys("Correct-Horse-42")
        driver.find_element(By.CSS_SELECTOR, "button[type=submit]").click()

        email = inbox.wait_for_email(local, subject="Verify")
        driver.get(inbox.extract_link(email, r"/verify"))

        WebDriverWait(driver, 10).until(
            EC.text_to_be_present_in_element((By.TAG_NAME, "body"), "verified"))
    finally:
        driver.quit()

स्टेप 3: या वन-टाइम कोड

email = inbox.wait_for_email(local, subject="code")
driver.find_element(By.NAME, "code").send_keys(inbox.extract_code(email))

टिप्स

  • हर टेस्ट को उसका अपना इनबॉक्स दें, ताकि टेस्ट pytest-xdist से पैरेलल चल सकें।
  • फ़्री API हर IP से हर मिनट लगभग 10 नए पते बनाने देती है। बड़े पैरेलल सूट के लिए Business API इस्तेमाल करें, जहाँ आपके namespace पर हर टैग बिना create कॉल के नया पता है।
  • फ़्री इनबॉक्स में कभी असली डेटा न भेजें: पता जानने वाला कोई भी उसे पढ़ सकता है।
  • SMS कोड के लिए भी यही तरीका: Selenium में SMS 2FA।

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

अभी आज़माएँ: ऊपर वाले फ़्री एंडपॉइंट को साइन अप की ज़रूरत नहीं है और उनकी जानकारी temporary email API पेज पर है। प्राइवेट इनबॉक्स, webhooks और बड़े पैमाने पर CI के लिए, QuickInbox Business API में हर महीने 5,000 ईमेल वाला फ़्री प्लान है।

← सभी लेख