"""E2E tests for viewing posts. Tests post listing, pagination, and detail view functionality. """ import pytest from playwright.sync_api import Page @pytest.mark.e2e class TestHomePage: """Tests for blog home page.""" def test_homepage_loads(self, page: Page, base_url: str) -> None: """Test that homepage loads and shows expected elements.""" page.goto(f"{base_url}/web/") page.wait_for_load_state("networkidle") # Check main elements are visible assert page.locator("[data-testid='nav-logo']").is_visible() assert page.locator("[data-testid='page-title-home']").is_visible() def test_posts_list_displayed(self, page: Page, base_url: str) -> None: """Test that posts list is displayed.""" page.goto(f"{base_url}/web/") page.wait_for_load_state("networkidle") # Wait for content to load post_list = page.locator("[data-testid='post-list']") empty_state = page.locator("[data-testid='empty-state']") # Either posts or empty state should be visible assert post_list.is_visible() or empty_state.is_visible(), ( "Neither posts nor empty state visible" ) def test_navigation_present(self, page: Page, base_url: str) -> None: """Test that navigation elements are present.""" page.goto(f"{base_url}/web/") page.wait_for_load_state("networkidle") # Logo should be visible assert page.locator("[data-testid='nav-logo']").is_visible() def test_theme_toggle_works(self, page: Page, base_url: str) -> None: """Test theme toggle functionality.""" page.goto(f"{base_url}/web/") page.wait_for_load_state("networkidle") # Theme toggle should be present theme_toggle = page.locator("[data-testid='theme-toggle']") assert theme_toggle.is_visible(), "Theme toggle should be visible" # Click should not error (actual theme change requires visual check) theme_toggle.click() @pytest.mark.e2e class TestPostDetail: """Tests for individual post detail page.""" def test_post_detail_loads(self, page: Page, base_url: str) -> None: """Test that post detail page loads.""" # First get a post from home page page.goto(f"{base_url}/web/") page.wait_for_load_state("networkidle") # Skip if no posts empty_state = page.locator("[data-testid='empty-state']") if empty_state.is_visible(): pytest.skip("No posts available") # Click on first post read_more = page.locator("[data-testid^='btn-read-more-']").first if not read_more.is_visible(): pytest.skip("No posts to click") read_more.click() page.wait_for_load_state("networkidle") # Verify we're on detail page assert page.locator("[data-testid='post-detail-title']").is_visible() def test_post_detail_content(self, page: Page, base_url: str) -> None: """Test that post detail shows content.""" page.goto(f"{base_url}/web/") page.wait_for_load_state("networkidle") empty_state = page.locator("[data-testid='empty-state']") if empty_state.is_visible(): pytest.skip("No posts available") # Navigate to first post read_more = page.locator("[data-testid^='btn-read-more-']").first if not read_more.is_visible(): pytest.skip("No posts to click") read_more.click() page.wait_for_load_state("networkidle") # Check content elements assert page.locator("[data-testid='post-detail-content']").is_visible() def test_back_to_list(self, page: Page, base_url: str) -> None: """Test back button returns to list.""" page.goto(f"{base_url}/web/") page.wait_for_load_state("networkidle") empty_state = page.locator("[data-testid='empty-state']") if empty_state.is_visible(): pytest.skip("No posts available") # Go to detail read_more = page.locator("[data-testid^='btn-read-more-']").first if not read_more.is_visible(): pytest.skip("No posts to click") read_more.click() page.wait_for_load_state("networkidle") # Go back back_btn = page.locator("[data-testid='btn-back-to-list']") if back_btn.is_visible(): back_btn.click() page.wait_for_load_state("networkidle") # Verify we're back on home assert "/web/" in page.url @pytest.mark.e2e class TestEmptyState: """Tests for empty state when no posts.""" def test_empty_state_shown_when_no_posts(self, page: Page, base_url: str) -> None: """Test that empty state appears when no posts.""" page.goto(f"{base_url}/web/") page.wait_for_load_state("networkidle") # If empty state is shown empty_state = page.locator("[data-testid='empty-state']") if empty_state.is_visible(): # Check elements assert page.locator("[data-testid='empty-state-title']").is_visible() assert page.locator("[data-testid='btn-create-first-post']").is_visible() def test_create_first_post_button(self, page: Page, base_url: str) -> None: """Test 'Create first post' button in empty state.""" page.goto(f"{base_url}/web/") page.wait_for_load_state("networkidle") empty_state = page.locator("[data-testid='empty-state']") if not empty_state.is_visible(): pytest.skip("Posts exist, empty state not shown") # Guest user won't see button (requires auth) # But if button is visible, it should work create_btn = page.locator("[data-testid='btn-create-first-post']") if create_btn.is_visible(): create_btn.click() page.wait_for_load_state("networkidle") # Should navigate to form (or login for guests) assert "login" in page.url or "new" in page.url