diff --git a/scroll-to-text-fragment/sequential-focus.html b/scroll-to-text-fragment/sequential-focus.html index 30b467f1ecd1a5..79b013ca229c66 100644 --- a/scroll-to-text-fragment/sequential-focus.html +++ b/scroll-to-text-fragment/sequential-focus.html @@ -24,21 +24,21 @@ const link = document.getElementById('link'); const nextButton = document.getElementById('next-button'); - window.addEventListener('hashchange', async () => { - // Assert that the link element does not have focus initially - assert_not_equals(document.activeElement, link, 'Link element should not have focus initially'); + const hashChange = new Promise(r => window.addEventListener('hashchange', r, { once: true })); - // Simulate pressing TAB to shift focus to the next element - await test_driver.send_keys(document.body, '\uE004'); // '\uE004' is the WebDriver key code for TAB + // Simulate a same-document load to the text fragment + location.hash = '#some-hash-to-trigger-hashchange:~:text=foo'; - // Assert that the next focusable element (button) has focus - assert_equals(document.activeElement, nextButton, 'Next focusable element should have focus after pressing TAB'); + await hashChange; - t.done(); - }); + // Assert that the link element does not have focus initially + assert_not_equals(document.activeElement, link, 'Link element should not have focus initially'); - // Simulate a same-document load to the text fragment - location.hash = '#some-hash-to-trigger-hashchange:~:text=foo'; + // Simulate pressing TAB to shift focus to the next element + await test_driver.send_keys(document.body, '\uE004'); // '\uE004' is the WebDriver key code for TAB + + // Assert that the next focusable element (button) has focus + assert_equals(document.activeElement, nextButton, 'Next focusable element should have focus after pressing TAB'); }, 'Text Fragment focus behavior');