Skip to content

Commit eafb623

Browse files
committed
fix(desktop-browser): keep a ref whose name grew when its own menu opened
1 parent 0395eb3 commit eafb623

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎apps/desktop/src/main/browser-agent/page-functions.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,26 @@ describe('collectSnapshot', () => {
673673
expect(clickElement(ref)).toMatchObject({ error: 'stale' })
674674
})
675675

676+
it('keeps a connected ref whose name grew when its own submenu opened', () => {
677+
document.body.innerHTML = '<div role="button" tabindex="0">Products ▾<ul></ul></div>'
678+
const menu = visible(document.querySelector('[role="button"]') as HTMLDivElement)
679+
const ref = refFor(outlineOf(collectSnapshot()), 'Products ▾')
680+
681+
;(menu.querySelector('ul') as HTMLUListElement).innerHTML = ' <li>Starter</li> <li>Business</li>'
682+
683+
expect(clickElement(ref)).toMatchObject({ dispatched: true })
684+
})
685+
686+
it('invalidates a connected ref whose name changed to a longer, different word', () => {
687+
document.body.innerHTML = '<div role="listitem">Item 1</div>'
688+
const row = visible(document.querySelector('[role="listitem"]') as HTMLDivElement)
689+
const ref = refFor(outlineOf(collectSnapshot()), 'Item 1')
690+
691+
row.textContent = 'Item 12'
692+
693+
expect(clickElement(ref)).toMatchObject({ error: 'stale' })
694+
})
695+
676696
it('never recycles numeric refs across snapshots', () => {
677697
document.body.innerHTML = '<button>Pins</button>'
678698
visible(document.querySelector('button') as HTMLButtonElement)

‎apps/desktop/src/main/browser-agent/page-functions.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,16 @@ export function collectSnapshot(
897897
) {
898898
return false
899899
}
900-
if (nameFor(candidate) !== locator.name) return false
900+
// A still-mounted control whose own menu or disclosure opened (often by
901+
// the hover or click being validated) appends its new content to its
902+
// name; a recycled row replaces the name instead.
903+
const name = nameFor(candidate)
904+
const nameGrewInPlace =
905+
connected &&
906+
locator.name.length > 0 &&
907+
name.startsWith(locator.name) &&
908+
(locator.name.length >= 120 || /\s/.test(name.charAt(locator.name.length)))
909+
if (name !== locator.name && !nameGrewInPlace) return false
901910
const candidateAttributes = locatorAttributes(candidate)
902911
const attributes = connected ? connectedIdentityAttributes : stableAttributes
903912
const genericName =

0 commit comments

Comments
 (0)