1
- """Build minimal test sites with sphinx_build_factory and test them with Playwright."""
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
+ """
2
6
3
7
from pathlib import Path
4
8
from typing import Callable
21
25
test_sites_dir = repo_path / "docs" / "_build" / "html" / "playwright_tests"
22
26
23
27
28
+ # ------------------------- Helper functions -------------------------
24
29
def _is_overflowing (element ):
25
30
"""Check if an element is being shortened via CSS due to text-overflow property.
26
31
@@ -42,7 +47,8 @@ def _build_test_site(site_name: str, sphinx_build_factory: Callable) -> None:
42
47
43
48
def _check_test_site (site_name : str , site_path : Path , test_func : Callable ):
44
49
"""Make the built test site available to Playwright, then run `test_func` on it."""
45
- test_sites_dir .mkdir (exist_ok = True )
50
+ # Need to ensure parent directories exist in CI
51
+ test_sites_dir .mkdir (exist_ok = True , parents = True )
46
52
symlink_path = test_sites_dir / site_name
47
53
try :
48
54
symlink_path .symlink_to (site_path , True )
@@ -55,65 +61,37 @@ def _check_test_site(site_name: str, site_path: Path, test_func: Callable):
55
61
test_sites_dir .rmdir ()
56
62
57
63
58
- def test_version_switcher_highlighting (page : Page , url_base : str ) -> None :
64
+ # ------------------------- Test functions: style -------------------------
65
+ def test_version_switcher_highlighting (
66
+ sphinx_build_factory : Callable , page : Page , url_base : str
67
+ ) -> None :
59
68
"""
60
69
In sidebar and topbar - version switcher should apply highlight color to currently
61
70
selected version.
62
71
"""
63
- page .goto (url = url_base )
64
- # no need to include_hidden here ↓↓↓, we just need to get the active version name
65
- button = page .get_by_role ("button" ).filter (has_text = "dev" )
66
- active_version_name = button .get_attribute ("data-active-version-name" )
67
- # here we do include_hidden, so sidebar & topbar menus should each have a
68
- # matching entry:
69
- entries = page .get_by_role ("option" , include_hidden = True ).filter (
70
- has_text = active_version_name
71
- )
72
- assert entries .count () == 2
73
- # make sure they're highlighted
74
- for entry in entries .all ():
75
- light_mode = "rgb(10, 125, 145)" # pst-color-primary
76
- # dark_mode = "rgb(63, 177, 197)"
77
- expect (entry ).to_have_css ("color" , light_mode )
78
-
79
-
80
- def test_breadcrumb_expansion (page : Page , url_base : str ) -> None :
81
- """Test breadcrumb text-overflow."""
82
- # wide viewport width → no truncation
83
- page .set_viewport_size ({"width" : 1440 , "height" : 720 })
84
- page .goto (urljoin (url_base , "community/topics/config.html" ))
85
- expect (page .get_by_label ("Breadcrumb" ).get_by_role ("list" )).to_contain_text (
86
- "Update Sphinx configuration during the build"
87
- )
88
- el = page .get_by_text ("Update Sphinx configuration during the build" ).nth (1 )
89
- expect (el ).to_have_css ("overflow-x" , "hidden" )
90
- expect (el ).to_have_css ("text-overflow" , "ellipsis" )
91
- assert not _is_overflowing (el )
92
- # narrow viewport width → truncation
93
- page .set_viewport_size ({"width" : 150 , "height" : 720 })
94
- assert _is_overflowing (el )
95
-
96
-
97
- def test_breadcrumbs_everywhere (
98
- sphinx_build_factory : Callable , page : Page , url_base : str
99
- ) -> None :
100
- """Test breadcrumbs truncate properly when placed in various parts of the layout."""
101
- site_name = "breadcrumbs"
72
+ site_name = "version_switcher"
102
73
site_path = _build_test_site (site_name , sphinx_build_factory = sphinx_build_factory )
103
74
104
- def check_breadcrumb_truncation ():
105
- page .goto (
106
- urljoin (url_base , f"playwright_tests/{ site_name } /hansel/gretel/house.html" )
75
+ def check_version_switcher_highlighting ():
76
+ page .goto (urljoin (url_base , f"playwright_tests/{ site_name } /index.html" ))
77
+ # no need to include_hidden here ↓↓↓, we just need to get the active
78
+ # version name
79
+ button = page .get_by_role ("button" ).filter (has_text = "dev" )
80
+ active_version_name = button .get_attribute ("data-active-version-name" )
81
+
82
+ # here we do include_hidden, since we are not adding this in the sidebar
83
+ # we should only get one entry
84
+ entries = page .get_by_role ("option" , include_hidden = True ).filter (
85
+ has_text = active_version_name
107
86
)
108
- # sidebar should overflow
109
- text = "In the oven with my sister, so hot right now. Soooo. Hotttt."
110
- el = page .locator ("#main-content" ).get_by_text (text ).last
111
- assert _is_overflowing (el )
112
- # footer containers never trigger ellipsis overflow because min-width is content
113
- el = page .locator (".footer-items__center > .footer-item" )
114
- assert not _is_overflowing (el )
87
+ assert entries .count () == 1
88
+ # make sure they're highlighted
89
+ for entry in entries .all ():
90
+ light_mode = "rgb(10, 125, 145)" # pst-color-primary
91
+ # dark_mode = "rgb(63, 177, 197)"
92
+ expect (entry ).to_have_css ("color" , light_mode )
115
93
116
- _check_test_site (site_name , site_path , check_breadcrumb_truncation )
94
+ _check_test_site (site_name , site_path , check_version_switcher_highlighting )
117
95
118
96
119
97
def test_colors (sphinx_build_factory : Callable , page : Page , url_base : str ) -> None :
@@ -143,3 +121,53 @@ def check_colors():
143
121
expect (el ).to_have_css ("color" , hover_color )
144
122
145
123
_check_test_site (site_name , site_path , check_colors )
124
+
125
+
126
+ # ------------------------- Test functions: layout & components -----------------------
127
+
128
+
129
+ def test_breadcrumb_expansion (
130
+ sphinx_build_factory : Callable , page : Page , url_base : str
131
+ ) -> None :
132
+ """Test breadcrumb text-overflow."""
133
+ site_name = "breadcrumbs"
134
+ site_path = _build_test_site (site_name , sphinx_build_factory = sphinx_build_factory )
135
+
136
+ def check_breadcrumb_expansion ():
137
+ # wide viewport width → no truncation
138
+ page .set_viewport_size ({"width" : 1440 , "height" : 720 })
139
+ page .goto (urljoin (url_base , "community/topics/config.html" ))
140
+ expect (page .get_by_label ("Breadcrumb" ).get_by_role ("list" )).to_contain_text (
141
+ "Update Sphinx configuration during the build"
142
+ )
143
+ el = page .get_by_text ("Update Sphinx configuration during the build" ).nth (1 )
144
+ expect (el ).to_have_css ("overflow-x" , "hidden" )
145
+ expect (el ).to_have_css ("text-overflow" , "ellipsis" )
146
+ assert not _is_overflowing (el )
147
+ # narrow viewport width → truncation
148
+ page .set_viewport_size ({"width" : 150 , "height" : 720 })
149
+ assert _is_overflowing (el )
150
+
151
+ _check_test_site (site_name , site_path , check_breadcrumb_expansion )
152
+
153
+
154
+ def test_breadcrumbs_everywhere (
155
+ sphinx_build_factory : Callable , page : Page , url_base : str
156
+ ) -> None :
157
+ """Test breadcrumbs truncate properly when placed in various parts of the layout."""
158
+ site_name = "breadcrumbs"
159
+ site_path = _build_test_site (site_name , sphinx_build_factory = sphinx_build_factory )
160
+
161
+ def check_breadcrumb_truncation ():
162
+ page .goto (
163
+ urljoin (url_base , f"playwright_tests/{ site_name } /hansel/gretel/house.html" )
164
+ )
165
+ # sidebar should overflow
166
+ text = "In the oven with my sister, so hot right now. Soooo. Hotttt."
167
+ el = page .locator ("#main-content" ).get_by_text (text ).last
168
+ assert _is_overflowing (el )
169
+ # footer containers never trigger ellipsis overflow because min-width is content
170
+ el = page .locator (".footer-items__center > .footer-item" )
171
+ assert not _is_overflowing (el )
172
+
173
+ _check_test_site (site_name , site_path , check_breadcrumb_truncation )
0 commit comments