Skip to content

Commit 255bc06

Browse files
authored
BUG - Use Playwright expect() for code block tab stop tests (#2160)
Maybe fixes #2158. This takes advantage of Playwright's ability to wait for a condition to be true rather than hard-coding manual wait times with the `page.wait_for_timeout()` function. From the Playwright docs: > Playwright includes assertions that will wait until the expected condition is met. Using these assertions allows making the tests non-flaky and resilient. > -[Writing Tests: Assertions](https://playwright.dev/python/docs/writing-tests#assertions)
1 parent 237b9b0 commit 255bc06

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

tests/test_a11y.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,15 @@ def test_code_block_tab_stop(page: Page, url_base: str) -> None:
235235

236236
page.set_viewport_size({"width": 400, "height": 720})
237237

238-
# Resize handler is debounced with 300 ms wait time
239-
page.wait_for_timeout(301)
240-
241-
# Narrow viewport, content overflows and code block should be a tab stop
238+
# Narrow viewport, content overflows ...
242239
assert code_block.evaluate("el => el.scrollWidth > el.clientWidth") is True
243-
assert code_block.evaluate("el => el.tabIndex") == 0
240+
241+
# ... and code block should be a tab stop.
242+
#
243+
# Note: expect() will wait until the expect condition is true (up to the
244+
# test timeout limit). This is important because the resize handler is
245+
# debounced.
246+
expect(code_block).to_have_attribute("tabindex", "0")
244247

245248

246249
@pytest.mark.a11y
@@ -269,17 +272,20 @@ def test_notebook_ipywidget_output_tab_stop(page: Page, url_base: str) -> None:
269272
ipywidget = page.locator("css=.jp-RenderedHTMLCommon").first
270273

271274
# As soon as the ipywidget is attached to the page it should trigger the
272-
# mutation observer, which has a 300 ms debounce
275+
# mutation observer
273276
ipywidget.wait_for(state="attached")
274-
page.wait_for_timeout(301)
275277

276278
# At the default viewport size (1280 x 720) the data table inside the
277279
# ipywidget has overflow...
278280
assert ipywidget.evaluate("el => el.scrollWidth > el.clientWidth") is True
279281

280-
# ...and so our js code on the page should make it keyboard-focusable
281-
# (tabIndex = 0)
282-
assert ipywidget.evaluate("el => el.tabIndex") == 0
282+
# ... and so our js code on the page should make it keyboard-focusable
283+
# (tabIndex=0).
284+
#
285+
# Note: expect() will wait until the expect condition is true (up to the
286+
# test timeout limit). This is important because the mutation callback that
287+
# sets tabIndex=0 is debounced.
288+
expect(ipywidget).to_have_attribute("tabindex", "0")
283289

284290

285291
def test_breadcrumb_expansion(page: Page, url_base: str) -> None:

0 commit comments

Comments
 (0)