|
16 | 16 | from urllib.parse import urljoin
|
17 | 17 |
|
18 | 18 | import pytest
|
19 |
| -from playwright.sync_api import sync_playwright |
20 | 19 |
|
21 | 20 | MAX_TEST_TIME = 30 # Number of seconds allowed for checking a testing condition
|
22 | 21 | TEST_TIME_INCREMENT = 0.25 # 1/4 second, the length of each iteration
|
|
147 | 146 |
|
148 | 147 |
|
149 | 148 | @pytest.mark.parametrize("example", EXAMPLES)
|
150 |
| -def test_examples(example, http_server): |
| 149 | +def test_examples(example, http_server, page): |
151 | 150 |
|
152 | 151 | base_url = http_server
|
153 | 152 | example_path = urljoin(base_url, TEST_PARAMS[example]["file"])
|
154 | 153 |
|
155 |
| - # Invoke playwright |
156 |
| - with sync_playwright() as p: |
157 |
| - browser = p.chromium.launch() |
158 |
| - page = browser.new_page() |
159 |
| - page.goto(example_path) |
| 154 | + page.goto(example_path, wait_until="commit") |
160 | 155 |
|
161 |
| - # STEP 1: Check page title proper initial loading of the example page |
| 156 | + content = page.text_content("*") |
| 157 | + title = page.title() |
162 | 158 |
|
163 |
| - expected_title = TEST_PARAMS[example]["title"] |
164 |
| - if isinstance(expected_title, list): |
165 |
| - # One example's title changes so expected_title is a list of possible |
166 |
| - # titles in that case |
167 |
| - assert page.title() in expected_title # nosec |
168 |
| - else: |
169 |
| - assert page.title() == expected_title # nosec |
| 159 | + # STEP 1: Check page title proper initial loading of the example page |
| 160 | + expected_title = TEST_PARAMS[example]["title"] |
| 161 | + if isinstance(expected_title, list): |
| 162 | + # One example's title changes so expected_title is a list of possible |
| 163 | + # titles in that case |
| 164 | + assert title in expected_title # nosec |
| 165 | + else: |
| 166 | + assert title == expected_title # nosec |
170 | 167 |
|
171 |
| - # STEP 2: Test that pyodide is loading via messages displayed during loading |
| 168 | + # STEP 2: Test that pyodide is loading via messages displayed during loading |
172 | 169 |
|
173 |
| - pyodide_loading = False # Flag to be set to True when condition met |
| 170 | + pyodide_loading = False # Flag to be set to True when condition met |
174 | 171 |
|
175 |
| - for _ in range(TEST_ITERATIONS): |
176 |
| - time.sleep(TEST_TIME_INCREMENT) |
177 |
| - content = page.text_content("*") |
178 |
| - for message in LOADING_MESSAGES: |
179 |
| - if message in content: |
180 |
| - pyodide_loading = True |
181 |
| - if pyodide_loading: |
182 |
| - break |
| 172 | + for _ in range(TEST_ITERATIONS): |
| 173 | + for message in LOADING_MESSAGES: |
| 174 | + if message in content: |
| 175 | + pyodide_loading = True |
| 176 | + if pyodide_loading: |
| 177 | + break |
| 178 | + content = page.text_content("*") |
| 179 | + time.sleep(TEST_TIME_INCREMENT) |
183 | 180 |
|
184 |
| - assert pyodide_loading # nosec |
| 181 | + assert pyodide_loading # nosec |
185 | 182 |
|
186 |
| - # STEP 3: |
187 |
| - # Assert that rendering inserts data into the page as expected: search the |
188 |
| - # DOM from within the timing loop for a string that is not present in the |
189 |
| - # initial markup but should appear by way of rendering |
| 183 | + # STEP 3: |
| 184 | + # Assert that rendering inserts data into the page as expected: search the |
| 185 | + # DOM from within the timing loop for a string that is not present in the |
| 186 | + # initial markup but should appear by way of rendering |
190 | 187 |
|
191 |
| - re_sub_content = re.compile(TEST_PARAMS[example]["pattern"]) |
192 |
| - py_rendered = False # Flag to be set to True when condition met |
| 188 | + re_sub_content = re.compile(TEST_PARAMS[example]["pattern"]) |
| 189 | + py_rendered = False # Flag to be set to True when condition met |
193 | 190 |
|
194 |
| - for _ in range(TEST_ITERATIONS): |
195 |
| - time.sleep(TEST_TIME_INCREMENT) |
196 |
| - content = page.inner_html("*") |
197 |
| - if re_sub_content.search(content): |
198 |
| - py_rendered = True |
199 |
| - break |
| 191 | + for _ in range(TEST_ITERATIONS): |
| 192 | + time.sleep(TEST_TIME_INCREMENT) |
| 193 | + content = page.inner_html("*") |
| 194 | + if re_sub_content.search(content): |
| 195 | + py_rendered = True |
| 196 | + break |
200 | 197 |
|
201 |
| - assert py_rendered # nosec |
202 |
| - |
203 |
| - browser.close() |
| 198 | + assert py_rendered # nosec |
0 commit comments