Files
main-site/e2e/test_cookie_consent.py
codex_a 9d323d2040
Some checks failed
CI / nightly-e2e (pull_request) Has been skipped
CI / ci (pull_request) Successful in 1m22s
CI / pr-e2e (pull_request) Failing after 3m28s
feat: add comprehensive Playwright E2E test suite
- Create e2e/ directory with 7 test modules covering:
  - Home page: title, nav links, theme toggle, newsletter form
  - Cookie consent: accept all, reject all, granular prefs, persistence
  - Article index: loads, tag filter, click-through navigation
  - Article detail: title/read-time, share section, comments, newsletter aside, related
  - Comments: valid submit → redirect, empty body → error display, disabled check
  - Newsletter: JS confirmation message, invalid email error, aside form, duplicate
  - Feeds: RSS/sitemap/robots.txt validity, tag feed, seeded content present
- Extend seed_e2e_content management command with tagged article, about page,
  no-comments article, and legal pages for richer test coverage
- Add seed command tests (create + idempotency) to keep coverage ≥ 90%
- Add pr-e2e CI job (runs on pull_request): builds image, starts postgres + app,
  installs playwright, runs pytest e2e/
- Update nightly-e2e to run full e2e/ suite alongside legacy journey test
- Add --ignore=e2e to unit-test pytest step (coverage must not include browser tests)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-02-28 19:30:43 +00:00

73 lines
2.5 KiB
Python

"""E2E tests for the cookie consent banner."""
from __future__ import annotations
import pytest
from playwright.sync_api import Page, expect
def _open_fresh_page(page: Page, url: str) -> None:
"""Navigate to URL with no existing consent cookie (fresh context guarantees this)."""
page.goto(url, wait_until="networkidle")
@pytest.mark.e2e
def test_banner_visible_on_first_visit(page: Page, base_url: str) -> None:
_open_fresh_page(page, f"{base_url}/")
expect(page.locator("#cookie-banner")).to_be_visible()
@pytest.mark.e2e
def test_accept_all_dismisses_banner(page: Page, base_url: str) -> None:
_open_fresh_page(page, f"{base_url}/")
banner = page.locator("#cookie-banner")
expect(banner).to_be_visible()
page.get_by_role("button", name="Accept all").first.click()
page.wait_for_load_state("networkidle")
expect(banner).to_have_count(0)
@pytest.mark.e2e
def test_reject_all_dismisses_banner(page: Page, base_url: str) -> None:
_open_fresh_page(page, f"{base_url}/")
banner = page.locator("#cookie-banner")
expect(banner).to_be_visible()
page.get_by_role("button", name="Reject all").first.click()
page.wait_for_load_state("networkidle")
expect(banner).to_have_count(0)
@pytest.mark.e2e
def test_granular_preferences_save_dismisses_banner(page: Page, base_url: str) -> None:
_open_fresh_page(page, f"{base_url}/")
banner = page.locator("#cookie-banner")
expect(banner).to_be_visible()
# Expand the "Manage preferences" details section
banner.get_by_role("group").first.click()
# Or use the summary text
page.get_by_text("Manage preferences").click()
# Check analytics, leave advertising unchecked
analytics_checkbox = page.locator('input[name="analytics"]')
expect(analytics_checkbox).to_be_visible()
analytics_checkbox.check()
# Submit granular preferences
page.get_by_role("button", name="Save preferences").click()
page.wait_for_load_state("networkidle")
expect(banner).to_have_count(0)
@pytest.mark.e2e
def test_banner_absent_after_consent_cookie_set(page: Page, base_url: str) -> None:
"""After accepting consent, subsequent page loads must not show the banner."""
_open_fresh_page(page, f"{base_url}/")
# Accept consent
page.get_by_role("button", name="Accept all").first.click()
page.wait_for_load_state("networkidle")
# Navigate to another page in the same context — cookie should persist
page.goto(f"{base_url}/articles/", wait_until="networkidle")
expect(page.locator("#cookie-banner")).to_have_count(0)