Frontend tests verify what the user actually experiences: every page loads, every control works and each critical flow survives a change. I keep two reference frameworks side by side in a public repository, Playwright + pytest and Selenium 4 + pytest. They follow the same principles and run against a small bundled store (the demo app), so they are deterministic, need no credentials and work for anyone who clones the repo.
In short
- Two public frameworks built on the same principles and run against a bundled demo app, so anyone can run them.
- Page Objects with intent-level methods, stable
data-testidlocators and no sleeps. - Evidence kept on failure, and a daily run in CI on Chromium, Chrome and Firefox.
2
public frameworks
4
browser runs in CI
14:00
UTC, daily run
| Framework | What it demonstrates | Browsers in CI |
|---|---|---|
| Playwright + pytest | Page Objects, web-first assertions, shared login state, isolated contexts, API checks, network interception, trace on failure | Chromium, Firefox |
| Selenium 4 + pytest | Page Objects, explicit waits only, fixtures, screenshot and page source on failure | Chrome, Firefox |
| Behave + Gherkin | Behaviour-driven scenarios shared with business (earlier work, kept as a record) | n/a |
Principles shared by both frameworks
- Page Objects with intent-level methods.
login(),add()andcheckout()describe what the user does, and tests never touch the driver. A UI change is fixed in one class, not in every test. - Stable locators.
data-testidattributes instead of absolute XPath, so layout and style changes do not break tests. - No sleeps. Playwright’s web-first assertions and Selenium’s explicit waits retry until the condition holds: faster when the app is fast, stable when it is slow.
- Independent tests. A fresh browser or isolated context per test, so tests run in any order or in parallel.
- Evidence on failure, nothing more. A red run can be diagnosed without re-running it, and green runs keep artifacts small.
- Fast subset and full run. Markers
smokeandregressionpick one or the other. Settings come from environment variables, andBASE_URLpoints the same suite at any deployment.
Playwright with pytest
| Decision | Why |
|---|---|
Log in once, reuse storage_state | Tests that are not about login start authenticated: faster and less flaky |
| One isolated browser context per test | No shared cookies or cart; any order, any parallelism |
get_by_test_id locators inside Page Objects | Locators survive layout changes and tests read as user intent |
Playwright’s APIRequestContext for API checks | UI and API tests share one tool, one config and one report |
page.route to fake a server error | Failure paths are tested without breaking a real backend |
| Trace, screenshot and video kept only on failure | Open the trace with playwright show-trace and replay the failure step by step |
Log in once, start every test authenticated
@pytest.fixture(scope="session")
def auth_state(browser, base_url, tmp_path_factory) -> str:
"""Log in once and save cookies and storage; later contexts start already signed in."""
path = tmp_path_factory.mktemp("auth") / "state.json"
context = browser.new_context(base_url=base_url)
page = context.new_page()
LoginPage(page).open().login(SETTINGS.user, SETTINGS.password)
expect(page).to_have_url(re.compile(r"/inventory$"))
context.storage_state(path=str(path))
context.close()
return str(path)
UI and API in the same framework
One test checks that the screen shows exactly what the API returns. Another intercepts the add-to-cart request, answers with a 500 and verifies that the user sees the failure:
@pytest.mark.api
@pytest.mark.regression
def test_ui_lists_exactly_what_the_api_returns(api, inventory):
expected = sorted(p["name"] for p in api.get("/api/products").json())
assert inventory.product_names() == expected
@pytest.mark.regression
def test_network_failure_on_add_to_cart_is_visible_to_the_user(auth_page, inventory):
auth_page.route("**/cart/add/*", lambda route: route.fulfill(status=500, body="Service unavailable"))
auth_page.get_by_test_id("add-1").click()
expect(auth_page.locator("body")).to_have_text("Service unavailable")
Selenium 4 with pytest
Selenium is still the standard in many teams, so the reference framework shows how to keep it maintainable: a base page that routes every interaction through an explicit wait, one Page Object class per page, validation cases written once and parametrised, and Selenium Manager instead of driver paths on one machine. On failure it saves a screenshot, the page source and the URL, and embeds them in the HTML report.
def testid(value):
return By.CSS_SELECTOR, f'[data-testid="{value}"]'
class BasePage:
def __init__(self, driver, base_url, timeout=10):
self.driver = driver
self.base_url = base_url.rstrip("/")
self.wait = WebDriverWait(driver, timeout)
def click(self, locator):
self.wait.until(EC.element_to_be_clickable(locator)).click()
def type(self, locator, text):
element = self.wait.until(EC.visibility_of_element_located(locator))
element.clear()
element.send_keys(text)
Run it yourself
cd Playwright-Pytest/framework
pip install -r requirements.txt
playwright install --with-deps chromium firefox
pytest -m smoke # fast subset
pytest --browser firefox # another browser
Selenium works the same way: pytest --selenium-browser firefox, or edge on a machine that has it.
Running unattended
A GitHub Actions workflow runs Selenium on Chrome and Firefox and Playwright on Chromium and Firefox on every change to the frameworks and every day at 14:00 UTC. Reports and failure evidence are uploaded as artifacts. See CI/CD pipelines.
AI-assisted, human-owned
I use Claude Code as a day-to-day support tool to draft and review tests, and the setup is versioned in the repository so it can be inspected: project rules that fix these conventions (Page Objects, data-testid, no sleeps), the Playwright MCP server to explore a page and find stable locators, reusable skills for new tests and Gherkin scenarios, hooks that block secrets from being written, and read-only reviewer subagents that check every change. I define the scope, review the evidence of each run and make the commits myself. The details are in How I work with AI. For testing the AI itself, see LLM evaluation.
BDD with Behave and Gherkin
When the business needs to read the tests, I write them in Gherkin and run them with Behave: the scenario is the documentation and also an executable test. These suites come from earlier work against private environments and are kept in the repository as a record.
Feature: User who can log in successfully
If the user has the correct ID and password, after clicking on log in,
they reach the client area.
Scenario: User with valid credentials logs in successfully
Given User with a valid ID and Pass
When Click on LogIn
Then Successfully log in to the client area
Source code: Playwright framework, Selenium framework, demo app and the earlier suites. These run in CI/CD, with latency checks from performance testing.
