SmokeScan / tests /conftest.py
KinetoLabs's picture
Fix critical model implementations and add sample scenarios
f3ebc82
raw
history blame
1.88 kB
"""Pytest fixtures for Playwright E2E tests."""
import pytest
import subprocess
import time
import os
import urllib.request
import urllib.error
from playwright.sync_api import sync_playwright, Browser, Page
GRADIO_PORT = 7860
GRADIO_URL = f"http://localhost:{GRADIO_PORT}"
@pytest.fixture(scope="session")
def gradio_server():
"""Start Gradio server for E2E tests."""
env = os.environ.copy()
env["MOCK_MODELS"] = "true"
# Use venv python for consistent environment
python_cmd = ".venv/bin/python" if os.path.exists(".venv/bin/python") else "python3"
# Don't capture output - let it go to console for debugging
process = subprocess.Popen(
[python_cmd, "app.py"],
env=env,
)
# Wait for server to start with health check
max_retries = 30
for i in range(max_retries):
try:
urllib.request.urlopen(GRADIO_URL, timeout=1)
break
except (urllib.error.URLError, ConnectionRefusedError):
time.sleep(1)
else:
process.terminate()
raise RuntimeError("Gradio server failed to start")
yield GRADIO_URL
process.terminate()
process.wait()
@pytest.fixture(scope="session")
def browser_instance():
"""Create browser instance for the session."""
# Use headless mode for WSL/CI environments, headed for local debugging
headless = os.environ.get("PLAYWRIGHT_HEADLESS", "true").lower() == "true"
with sync_playwright() as p:
browser = p.chromium.launch(headless=headless)
yield browser
browser.close()
@pytest.fixture
def page(browser_instance, gradio_server):
"""Create new page for each test."""
context = browser_instance.new_context()
page = context.new_page()
page.goto(gradio_server)
page.wait_for_selector("#sample_dropdown", timeout=10000)
yield page
context.close()