| 1 |
""" |
| 2 |
Build minimal test sites with sphinx_build_factory and test them with Playwright. |
| 3 |
When adding new tests to this file, remember to also add the corresponding test site |
| 4 |
to `tests/sites/` or use an existing one. |
| 5 |
""" |
| 6 |
|
| 7 |
import re |
| 8 |
|
| 9 |
from pathlib import Path |
| 10 |
from typing import Callable |
| 11 |
from urllib.parse import urljoin |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
|
| 16 |
try: |
| 17 |
from pathlib import UnsupportedOperation # added in Py 3.13 |
| 18 |
except ImportError: |
| 19 |
UnsupportedOperation = NotImplementedError |
| 20 |
|
| 21 |
# Using importorskip to ensure these tests are only loaded if Playwright is installed. |
| 22 |
playwright = pytest.importorskip("playwright") |
| 23 |
from playwright.sync_api import ConsoleMessage, Page, expect # noqa: E402 |
| 24 |
|
| 25 |
|
| 26 |
repo_path = Path(__file__).parents[1] |
| 27 |
test_sites_dir = repo_path / "docs" / "_build" / "html" / "playwright_tests" |
| 28 |
|
| 29 |
|
| 30 |
# ------------------------- Helper functions ------------------------- |
| 31 |
def _is_overflowing(element): |
| 32 |
"""Check if an element is being shortened via CSS due to text-overflow property. |
| 33 |
|
| 34 |
We can't check the rendered text because we can't easily get that; all we can get |
| 35 |
is the text as it exists in the DOM (prior to its truncation/elision). Thus we must |
| 36 |
directly compare the rendered clientWidth to the scrollWidth required to display the |
| 37 |
text. |
| 38 |
""" |
| 39 |
return element.evaluate("e => e.clientWidth < e.scrollWidth", element) |
| 40 |
|
| 41 |
|
| 42 |
def _build_test_site(site_name: str, sphinx_build_factory: Callable) -> None: |
| 43 |
"""Helper function for building simple test sites (with no `confoverrides`).""" |
| 44 |
sphinx_build = sphinx_build_factory(site_name) |
| 45 |
sphinx_build.build() |
| 46 |
assert (sphinx_build.outdir / "index.html").exists(), sphinx_build.outdir.glob("*") |
| 47 |
return sphinx_build.outdir |
| 48 |
|
| 49 |
|
| 50 |
def _check_test_site(site_name: str, site_path: Path, test_func: Callable): |
| 51 |
"""Make the built test site available to Playwright, then run `test_func` on it.""" |
| 52 |
# Need to ensure parent directories exist in CI |
| 53 |
test_sites_dir.mkdir(exist_ok=True, parents=True) |
| 54 |
symlink_path = test_sites_dir / site_name |
| 55 |
try: |
| 56 |
symlink_path.symlink_to(site_path, True) |
| 57 |
except UnsupportedOperation: |
| 58 |
pytest.xfail("filesystem doesn't support symlinking") |
| 59 |
else: |
| 60 |
test_func() |
| 61 |
finally: |
| 62 |
symlink_path.unlink() |
| 63 |
test_sites_dir.rmdir() |
| 64 |
|
| 65 |
|
| 66 |
# ------------------------- Test functions: style ------------------------- |
| 67 |
def test_version_switcher_highlighting( |
| 68 |
sphinx_build_factory: Callable, page: Page, url_base: str |
| 69 |
) -> None: |
| 70 |
""" |
| 71 |
In sidebar and topbar - version switcher should apply highlight color to currently |
| 72 |
selected version. |
| 73 |
""" |
| 74 |
site_name = "version_switcher" |
| 75 |
site_path = _build_test_site(site_name, sphinx_build_factory=sphinx_build_factory) |
| 76 |
|
| 77 |
def check_version_switcher_highlighting(): |
| 78 |
page.goto(urljoin(url_base, f"playwright_tests/{site_name}/index.html")) |
| 79 |
# no need to include_hidden here ↓↓↓, we just need to get the active |
| 80 |
# version name |
| 81 |
button = page.get_by_role("button").filter(has_text="dev") |
| 82 |
active_version_name = button.get_attribute("data-active-version-name") |
| 83 |
|
| 84 |
# here we do include_hidden, since we are not adding this in the sidebar |
| 85 |
# we should only get one entry |
| 86 |
entries = page.get_by_role("option", include_hidden=True).filter( |
| 87 |
has_text=active_version_name |
| 88 |
) |
| 89 |
assert entries.count() == 1 |
| 90 |
# make sure they're highlighted |
| 91 |
for entry in entries.all(): |
| 92 |
light_mode = "rgb(10, 125, 145)" # pst-color-primary |
| 93 |
# dark_mode = "rgb(63, 177, 197)" |
| 94 |
expect(entry).to_have_css("color", light_mode) |
| 95 |
|
| 96 |
_check_test_site(site_name, site_path, check_version_switcher_highlighting) |
| 97 |
|
| 98 |
|
| 99 |
def test_colors(sphinx_build_factory: Callable, page: Page, url_base: str) -> None: |
| 100 |
"""Test that things get colored the way we expect them to. |
| 101 |
|
| 102 |
Note: this is not comprehensive! Please feel free to add to this test by editing |
| 103 |
`../sites/colors/index.rst` and adding more `expect` statements below. |
| 104 |
""" |
| 105 |
site_name = "colors" |
| 106 |
site_path = _build_test_site(site_name, sphinx_build_factory=sphinx_build_factory) |
| 107 |
|
| 108 |
def check_colors(): |
| 109 |
page.goto(urljoin(url_base, f"playwright_tests/{site_name}/index.html")) |
| 110 |
primary_color = "rgb(10, 125, 145)" |
| 111 |
hover_color = "rgb(128, 69, 229)" |
| 112 |
spans = { |
| 113 |
"text link": primary_color, |
| 114 |
"cross reference link": primary_color, |
| 115 |
"inline code": "rgb(145, 37, 131)", |
| 116 |
"code link": "rgb(8, 93, 108)", # teal-600, AKA #085d6c |
| 117 |
} |
| 118 |
for text, color in spans.items(): |
| 119 |
el = page.get_by_text(text).first |
| 120 |
expect(el).to_have_css("color", color) |
| 121 |
if "link" in text: |
| 122 |
el.hover() |
| 123 |
expect(el).to_have_css("color", hover_color) |
| 124 |
|
| 125 |
_check_test_site(site_name, site_path, check_colors) |
| 126 |
|
| 127 |
|
| 128 |
# ------------------------- Test functions: layout & components ----------------------- |
| 129 |
|
| 130 |
|
| 131 |
def test_breadcrumb_expansion( |
| 132 |
sphinx_build_factory: Callable, page: Page, url_base: str |
| 133 |
) -> None: |
| 134 |
"""Test breadcrumb text-overflow.""" |
| 135 |
site_name = "breadcrumbs" |
| 136 |
site_path = _build_test_site(site_name, sphinx_build_factory=sphinx_build_factory) |
| 137 |
|
| 138 |
def check_breadcrumb_expansion(): |
| 139 |
# wide viewport width → no truncation |
| 140 |
page.set_viewport_size({"width": 1440, "height": 720}) |
| 141 |
page.goto(urljoin(url_base, "community/topics/config.html")) |
| 142 |
expect(page.get_by_label("Breadcrumb").get_by_role("list")).to_contain_text( |
| 143 |
"Update Sphinx configuration during the build" |
| 144 |
) |
| 145 |
el = page.get_by_text("Update Sphinx configuration during the build").nth(1) |
| 146 |
expect(el).to_have_css("overflow-x", "hidden") |
| 147 |
expect(el).to_have_css("text-overflow", "ellipsis") |
| 148 |
assert not _is_overflowing(el) |
| 149 |
# narrow viewport width → truncation |
| 150 |
page.set_viewport_size({"width": 150, "height": 720}) |
| 151 |
assert _is_overflowing(el) |
| 152 |
|
| 153 |
_check_test_site(site_name, site_path, check_breadcrumb_expansion) |
| 154 |
|
| 155 |
|
| 156 |
def test_breadcrumbs_everywhere( |
| 157 |
sphinx_build_factory: Callable, page: Page, url_base: str |
| 158 |
) -> None: |
| 159 |
"""Test breadcrumbs truncate properly when placed in various parts of the layout.""" |
| 160 |
site_name = "breadcrumbs" |
| 161 |
site_path = _build_test_site(site_name, sphinx_build_factory=sphinx_build_factory) |
| 162 |
|
| 163 |
def check_breadcrumb_truncation(): |
| 164 |
page.goto( |
| 165 |
urljoin(url_base, f"playwright_tests/{site_name}/hansel/gretel/house.html") |
| 166 |
) |
| 167 |
# sidebar should overflow |
| 168 |
text = "In the oven with my sister, so hot right now. Soooo. Hotttt." |
| 169 |
el = page.locator("#main-content").get_by_text(text).last |
| 170 |
assert _is_overflowing(el) |
| 171 |
# footer containers never trigger ellipsis overflow because min-width is content |
| 172 |
el = page.locator(".footer-items__center > .footer-item") |
| 173 |
assert not _is_overflowing(el) |
| 174 |
|
| 175 |
_check_test_site(site_name, site_path, check_breadcrumb_truncation) |
| 176 |
|
| 177 |
|
| 178 |
def test_article_toc_syncing( |
| 179 |
sphinx_build_factory: Callable, |
| 180 |
page: Page, |
| 181 |
url_base: str, |
| 182 |
) -> None: |
| 183 |
"""Test that the secondary sidebar TOC highlights the correct entry.""" |
| 184 |
site_name = "version_switcher" |
| 185 |
site_path = _build_test_site(site_name, sphinx_build_factory=sphinx_build_factory) |
| 186 |
assert site_path |
| 187 |
|
| 188 |
def check_toc_syncing(): |
| 189 |
page.goto( |
| 190 |
urljoin(url_base, f"playwright_tests/{site_name}/fixture_blocks.html") |
| 191 |
) |
| 192 |
|
| 193 |
# Define locators and other variables |
| 194 |
toc = page.locator("#pst-page-toc-nav") |
| 195 |
first_toc_link = toc.get_by_role("link", name="Block Quotes") |
| 196 |
some_toc_link_with_active = toc.locator("a.active") |
| 197 |
some_toc_link_with_aria_current = toc.locator("a[aria-current]") |
| 198 |
first_heading = page.locator(str(first_toc_link.get_attribute("href"))) |
| 199 |
active_re = re.compile("active") |
| 200 |
|
| 201 |
# Click the first TOC link, check that it gets highlighted and that the |
| 202 |
# associated heading is in the viewport |
| 203 |
first_toc_link.click() |
| 204 |
expect(first_toc_link).to_have_class(active_re) |
| 205 |
expect(first_toc_link).to_have_attribute("aria-current", "true") |
| 206 |
expect(first_heading).to_be_in_viewport() |
| 207 |
|
| 208 |
# Verify that one and only one TOC link is active/highlighted/current |
| 209 |
expect(some_toc_link_with_active).to_have_count(1) |
| 210 |
expect(some_toc_link_with_aria_current).to_have_count(1) |
| 211 |
|
| 212 |
# After clicking a link, the pydata-sphinx-theme.js script sets a 1 |
| 213 |
# second timeout before processing intersection events again |
| 214 |
page.wait_for_timeout(1500) |
| 215 |
|
| 216 |
# Scroll to the bottom of the page, check that the first TOC entry |
| 217 |
# becomes un-highlighted. For some reason, we have to use page.mouse |
| 218 |
# rather than: |
| 219 |
# |
| 220 |
# page.locator("p.copyright").scroll_into_view_if_needed() |
| 221 |
# |
| 222 |
# Apparently the Playwright scroll function does not trigger the |
| 223 |
# IntersectionObserver callback. |
| 224 |
page_height = page.evaluate("document.body.offsetHeight") |
| 225 |
# If we scroll 1 pixel per turn of the loop, the test can take a long |
| 226 |
# time to run, so we limit the number of loops |
| 227 |
num_loops = 200 |
| 228 |
delta_y = int(page_height / num_loops) |
| 229 |
for _ in range(num_loops + 1): # add +1 because int() always rounds down |
| 230 |
page.mouse.wheel(0, delta_y) |
| 231 |
|
| 232 |
# Verify that the active state is removed from the first TOC link |
| 233 |
expect(first_toc_link).not_to_have_class(active_re) |
| 234 |
expect(first_toc_link).not_to_have_attribute("aria-current", re.compile(".")) |
| 235 |
|
| 236 |
# Verify that one and only one TOC link is active/highlighted/current |
| 237 |
expect(some_toc_link_with_active).to_have_count(1) |
| 238 |
expect(some_toc_link_with_aria_current).to_have_count(1) |
| 239 |
|
| 240 |
_check_test_site(site_name, site_path, check_toc_syncing) |
| 241 |
|
| 242 |
|
| 243 |
# ----------------- Test functions: collapse primary sidebar button -------------------- |
| 244 |
class TestCollapseSidebarButton: |
| 245 |
"""Group the tests for the collapse sidebar button.""" |
| 246 |
|
| 247 |
site_name = "sidebars" |
| 248 |
|
| 249 |
def test_collapse_sidebar_button( |
| 250 |
self, sphinx_build_factory: Callable, page: Page, url_base: str |
| 251 |
) -> None: |
| 252 |
"""Test basic functionality of the collapse sidebar button. |
| 253 |
|
| 254 |
Clicking the button should collapse the sidebar. Clicking again should expand. |
| 255 |
""" |
| 256 |
site_path = _build_test_site( |
| 257 |
self.site_name, sphinx_build_factory=sphinx_build_factory |
| 258 |
) |
| 259 |
assert site_path is not None |
| 260 |
|
| 261 |
def get_width(locator): |
| 262 |
bbox = locator.bounding_box() |
| 263 |
assert bbox is not None |
| 264 |
return bbox["width"] |
| 265 |
|
| 266 |
def check_collapse_expand(): |
| 267 |
page.goto( |
| 268 |
urljoin( |
| 269 |
url_base, f"playwright_tests/{self.site_name}/section1/index.html" |
| 270 |
) |
| 271 |
) |
| 272 |
sidebar = page.locator("#pst-primary-sidebar") |
| 273 |
expanded_sidebar_width = get_width(sidebar) |
| 274 |
|
| 275 |
# Before collapsing the sidebar, a link to another page should be visible |
| 276 |
toc_link = sidebar.get_by_role("link", name="Section 1 page 1") |
| 277 |
expect(toc_link).to_be_visible() |
| 278 |
|
| 279 |
button = page.locator("#pst-collapse-sidebar-button") |
| 280 |
expect(button).to_have_attribute("aria-expanded", "true") |
| 281 |
expanded_button_width = get_width(button) |
| 282 |
|
| 283 |
# Collapse the sidebar |
| 284 |
button.click() |
| 285 |
|
| 286 |
# Wait for the button have aria-expanded=false so we know the CSS |
| 287 |
# transitions have finished |
| 288 |
expect(button).to_have_attribute("aria-expanded", "false") |
| 289 |
|
| 290 |
# After collapsing sidebar, the link to another page should NOT be visible |
| 291 |
expect(toc_link).not_to_be_visible() |
| 292 |
|
| 293 |
# But the button should still be visible though smaller |
| 294 |
expect(button).to_be_visible() |
| 295 |
assert get_width(button) < expanded_button_width |
| 296 |
|
| 297 |
# After collapsing the sidebar, its width should be smaller than it |
| 298 |
# was before collapse |
| 299 |
assert get_width(sidebar) < expanded_sidebar_width |
| 300 |
|
| 301 |
# Expand the sidebar |
| 302 |
button.click() |
| 303 |
|
| 304 |
# Wait for aria-expanded=true so we know CSS transitions have finished |
| 305 |
expect(button).to_have_attribute("aria-expanded", "true") |
| 306 |
expect(toc_link).to_be_visible() |
| 307 |
assert get_width(sidebar) == expanded_sidebar_width |
| 308 |
assert get_width(button) == expanded_button_width |
| 309 |
|
| 310 |
_check_test_site(self.site_name, site_path, check_collapse_expand) |
| 311 |
|
| 312 |
def test_collapse_sidebar_button_not_in_mobile( |
| 313 |
self, sphinx_build_factory: Callable, page: Page, url_base: str |
| 314 |
) -> None: |
| 315 |
"""Collapse button should not appear in mobile sidebar.""" |
| 316 |
# Size viewport to mobile phone |
| 317 |
page.set_viewport_size({"width": 400, "height": 900}) |
| 318 |
|
| 319 |
site_path = _build_test_site( |
| 320 |
self.site_name, sphinx_build_factory=sphinx_build_factory |
| 321 |
) |
| 322 |
assert site_path is not None |
| 323 |
|
| 324 |
def check_not_in_mobile(): |
| 325 |
page.goto( |
| 326 |
urljoin( |
| 327 |
url_base, f"playwright_tests/{self.site_name}/section1/index.html" |
| 328 |
) |
| 329 |
) |
| 330 |
|
| 331 |
dialog = page.locator("#pst-primary-sidebar-modal") |
| 332 |
hamburger = page.get_by_role("button", name="Site navigation") |
| 333 |
collapse_button = page.locator("#pst-collapse-sidebar-button") |
| 334 |
|
| 335 |
expect(dialog).not_to_be_visible() |
| 336 |
expect(collapse_button).not_to_be_visible() |
| 337 |
|
| 338 |
# open the mobile menu |
| 339 |
hamburger.click() |
| 340 |
|
| 341 |
expect(dialog).to_be_visible() |
| 342 |
expect(collapse_button).not_to_be_visible() |
| 343 |
|
| 344 |
_check_test_site(self.site_name, site_path, check_not_in_mobile) |
| 345 |
|
| 346 |
def test_no_collapse_sidebar_button( |
| 347 |
self, sphinx_build_factory: Callable, page: Page, url_base: str |
| 348 |
) -> None: |
| 349 |
"""No sidebar -> no collapse button.""" |
| 350 |
site_path = _build_test_site( |
| 351 |
self.site_name, sphinx_build_factory=sphinx_build_factory |
| 352 |
) |
| 353 |
assert site_path is not None |
| 354 |
|
| 355 |
def check_console_msg(msg: ConsoleMessage): |
| 356 |
if msg.text == "[PST] Error setting up collapse sidebar button": |
| 357 |
pytest.fail("Error setting up collapse sidebar button") |
| 358 |
|
| 359 |
page.on("console", check_console_msg) |
| 360 |
|
| 361 |
def check_no_collapse_sidebar_button(): |
| 362 |
response = page.goto( |
| 363 |
urljoin( |
| 364 |
url_base, |
| 365 |
f"playwright_tests/{self.site_name}/section2/no-sidebar.html", |
| 366 |
) |
| 367 |
) |
| 368 |
|
| 369 |
# Need to check that the response is 200 and need to wait for the |
| 370 |
# JavaScript to finish executing. Otherwise, if the page is a 404 or |
| 371 |
# if the JavaScript hasn't finished loading, then the test will |
| 372 |
# check for the button, not find it, and pass. |
| 373 |
assert response is not None and response.status == 200 |
| 374 |
page.wait_for_load_state("load") |
| 375 |
|
| 376 |
button = page.locator("#pst-collapse-sidebar-button") |
| 377 |
expect(button).not_to_be_attached() |
| 378 |
|
| 379 |
_check_test_site(self.site_name, site_path, check_no_collapse_sidebar_button) |