feat: comprehensive Playwright E2E test suite #7
@@ -81,6 +81,10 @@ class Command(BaseCommand):
|
|||||||
comments_enabled=False,
|
comments_enabled=False,
|
||||||
)
|
)
|
||||||
article_index.add_child(instance=no_comments_article)
|
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()
|
no_comments_article.save_revision().publish()
|
||||||
|
|
||||||
# About page
|
# About page
|
||||||
|
|||||||
@@ -33,8 +33,25 @@ def _browser(base_url: str) -> Generator[Browser, None, None]: # noqa: ARG001
|
|||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
def page(_browser: Browser) -> Generator[Page, None, None]:
|
def page(_browser: Browser) -> Generator[Page, None, None]:
|
||||||
"""Fresh browser context + page per test — no shared state between tests."""
|
"""Fresh browser context + page per test — no shared state between tests.
|
||||||
ctx: BrowserContext = _browser.new_context()
|
|
||||||
|
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()
|
pg: Page = ctx.new_page()
|
||||||
yield pg
|
yield pg
|
||||||
ctx.close()
|
ctx.close()
|
||||||
|
|||||||
@@ -66,8 +66,6 @@ def test_copy_link_button_updates_text(page: Page, base_url: str) -> None:
|
|||||||
_go_to_article(page, base_url)
|
_go_to_article(page, base_url)
|
||||||
copy_btn = page.get_by_role("button", name="Copy link")
|
copy_btn = page.get_by_role("button", name="Copy link")
|
||||||
expect(copy_btn).to_be_visible()
|
expect(copy_btn).to_be_visible()
|
||||||
# Grant clipboard permission and click
|
|
||||||
page.context.grant_permissions(["clipboard-read", "clipboard-write"])
|
|
||||||
copy_btn.click()
|
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")
|
expect(copy_btn).to_have_text("Copied")
|
||||||
|
|||||||
@@ -48,6 +48,12 @@ def test_empty_body_shows_form_errors(page: Page, base_url: str) -> None:
|
|||||||
@pytest.mark.e2e
|
@pytest.mark.e2e
|
||||||
def test_comments_section_absent_when_disabled(page: Page, base_url: str) -> None:
|
def test_comments_section_absent_when_disabled(page: Page, base_url: str) -> None:
|
||||||
"""Article with comments_enabled=False must not show the comments section."""
|
"""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("heading", name="Comments")).to_have_count(0)
|
||||||
expect(page.get_by_role("button", name="Post comment")).to_have_count(0)
|
expect(page.get_by_role("button", name="Post comment")).to_have_count(0)
|
||||||
|
|||||||
@@ -43,13 +43,11 @@ def test_granular_preferences_save_dismisses_banner(page: Page, base_url: str) -
|
|||||||
banner = page.locator("#cookie-banner")
|
banner = page.locator("#cookie-banner")
|
||||||
expect(banner).to_be_visible()
|
expect(banner).to_be_visible()
|
||||||
|
|
||||||
# Expand the "Manage preferences" details section
|
# Click the <summary> element to expand <details> inside the banner
|
||||||
banner.get_by_role("group").first.click()
|
banner.locator("details summary").click()
|
||||||
# Or use the summary text
|
|
||||||
page.get_by_text("Manage preferences").click()
|
|
||||||
|
|
||||||
# Check analytics, leave advertising unchecked
|
# Analytics checkbox is now revealed; check it and save
|
||||||
analytics_checkbox = page.locator('input[name="analytics"]')
|
analytics_checkbox = banner.locator('input[name="analytics"]')
|
||||||
expect(analytics_checkbox).to_be_visible()
|
expect(analytics_checkbox).to_be_visible()
|
||||||
analytics_checkbox.check()
|
analytics_checkbox.check()
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from playwright.sync_api import Page, expect
|
from playwright.sync_api import Page, expect
|
||||||
|
|
||||||
@@ -9,9 +11,7 @@ from playwright.sync_api import Page, expect
|
|||||||
@pytest.mark.e2e
|
@pytest.mark.e2e
|
||||||
def test_homepage_title_contains_brand(page: Page, base_url: str) -> None:
|
def test_homepage_title_contains_brand(page: Page, base_url: str) -> None:
|
||||||
page.goto(f"{base_url}/", wait_until="networkidle")
|
page.goto(f"{base_url}/", wait_until="networkidle")
|
||||||
expect(page).to_have_title(lambda t: "No Hype AI" in t or len(t) > 0)
|
expect(page).to_have_title(re.compile("No Hype AI"))
|
||||||
# At minimum the page must load without error
|
|
||||||
assert page.url.startswith(base_url)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.e2e
|
@pytest.mark.e2e
|
||||||
|
|||||||
@@ -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:
|
def test_subscribe_invalid_email_shows_error(page: Page, base_url: str) -> None:
|
||||||
page.goto(f"{base_url}/", wait_until="networkidle")
|
page.goto(f"{base_url}/", wait_until="networkidle")
|
||||||
form = _nav_newsletter_form(page)
|
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.locator('input[type="email"]').fill("not-an-email")
|
||||||
form.get_by_role("button", name="Subscribe").click()
|
form.get_by_role("button", name="Subscribe").click()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user