"""Actual keyboard dispatch once through the original ContextMenu in three browser engines.""" from pathlib import Path import json from playwright.sync_api import sync_playwright,expect out=Path(__file__).resolve().parent;results=[] with sync_playwright() as p: for name in ['chromium','firefox','webkit']: b=getattr(p,name).launch();page=b.new_page(viewport={'width':390,'height':844},reduced_motion='reduce');errors=[];page.on('pageerror',lambda e:errors.append(str(e))) page.goto('http://127.0.0.1:18139/density-pilot');page.wait_for_selector('.raw-tree-row',state='attached');page.evaluate('document.fonts.ready');page.wait_for_timeout(350);expect(page.locator('#selection-request-count')).to_have_text('0') trigger=page.locator('.ctx-ellipsis').first;trigger.focus();expect(trigger).to_be_focused();page.keyboard.press('Enter');menu=page.locator('[role=menu]');expect(menu).to_be_focused() box=menu.bounding_box();assert box and box['x']>=0 and box['x']+box['width']<=390+.5 assert menu.evaluate('(e)=>e.matches(":popover-open")');page.keyboard.press('Enter');expect(menu).to_have_count(0);expect(page.locator('#selection-count')).to_have_text('800 selected');expect(page.locator('#selection-request-count')).to_have_text('1');expect(trigger).to_be_focused() trigger.focus();page.keyboard.press('Enter');expect(menu).to_be_focused();page.keyboard.press('Tab');page.keyboard.press('Tab');expect(menu.locator('[role=menuitem]').nth(1)).to_be_focused();page.keyboard.press('Enter');expect(menu).to_have_count(0);expect(page.locator('#selection-count')).to_have_text('0 selected');expect(page.locator('#selection-request-count')).to_have_text('2');expect(trigger).to_be_focused();assert not errors results.append({'engine':name,'version':b.version,'parentEnterDispatches':1,'selectedAfterParent':800,'focusedChildEnterDispatches':1,'selectedAfterChild':0,'totalLocalRequests':2,'focusReturned':True,'menuBox':box,'nativeTopLayer':True,'errors':errors});(out/'keyboard-menu-three-engines.json').write_text(json.dumps(results,indent=2)+'\n');b.close() (out/'keyboard-menu-three-engines.json').write_text(json.dumps(results,indent=2)+'\n');print('PASS3actual keyboard menus;parent+focused-child eachdispatchonce;focusreturn/native top layer')