SeleniumLab

Course1 · Selenium automation → Assertions & Structure

1 · Selenium automation

Organizing tests with pytest

12 min · Module 10

Use fixtures for driver setup/teardown so each test starts clean. Name tests after the behavior you verify.

Example / notes

# test_login.py
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By

@pytest.fixture
def driver():
    driver = webdriver.Chrome()
    yield driver
    driver.quit()

def test_valid_login(driver):
    driver.get("http://127.0.0.1:8000/lab/login/")
    driver.find_element(By.ID, "username").send_keys("demo")
    driver.find_element(By.ID, "password").send_keys("demo1234")
    driver.find_element(By.ID, "login-button").click()
    assert "/lab/welcome/" in driver.current_url
    assert driver.find_element(By.ID, "flash-success").is_displayed()

Practice in Lab: /lab/login/

Log in to track progress / take quizzes
← Assertions that catch regressions
Modules in this track