"""E2E tests for the comment submission flow.""" from __future__ import annotations import pytest from playwright.sync_api import Page, expect ARTICLE_SLUG = "nightly-playwright-journey" def _go_to_article(page: Page, base_url: str) -> None: page.goto(f"{base_url}/articles/{ARTICLE_SLUG}/", wait_until="networkidle") @pytest.mark.e2e def test_valid_comment_submission_redirects(page: Page, base_url: str) -> None: _go_to_article(page, base_url) # Fill the main comment form (not a reply form) form = page.locator("form[action]").filter(has=page.get_by_role("button", name="Post comment")) form.locator('input[name="author_name"]').fill("E2E Tester") form.locator('input[name="author_email"]').fill("e2e@example.com") form.locator('textarea[name="body"]').fill("This is a test comment from Playwright.") form.get_by_role("button", name="Post comment").click() # Successful submission redirects back to the article with ?commented=1 page.wait_for_url(lambda url: "commented=1" in url, timeout=10_000) assert "commented=1" in page.url @pytest.mark.e2e def test_empty_body_shows_form_errors(page: Page, base_url: str) -> None: _go_to_article(page, base_url) form = page.locator("form[action]").filter(has=page.get_by_role("button", name="Post comment")) form.locator('input[name="author_name"]').fill("E2E Tester") form.locator('input[name="author_email"]').fill("e2e@example.com") form.locator('textarea[name="body"]').fill(" ") # whitespace-only body form.get_by_role("button", name="Post comment").click() page.wait_for_load_state("networkidle") # The page re-renders with the error summary visible expect(page.locator('[aria-label="Comment form errors"]')).to_be_visible() # URL must NOT have ?commented=1 — form was not accepted assert "commented=1" not in page.url @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.""" 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 — exact=True prevents matching "No Comments Article" h1 expect(page.get_by_role("heading", name="Comments", exact=True)).to_have_count(0) expect(page.get_by_role("button", name="Post comment")).to_have_count(0)