Skip to content

UI & Testing Automation

5. UI Automation & E-Commerce Testing

This section covers how to control a live desktop web browser automatically to fill out forms, click buttons, and run automated quality assurance (QA) tests.

Tools & Libraries

  • selenium (v4+): A foundational tool that acts as a robot driver to open a real desktop browser and click around exactly like a human user.
  • webdriver-manager: A helper package that automatically downloads and updates the background browser binaries (like Chrome drivers).

    💡 Pro-Tip: If you are using modern Selenium (v4.6 or higher), this feature is now built directly into Selenium natively, meaning you often do not need to install this extra tool anymore!


Industry Upgrades (What Teams Use at Scale)

While Selenium is an industry classic, enterprise testing suites frequently upgrade to modern tools optimized for speed and resilience against network lag.

1. High-Speed Headless Testing: Playwright (by Microsoft)

Built for extreme speed and reliability, Playwright executes browser actions natively, runs multiple isolated sessions simultaneously, and automatically waits for web elements to load before executing commands. Is less prone to sudden test crashes.

  • How it looks in code:
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    # Launches a fast background (headless) browser instance
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto("[https://example-store.com](https://example-store.com)")
    print(f"Page Title: {page.title()}")
    browser.close()

2. Frontend-First Ecosystem: Cypress

Unlike Selenium or Playwright, which run outside the browser sending remote commands, Cypress executes its automated operations directly inside the browser window alongside your app's frontend code. Note that Cypress is built entirely on JavaScript/TypeScript, making it a favorite for frontend developer QA teams.


Core Methods & Code Example

  • webdriver.Chrome(): Launches a fresh instance of a Google Chrome browser controlled entirely by your Python script.
  • driver.find_element(By.ID / By.XPATH): Searches through the live browser window using unique code fingerprints (IDs or XPath structural paths) to target specific buttons, input boxes, or menus.
  • .send_keys('text') & .click(): Types text directly into input fields and physically clicks on buttons inside the live web browser.
  • WebDriverWait(driver, timeout).until(): Tells the script to wait until elements are fully loaded on the screen. This stops your script from crashing if your internet or the website is running slow.
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

# 1. Spawn a background browser process controlled by script code
driver = webdriver.Chrome()

# 2. Navigate to your target application page
driver.get("[https://www.example-store.com/login](https://www.example-store.com/login)")

try:
    # 3. Set up a timing loop to safely wait up to 10 seconds for the element to appear
    username_field = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "user-input-box"))
    )

    # 4. Fire physical text keyboard strokes 
    username_field.send_keys("test_user_account")

    # 5. Track down the submission button using structural XPath maps and click it
    driver.find_element(By.XPATH, "//button[@type='submit']").click()

finally:
    # 6. Always shut down the browser window to clear up computer memory resources
    driver.quit()