helpers.js (2689B)
1 window.createRecordingCloseWatcher = (t, events, name, type, parentWatcher) => { 2 let watcher = null; 3 if (type === 'dialog') { 4 watcher = document.createElement('dialog'); 5 watcher.textContent = 'hello world'; 6 t.add_cleanup(() => watcher.remove()); 7 if (parentWatcher?.appendChild) { 8 parentWatcher.appendChild(watcher); 9 } else { 10 document.body.appendChild(watcher); 11 } 12 watcher.showModal(); 13 } else if (type === 'popover') { 14 watcher = document.createElement('div'); 15 watcher.setAttribute('popover', 'auto'); 16 watcher.textContent = 'hello world'; 17 t.add_cleanup(() => watcher.remove()); 18 if (parentWatcher?.appendChild) { 19 parentWatcher.appendChild(watcher); 20 } else { 21 document.body.appendChild(watcher); 22 } 23 watcher.showPopover(); 24 } else { 25 watcher = new CloseWatcher(); 26 t.add_cleanup(() => watcher.destroy()); 27 } 28 29 const prefix = name === undefined ? '' : name + ' '; 30 watcher.addEventListener('cancel', e => { 31 const cancelable = e.cancelable ? '[cancelable=true]' : '[cancelable=false]'; 32 events.push(prefix + 'cancel' + cancelable); 33 }); 34 watcher.addEventListener('close', () => { 35 events.push(prefix + 'close'); 36 }); 37 38 return watcher; 39 }; 40 41 window.createBlessedRecordingCloseWatcher = async (t, events, name, type, parentWatcher) => { 42 await maybeTopLayerBless(parentWatcher); 43 return createRecordingCloseWatcher(t, events, name, type, parentWatcher); 44 }; 45 46 window.destroyCloseWatcher = (watcher) => { 47 if (watcher instanceof HTMLElement) { 48 watcher.remove(); 49 } else { 50 watcher.destroy(); 51 } 52 }; 53 54 window.sendEscKey = () => { 55 // Esc is \uE00C, *not* \uu001B; see https://w3c.github.io/webdriver/#keyboard-actions. 56 // 57 // It's important to target document.body, and not any element that might stop receiving events 58 // if a popover or dialog is making that element inert. 59 return test_driver.send_keys(document.body, '\uE00C'); 60 }; 61 62 // For now, we always use the Esc keypress as our close request. In 63 // theory, in the future, we could add a WebDriver command or similar 64 // for the close request, which would allow different tests on platforms 65 // with different close requests. In that case, we'd update this 66 // function, but not update the sendEscKey function above. 67 window.sendCloseRequest = window.sendEscKey; 68 69 window.maybeTopLayerBless = (watcher) => { 70 if (watcher instanceof HTMLElement) { 71 return blessTopLayer(watcher); 72 } 73 return test_driver.bless(); 74 }; 75 76 window.waitForPotentialCloseEvent = () => { 77 // CloseWatchers fire close events synchronously, but dialog elements wait 78 // for a rAF before firing them. 79 return new Promise(requestAnimationFrame); 80 };