Skip to content

Commit efe5c93

Browse files
committed
Update code and tests
1 parent af4365e commit efe5c93

File tree

10 files changed

+79
-6
lines changed

10 files changed

+79
-6
lines changed

.github/workflows/ci.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
run: uv run bandit -r mkdocs_swagger_ui_tag
2727

2828
- name: Run tests
29-
run: uv run pytest tests
29+
run: uv run pytest tests -m "not e2e"
3030

3131
- name: Build the package
3232
run: uv build

.github/workflows/scheduled_unittests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ jobs:
2929
run: uv sync --all-extras --dev
3030

3131
- name: Run tests
32-
run: uv run pytest tests
32+
run: uv run pytest tests -m "not e2e"

.github/workflows/unittests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ jobs:
2727
run: uv sync --all-extras --dev --frozen
2828

2929
- name: Run tests
30-
run: uv run pytest tests
30+
run: uv run pytest tests -m "not e2e"

.github/workflows/unittests_codecov.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
run: |
3838
git config --global user.name "Github Action"
3939
git config --global user.email "[email protected]"
40-
uv run pytest --cov=mkdocs_swagger_ui_tag --cov-report=xml
40+
uv run pytest --cov=mkdocs_swagger_ui_tag --cov-report=xml -m "not e2e"
4141
4242
- name: Upload coverage to Codecov
4343
if: "contains(env.USING_COVERAGE, matrix.python-version)"

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
.DS_Store
22
.vscode/
3+
playwright-results/
4+
docs/
35

46
# Byte-compiled / optimized / DLL files
57
__pycache__/

mkdocs_swagger_ui_tag/plugin.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,15 @@ def render_template(openapi_spec_url, swagger_ui_ele):
268268
const dark_scheme_name = "{self.config["dark_scheme_name"]}"
269269
"""
270270
js_code.string += """
271-
window.scheme = document.body.getAttribute("data-md-color-scheme")
271+
const schemeAttr = document.body.getAttribute("data-md-color-scheme");
272+
const isMediaPrefersScheme = document.body.getAttribute("data-md-color-media") === "(prefers-color-scheme: dark)";
273+
274+
if (!isMediaPrefersScheme) {
275+
window.__init_is_dark_mode = (schemeAttr === dark_scheme_name);
276+
} else {
277+
const computedScheme = window.getComputedStyle(document.body).getPropertyValue('color-scheme');
278+
window.__init_is_dark_mode = computedScheme === "dark";
279+
}
272280
const options = {
273281
attributeFilter: ['data-md-color-scheme'],
274282
};

pytest.ini

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
[pytest]
22
log_cli = 1
3+
markers =
4+
e2e: marks end-to-end tests

tests/__init__.py

Whitespace-only changes.

tests/test_builds.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def validate_additional_script_code(html_content, exists=True):
158158

159159
def validate_additional_script_code_for_material(html_content, exists=True):
160160
assert exists == (
161-
'window.scheme = document.body.getAttribute("data-md-color-scheme")'
161+
'const schemeAttr = document.body.getAttribute("data-md-color-scheme")'
162162
in html_content
163163
)
164164
assert exists == (

tests/test_e2e.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)