|
| 1 | +import subprocess |
| 2 | +import time |
| 3 | +import os |
| 4 | +import pytest |
| 5 | +from playwright.sync_api import Page, expect |
| 6 | + |
| 7 | +from .test_builds import validate_mkdocs_file |
| 8 | + |
| 9 | +pytestmark = pytest.mark.e2e |
| 10 | + |
| 11 | +MKDOCS_URL = "http://localhost:8000" |
| 12 | +PATHS = ["index.html", "build_in_multiple/index.html", "empty/index.html", "multiple/index.html", "oauth2/index.html", "options/index.html", "url/index.html", "sub_dir/page_in_sub_dir/index.html"] |
| 13 | + |
| 14 | +@pytest.fixture(scope="session", autouse=True) |
| 15 | +def start_mkdocs_server(tmp_path_factory): |
| 16 | + """ Start a local server to serve the MkDocs site """ |
| 17 | + |
| 18 | + tmp_path = tmp_path_factory.mktemp("mkdocs_test") |
| 19 | + mkdocs_file = "mkdocs-material.yml" |
| 20 | + testproject_path = validate_mkdocs_file(tmp_path, f"tests/fixtures/{mkdocs_file}") |
| 21 | + |
| 22 | + process = subprocess.Popen(["uv", "run", "-m", "http.server", "-d", os.path.join(testproject_path, "site")], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 23 | + time.sleep(1) # wait for server to start |
| 24 | + |
| 25 | + yield |
| 26 | + |
| 27 | + process.terminate() |
| 28 | + process.wait() |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def page_context(page: Page): |
| 32 | + """ |
| 33 | + Create a new page context with a longer timeout |
| 34 | + """ |
| 35 | + page.set_default_timeout(5000) |
| 36 | + return page |
| 37 | + |
| 38 | +@pytest.mark.parametrize("path", PATHS) |
| 39 | +def test_no_console_errors(page_context: Page, path): |
| 40 | + """ |
| 41 | + Validate that there are no console errors |
| 42 | + """ |
| 43 | + url = f"{MKDOCS_URL}/{path}" |
| 44 | + errors = [] |
| 45 | + |
| 46 | + page_context.on("console", lambda msg: errors.append(msg.text) if msg.type == "error" else None) |
| 47 | + page_context.goto(url) |
| 48 | + |
| 49 | + assert not errors, f"Got {len(errors)} console errors: {errors}" |
| 50 | + |
| 51 | +@pytest.mark.parametrize("path", PATHS) |
| 52 | +def test_mkdocs_screenshot(page_context: Page, path): |
| 53 | + """ |
| 54 | + Take a screenshot of the MkDocs |
| 55 | + """ |
| 56 | + url = f"{MKDOCS_URL}/{path}" |
| 57 | + |
| 58 | + page_context.goto(url) |
| 59 | + time.sleep(3) |
| 60 | + screenshot_path = f"playwright-results/{path.replace('/', '_')}.png" |
| 61 | + page_context.screenshot(path=screenshot_path, full_page=True) |
0 commit comments