diff --git a/test/integration/test_utils.mjs b/test/integration/test_utils.mjs index 1b1c5eb9e..401f23582 100644 --- a/test/integration/test_utils.mjs +++ b/test/integration/test_utils.mjs @@ -869,12 +869,37 @@ async function kbDeleteLastWord(page) { } } -async function kbFocusNext(page) { - const handle = await createPromise(page, resolve => { - window.addEventListener("focusin", resolve, { once: true }); - }); - await page.keyboard.press("Tab"); - await awaitPromise(handle); +async function kbFocusNext(page, selector = null) { + if (selector) { + await page.waitForSelector(selector, { visible: true }); + } + while (true) { + const handle = await page.evaluateHandle( + sel => [ + new Promise(resolve => { + const cb = e => { + if (!sel || document.querySelector(sel)?.contains(e.target)) { + window.removeEventListener("focusin", cb); + resolve(true); + } else { + resolve(false); + } + }; + window.addEventListener("focusin", cb); + }), + ], + selector + ); + + await page.keyboard.press("Tab"); + const result = await awaitPromise(handle); + if (result) { + break; + } + } + if (selector) { + await page.waitForSelector(`${selector}:focus`, { visible: true }); + } } async function kbFocusPrevious(page) { diff --git a/test/integration/thumbnail_view_spec.mjs b/test/integration/thumbnail_view_spec.mjs index 4df10495a..39185e176 100644 --- a/test/integration/thumbnail_view_spec.mjs +++ b/test/integration/thumbnail_view_spec.mjs @@ -167,21 +167,9 @@ describe("PDF Thumbnail View", () => { await waitForThumbnailVisible(page, 2); await waitForThumbnailVisible(page, 3); - await kbFocusNext(page); - await page.waitForSelector("#viewsManagerSelectorButton:focus", { - visible: true, - }); - - await kbFocusNext(page); - await page.waitForSelector("#viewsManagerStatusActionButton:focus", { - visible: true, - }); - - await kbFocusNext(page); - await page.waitForSelector( - `#thumbnailsView ${getThumbnailSelector(1)}:focus`, - { visible: true } - ); + await kbFocusNext(page, "#viewsManagerSelectorButton"); + await kbFocusNext(page, "#viewsManagerStatusActionButton"); + await kbFocusNext(page, `#thumbnailsView ${getThumbnailSelector(1)}`); await page.keyboard.press("ArrowDown"); await page.waitForSelector( @@ -260,11 +248,7 @@ describe("PDF Thumbnail View", () => { await enableMenuItems(page); // Focus the manage button - await kbFocusNext(page); - await kbFocusNext(page); - await page.waitForSelector("#viewsManagerStatusActionButton:focus", { - visible: true, - }); + await kbFocusNext(page, "#viewsManagerStatusActionButton"); // Press Enter to open the menu await page.keyboard.press("Enter"); @@ -292,11 +276,7 @@ describe("PDF Thumbnail View", () => { await enableMenuItems(page); // Focus the manage button - await kbFocusNext(page); - await kbFocusNext(page); - await page.waitForSelector("#viewsManagerStatusActionButton:focus", { - visible: true, - }); + await kbFocusNext(page, "#viewsManagerStatusActionButton"); // Press Space to open the menu await page.keyboard.press(" "); @@ -409,16 +389,11 @@ describe("PDF Thumbnail View", () => { }); // Press Tab to move to the manage button (should close views menu) - await page.keyboard.press("Tab"); + await kbFocusNext(page, "#viewsManagerStatusActionButton"); // Wait for views manager menu to be collapsed await waitForMenu(page, "#viewsManagerSelectorButton", false); - // Focus should be on manage button - await page.waitForSelector("#viewsManagerStatusActionButton:focus", { - visible: true, - }); - // Open manage menu with Space key await page.keyboard.press(" "); @@ -493,9 +468,7 @@ describe("PDF Thumbnail View", () => { await waitForThumbnailVisible(page, 1); // Focus the first thumbnail button - await kbFocusNext(page); - await kbFocusNext(page); - await kbFocusNext(page); + await kbFocusNext(page, getThumbnailSelector(1)); // Verify we're on the first thumbnail await page.waitForSelector(`${getThumbnailSelector(1)}:focus`, { @@ -503,10 +476,9 @@ describe("PDF Thumbnail View", () => { }); // Tab to checkbox - await kbFocusNext(page); - await page.waitForSelector( - `.thumbnail[page-number="1"] input[type="checkbox"]:focus`, - { visible: true } + await kbFocusNext( + page, + `.thumbnail[page-number="1"] input[type="checkbox"]` ); }) ); @@ -520,10 +492,10 @@ describe("PDF Thumbnail View", () => { await waitForThumbnailVisible(page, 2); // Navigate to first checkbox - await kbFocusNext(page); - await kbFocusNext(page); - await kbFocusNext(page); - await kbFocusNext(page); + await kbFocusNext( + page, + `.thumbnail[page-number="1"] input[type="checkbox"]` + ); // Verify first checkbox is focused await page.waitForSelector(