feat: comprehensive Playwright E2E test suite #7

Merged
mark merged 3 commits from tests/e2e into main 2026-02-28 20:25:30 +00:00
7 changed files with 41 additions and 15 deletions
Showing only changes of commit 4992b0cb9d - Show all commits

View File

@@ -81,6 +81,10 @@ class Command(BaseCommand):
comments_enabled=False,
)
article_index.add_child(instance=no_comments_article)
# Explicitly persist False after add_child (which internally calls save())
# to guard against any field reset in the page tree insertion path.
ArticlePage.objects.filter(pk=no_comments_article.pk).update(comments_enabled=False)
no_comments_article.comments_enabled = False
no_comments_article.save_revision().publish()
# About page

View File

@@ -33,8 +33,25 @@ def _browser(base_url: str) -> Generator[Browser, None, None]: # noqa: ARG001
@pytest.fixture()
def page(_browser: Browser) -> Generator[Page, None, None]:
"""Fresh browser context + page per test — no shared state between tests."""
ctx: BrowserContext = _browser.new_context()
"""Fresh browser context + page per test — no shared state between tests.
Clipboard permissions are pre-granted so copy-link and similar interactions
work in headless Chromium without triggering the permissions dialog.
"""
ctx: BrowserContext = _browser.new_context(
permissions=["clipboard-read", "clipboard-write"],
)
# Polyfill clipboard in environments where the native API is unavailable
# (e.g. non-HTTPS Docker CI). The polyfill stores writes in a variable so
# the JS success path still runs and button text updates as expected.
ctx.add_init_script("""
if (!navigator.clipboard || !navigator.clipboard.writeText) {
Object.defineProperty(navigator, 'clipboard', {
value: { writeText: () => Promise.resolve() },
configurable: true,
});
}
""")
pg: Page = ctx.new_page()
yield pg
ctx.close()

View File

@@ -66,8 +66,6 @@ def test_copy_link_button_updates_text(page: Page, base_url: str) -> None:
_go_to_article(page, base_url)
copy_btn = page.get_by_role("button", name="Copy link")
expect(copy_btn).to_be_visible()
# Grant clipboard permission and click
page.context.grant_permissions(["clipboard-read", "clipboard-write"])
copy_btn.click()
# Button text should change to "Copied" after click
# Clipboard polyfill in conftest ensures writeText resolves; button shows "Copied"
expect(copy_btn).to_have_text("Copied")

View File

@@ -48,6 +48,12 @@ def test_empty_body_shows_form_errors(page: Page, base_url: str) -> None:
@pytest.mark.e2e
def test_comments_section_absent_when_disabled(page: Page, base_url: str) -> None:
"""Article with comments_enabled=False must not show the comments section."""
page.goto(f"{base_url}/articles/e2e-no-comments/", wait_until="networkidle")
response = page.goto(f"{base_url}/articles/e2e-no-comments/", wait_until="networkidle")
assert response is not None and response.status == 200, (
f"Expected 200 for e2e-no-comments article, got {response and response.status}"
)
# Confirm we're on the right page
expect(page.get_by_role("heading", level=1)).to_have_text("No Comments Article")
# Comments section must be absent
expect(page.get_by_role("heading", name="Comments")).to_have_count(0)
expect(page.get_by_role("button", name="Post comment")).to_have_count(0)

View File

@@ -43,13 +43,11 @@ def test_granular_preferences_save_dismisses_banner(page: Page, base_url: str) -
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()
# Click the <summary> element to expand <details> inside the banner
banner.locator("details summary").click()
# Check analytics, leave advertising unchecked
analytics_checkbox = page.locator('input[name="analytics"]')
# Analytics checkbox is now revealed; check it and save
analytics_checkbox = banner.locator('input[name="analytics"]')
expect(analytics_checkbox).to_be_visible()
analytics_checkbox.check()

View File

@@ -2,6 +2,8 @@
from __future__ import annotations
import re
import pytest
from playwright.sync_api import Page, expect
@@ -9,9 +11,7 @@ from playwright.sync_api import Page, expect
@pytest.mark.e2e
def test_homepage_title_contains_brand(page: Page, base_url: str) -> None:
page.goto(f"{base_url}/", wait_until="networkidle")
expect(page).to_have_title(lambda t: "No Hype AI" in t or len(t) > 0)
# At minimum the page must load without error
assert page.url.startswith(base_url)
expect(page).to_have_title(re.compile("No Hype AI"))
@pytest.mark.e2e

View File

@@ -26,6 +26,9 @@ def test_subscribe_valid_email_shows_confirmation(page: Page, base_url: str) ->
def test_subscribe_invalid_email_shows_error(page: Page, base_url: str) -> None:
page.goto(f"{base_url}/", wait_until="networkidle")
form = _nav_newsletter_form(page)
# Disable the browser's native HTML5 email validation so the JS handler
# fires and sends the bad value to the server (which returns 400).
page.evaluate("document.querySelector('nav form[data-newsletter-form]').setAttribute('novalidate', '')")
form.locator('input[type="email"]').fill("not-an-email")
form.get_by_role("button", name="Subscribe").click()