refactor(tests): overhaul visual tests — single server, mock emails, fix init_db
- Consolidate 3 duplicate server processes into 1 session-scoped live_server fixture in conftest.py (port 5111, shared across all visual test modules). Reduces startup overhead from ~3× to 1×. - Fix init_db mock: patch padelnomics.app.init_db (where it's used) instead of core.init_db (where it's defined). The before_serving hook imported init_db locally — patching core alone didn't prevent the real init_db from replacing the in-memory test DB. - Keep patches active through app.run_task() so before_serving hooks can't replace the test DB during the server's lifetime. - Force RESEND_API_KEY="" in the visual test server subprocess to prevent real email sends (dev mode: prints to stdout, returns "dev"). - Remove 4 screenshot-only no-op tests, replace with single test_capture_screenshots that grabs all pages in one pass. - Fix test_planner_tab_switching: remove nonexistent "metrics" tab. - Delete ~200 lines of duplicated boilerplate from 3 test files. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -7,106 +7,21 @@ and cross-cutting checks (translations, footer, language switcher).
|
||||
Skipped by default (requires `playwright install chromium`).
|
||||
Run explicitly with:
|
||||
uv run pytest -m visual tests/test_e2e_flows.py -v
|
||||
|
||||
Server runs on port 5113 (isolated from test_visual.py on 5111 and
|
||||
test_quote_wizard.py on 5112).
|
||||
"""
|
||||
import asyncio
|
||||
import multiprocessing
|
||||
import sqlite3
|
||||
import tempfile
|
||||
import time
|
||||
from pathlib import Path
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
from padelnomics.app import create_app
|
||||
from padelnomics.migrations.migrate import migrate
|
||||
from playwright.sync_api import expect, sync_playwright
|
||||
|
||||
from padelnomics import core
|
||||
from playwright.sync_api import expect
|
||||
|
||||
pytestmark = pytest.mark.visual
|
||||
|
||||
PORT = 5113
|
||||
BASE = f"http://127.0.0.1:{PORT}"
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Server / Browser Fixtures
|
||||
# =============================================================================
|
||||
|
||||
def _run_server(ready_event):
|
||||
"""Run the Quart dev server in a subprocess with in-memory SQLite."""
|
||||
import aiosqlite
|
||||
|
||||
async def _serve():
|
||||
tmp_db = str(Path(tempfile.mkdtemp()) / "schema.db")
|
||||
migrate(tmp_db)
|
||||
tmp_conn = sqlite3.connect(tmp_db)
|
||||
rows = tmp_conn.execute(
|
||||
"SELECT sql FROM sqlite_master"
|
||||
" WHERE sql IS NOT NULL"
|
||||
" AND name NOT LIKE 'sqlite_%'"
|
||||
" AND name NOT LIKE '%_fts_%'"
|
||||
" AND name != '_migrations'"
|
||||
" ORDER BY rowid"
|
||||
).fetchall()
|
||||
tmp_conn.close()
|
||||
schema_ddl = ";\n".join(r[0] for r in rows) + ";"
|
||||
|
||||
conn = await aiosqlite.connect(":memory:")
|
||||
conn.row_factory = aiosqlite.Row
|
||||
await conn.execute("PRAGMA foreign_keys=ON")
|
||||
await conn.executescript(schema_ddl)
|
||||
await conn.commit()
|
||||
core._db = conn
|
||||
|
||||
with patch.object(core, "init_db", new_callable=AsyncMock), \
|
||||
patch.object(core, "close_db", new_callable=AsyncMock):
|
||||
app = create_app()
|
||||
app.config["TESTING"] = True
|
||||
|
||||
ready_event.set()
|
||||
await app.run_task(host="127.0.0.1", port=PORT)
|
||||
|
||||
asyncio.run(_serve())
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def live_server():
|
||||
ready = multiprocessing.Event()
|
||||
proc = multiprocessing.Process(target=_run_server, args=(ready,), daemon=True)
|
||||
proc.start()
|
||||
ready.wait(timeout=10)
|
||||
time.sleep(1)
|
||||
yield BASE
|
||||
proc.terminate()
|
||||
proc.join(timeout=5)
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def browser():
|
||||
with sync_playwright() as p:
|
||||
b = p.chromium.launch(headless=True)
|
||||
yield b
|
||||
b.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def page(browser):
|
||||
"""Wide desktop page for E2E tests (1440px for sidebar tests)."""
|
||||
pg = browser.new_page(viewport={"width": 1440, "height": 900})
|
||||
yield pg
|
||||
pg.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mobile_page(browser):
|
||||
pg = browser.new_page(viewport={"width": 390, "height": 844})
|
||||
yield pg
|
||||
pg.close()
|
||||
|
||||
|
||||
def dev_login(page, base, email="test@example.com"):
|
||||
"""Instantly authenticate via dev-login endpoint."""
|
||||
page.goto(f"{base}/auth/dev-login?email={email}")
|
||||
@@ -141,7 +56,6 @@ def test_public_page_200(live_server, page, path):
|
||||
"""Every public page should return 200 and contain meaningful content."""
|
||||
resp = page.goto(live_server + path)
|
||||
assert resp.ok, f"{path} returned {resp.status}"
|
||||
# Verify page has <h1> or <h2> — not a blank error page
|
||||
heading = page.locator("h1, h2").first
|
||||
expect(heading).to_be_visible()
|
||||
|
||||
@@ -171,9 +85,7 @@ def test_planner_en_loads(live_server, page):
|
||||
"""Planner page renders wizard and tab bar."""
|
||||
resp = page.goto(live_server + "/en/planner/")
|
||||
assert resp.ok
|
||||
# Tab bar
|
||||
expect(page.locator("#nav")).to_be_visible()
|
||||
# Wizard form
|
||||
expect(page.locator("#planner-form")).to_be_visible()
|
||||
|
||||
|
||||
@@ -181,7 +93,6 @@ def test_planner_de_loads(live_server, page):
|
||||
"""German planner renders with German UI strings."""
|
||||
resp = page.goto(live_server + "/de/planner/")
|
||||
assert resp.ok
|
||||
# Should contain a German-language label somewhere
|
||||
content = page.content()
|
||||
assert "Investition" in content or "Annahmen" in content or "Anlage" in content
|
||||
|
||||
@@ -191,7 +102,6 @@ def test_planner_calculate_htmx(live_server, page):
|
||||
page.goto(live_server + "/en/planner/")
|
||||
page.wait_for_load_state("networkidle")
|
||||
|
||||
# #tab-content starts display:none; clicking a result tab makes it visible
|
||||
capex_btn = page.locator("button[data-tab='capex'], [data-tab='capex']").first
|
||||
capex_btn.click()
|
||||
page.wait_for_timeout(1000)
|
||||
@@ -206,20 +116,18 @@ def test_planner_tab_switching(live_server, page):
|
||||
page.goto(live_server + "/en/planner/")
|
||||
page.wait_for_load_state("networkidle")
|
||||
|
||||
tabs = ["capex", "operating", "cashflow", "returns", "metrics"]
|
||||
tabs = ["capex", "operating", "cashflow", "returns"]
|
||||
seen_contents = set()
|
||||
|
||||
for tab_id in tabs:
|
||||
btn = page.locator(f"button[data-tab='{tab_id}'], [data-tab='{tab_id}']").first
|
||||
if btn.count() == 0:
|
||||
# Try clicking by visible text
|
||||
btn = page.get_by_role("button", name=tab_id, exact=False).first
|
||||
btn.click()
|
||||
page.wait_for_timeout(600)
|
||||
html = page.locator("#tab-content").inner_html()
|
||||
seen_contents.add(html[:100]) # first 100 chars as fingerprint
|
||||
seen_contents.add(html[:100])
|
||||
|
||||
# All tabs should render distinct content
|
||||
assert len(seen_contents) >= 3, "Tab content didn't change across tabs"
|
||||
|
||||
|
||||
@@ -228,7 +136,6 @@ def test_planner_chart_data_present(live_server, page):
|
||||
page.goto(live_server + "/en/planner/")
|
||||
page.wait_for_load_state("networkidle")
|
||||
|
||||
# Charts on the capex tab
|
||||
chart_scripts = page.locator("script[type='application/json']")
|
||||
assert chart_scripts.count() >= 1, "No chart JSON script tags found"
|
||||
|
||||
@@ -241,11 +148,10 @@ def test_planner_quote_sidebar_visible_wide(live_server, browser):
|
||||
|
||||
sidebar = pg.locator(".quote-sidebar")
|
||||
if sidebar.count() > 0:
|
||||
# If present, should not be display:none
|
||||
display = pg.evaluate(
|
||||
"getComputedStyle(document.querySelector('.quote-sidebar')).display"
|
||||
)
|
||||
assert display != "none", f"Quote sidebar is hidden on wide viewport: display={display}"
|
||||
assert display != "none", f"Quote sidebar hidden on wide viewport: display={display}"
|
||||
pg.close()
|
||||
|
||||
|
||||
@@ -282,7 +188,7 @@ def test_authenticated_dashboard_loads(live_server, page):
|
||||
|
||||
|
||||
def test_dashboard_quote_link_goes_to_wizard(live_server, page):
|
||||
"""Dashboard 'request quote' button should link to the quote wizard, not suppliers page."""
|
||||
"""Dashboard 'request quote' button should link to the quote wizard."""
|
||||
dev_login(page, live_server, "dashquote@example.com")
|
||||
href = page.locator("a", has_text="Quote").first.get_attribute("href")
|
||||
assert "/leads/quote" in href, f"Expected quote wizard link, got: {href}"
|
||||
@@ -318,7 +224,6 @@ def test_directory_de_loads(live_server, page):
|
||||
resp = page.goto(live_server + "/de/directory/")
|
||||
assert resp.ok
|
||||
content = page.content()
|
||||
# Should have German-language UI (filter label, heading, etc.)
|
||||
assert "Lieferanten" in content or "Anbieter" in content or "Kategorie" in content or resp.ok
|
||||
|
||||
|
||||
@@ -333,7 +238,6 @@ def test_directory_search_htmx(live_server, page):
|
||||
|
||||
search.fill("padel")
|
||||
page.wait_for_timeout(600)
|
||||
# Results container should exist
|
||||
results = page.locator("#supplier-results, #results, [id*='result']").first
|
||||
expect(results).to_be_visible()
|
||||
|
||||
@@ -353,15 +257,12 @@ def test_quote_step1_de_loads(live_server, page):
|
||||
resp = page.goto(live_server + "/de/leads/quote")
|
||||
assert resp.ok
|
||||
content = page.content()
|
||||
# Should have at least some German text
|
||||
assert "Anlage" in content or "Platz" in content or "Projekt" in content or resp.ok
|
||||
|
||||
|
||||
def test_quote_verify_url_includes_lang(live_server, page):
|
||||
"""Verify the leads/verify route exists at /<lang>/leads/verify."""
|
||||
# GET with no token should redirect to login or show error — but should NOT 404
|
||||
resp = page.goto(live_server + "/en/leads/verify?token=invalid")
|
||||
# Should be 200 (shows error) or a redirect — not 404
|
||||
assert resp.status != 404, "Verify endpoint returned 404 — lang prefix missing?"
|
||||
|
||||
|
||||
@@ -392,8 +293,7 @@ def test_footer_present_on_public_pages(live_server, page):
|
||||
"""Footer should be present on all public pages."""
|
||||
for path in ["/en/", "/en/features", "/en/about"]:
|
||||
page.goto(live_server + path)
|
||||
footer = page.locator("footer")
|
||||
expect(footer).to_be_visible()
|
||||
expect(page.locator("footer")).to_be_visible()
|
||||
|
||||
|
||||
def test_footer_has_four_column_layout(live_server, page):
|
||||
@@ -401,7 +301,6 @@ def test_footer_has_four_column_layout(live_server, page):
|
||||
page.goto(live_server + "/en/")
|
||||
page.wait_for_load_state("networkidle")
|
||||
|
||||
# Count footer navigation columns (divs/sections with link lists)
|
||||
footer_cols = page.evaluate("""
|
||||
(() => {
|
||||
const footer = document.querySelector('footer');
|
||||
@@ -419,7 +318,6 @@ def test_language_switcher_en_to_de(live_server, page):
|
||||
page.goto(live_server + "/en/")
|
||||
page.wait_for_load_state("networkidle")
|
||||
|
||||
# Find the footer language switcher link to /de/
|
||||
de_link = page.locator("a[href='/de/']").first
|
||||
if de_link.count() == 0:
|
||||
pytest.skip("No DE language switcher link found")
|
||||
@@ -427,18 +325,16 @@ def test_language_switcher_en_to_de(live_server, page):
|
||||
href = de_link.get_attribute("href")
|
||||
assert href and "/de" in href, f"DE link has unexpected href: {href}"
|
||||
|
||||
# Navigate directly to verify the German page loads
|
||||
page.goto(live_server + "/de/")
|
||||
page.wait_for_load_state("networkidle")
|
||||
assert "/de/" in page.url, f"Language switch failed: {page.url}"
|
||||
|
||||
|
||||
def test_no_missing_translations_en(live_server, page):
|
||||
"""EN pages should not contain 'None' or 'undefined' translation markers."""
|
||||
"""EN pages should not contain untranslated markers."""
|
||||
for path in ["/en/", "/en/features", "/en/about"]:
|
||||
page.goto(live_server + path)
|
||||
content = page.locator("body").inner_text()
|
||||
# Check for obvious translation failure markers
|
||||
assert "t.auth_" not in content, f"Untranslated key in {path}"
|
||||
assert "{{" not in content, f"Jinja template not rendered in {path}"
|
||||
|
||||
@@ -459,7 +355,6 @@ def test_auth_login_page_german(live_server, page):
|
||||
}])
|
||||
page.goto(live_server + "/auth/login")
|
||||
content = page.content()
|
||||
# German auth page should contain German text
|
||||
assert "Anmelden" in content or "E-Mail" in content or "Weiter" in content
|
||||
|
||||
|
||||
@@ -472,16 +367,15 @@ def test_404_for_nonexistent_page(live_server, page):
|
||||
def test_legacy_redirect_terms(live_server, page):
|
||||
"""Legacy /terms should redirect to /en/terms."""
|
||||
resp = page.goto(live_server + "/terms")
|
||||
# Either 301/302 redirect or 200 at /en/terms
|
||||
assert resp.ok or resp.status in (301, 302)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# H. Markets Waitlist (WAITLIST_MODE=False by default — page should load)
|
||||
# H. Markets
|
||||
# =============================================================================
|
||||
|
||||
def test_markets_hub_loads(live_server, page):
|
||||
"""Markets hub should load normally when WAITLIST_MODE is off."""
|
||||
"""Markets hub should load normally."""
|
||||
resp = page.goto(live_server + "/en/markets")
|
||||
assert resp.ok
|
||||
expect(page.locator("h1, h2").first).to_be_visible()
|
||||
@@ -494,7 +388,7 @@ def test_markets_results_partial_loads(live_server, page):
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# I. Tooltip Presence (result tab tooltips)
|
||||
# I. Tooltip Presence
|
||||
# =============================================================================
|
||||
|
||||
def test_planner_tooltips_present(live_server, page):
|
||||
@@ -502,12 +396,10 @@ def test_planner_tooltips_present(live_server, page):
|
||||
page.goto(live_server + "/en/planner/")
|
||||
page.wait_for_load_state("networkidle")
|
||||
|
||||
# The returns tab should have tooltip spans
|
||||
returns_btn = page.locator("button[data-tab='returns'], [data-tab='returns']").first
|
||||
if returns_btn.count() > 0:
|
||||
returns_btn.click()
|
||||
page.wait_for_timeout(600)
|
||||
|
||||
# After clicking returns tab, look for tooltip info spans
|
||||
ti_spans = page.locator(".ti")
|
||||
assert ti_spans.count() >= 1, "No tooltip spans (.ti) found on results tab"
|
||||
|
||||
Reference in New Issue
Block a user