"""E2E tests for article detail pages.""" 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_article_title_visible(page: Page, base_url: str) -> None: _go_to_article(page, base_url) h1 = page.get_by_role("heading", level=1) expect(h1).to_be_visible() assert h1.inner_text().strip() != "" @pytest.mark.e2e def test_article_read_time_visible(page: Page, base_url: str) -> None: _go_to_article(page, base_url) # Read time is rendered as "N min read" expect(page.get_by_text("min read")).to_be_visible() @pytest.mark.e2e def test_article_share_section_present(page: Page, base_url: str) -> None: _go_to_article(page, base_url) share_section = page.get_by_role("region", name="Share this article") expect(share_section).to_be_visible() expect(share_section.get_by_role("link", name="Share on X")).to_be_visible() expect(share_section.get_by_role("link", name="Share on LinkedIn")).to_be_visible() expect(share_section.get_by_role("button", name="Copy link")).to_be_visible() @pytest.mark.e2e def test_article_comments_section_present(page: Page, base_url: str) -> None: _go_to_article(page, base_url) # The article has comments_enabled=True expect(page.get_by_role("heading", name="Comments", exact=True)).to_be_visible() expect(page.get_by_role("button", name="Post comment")).to_be_visible() @pytest.mark.e2e def test_article_newsletter_aside_present(page: Page, base_url: str) -> None: _go_to_article(page, base_url) # There's a Newsletter aside within the article page aside = page.locator("aside") expect(aside).to_be_visible() expect(aside.locator('input[type="email"]')).to_be_visible() @pytest.mark.e2e def test_article_related_section_present(page: Page, base_url: str) -> None: _go_to_article(page, base_url) # Related section heading expect(page.get_by_role("heading", name="Related")).to_be_visible() @pytest.mark.e2e def test_copy_link_button_updates_text(page: Page, base_url: str) -> None: _go_to_article(page, base_url) copy_btn = page.locator("[data-copy-link]") expect(copy_btn).to_be_visible() # Force-override clipboard so writeText always resolves, even in non-HTTPS headless context page.evaluate("navigator.clipboard.writeText = () => Promise.resolve()") copy_btn.click() expect(copy_btn).to_have_text("Copied")