diff --git a/example/browser/index.html b/example/browser/index.html index 57587bf..71169d0 100644 --- a/example/browser/index.html +++ b/example/browser/index.html @@ -39,7 +39,7 @@

Choose an experiment

Choose 1 to 10,000 values and a wait of 0 to 1,000 ms.

-
+

What the C++ does

diff --git a/example/browser/workbench.mjs b/example/browser/workbench.mjs index 9fa11af..bf0cb29 100644 --- a/example/browser/workbench.mjs +++ b/example/browser/workbench.mjs @@ -123,6 +123,8 @@ const start = event => { document.body.appendChild(frame) } document.querySelector('form').addEventListener('submit', start) +// index.html disables Start until this handler is attached, so that no click submits the form natively. +if (!workerBuild) element('start').disabled = false element('cancel').addEventListener('click', () => { if (!active) return element('cancel').disabled = true diff --git a/test/browser/pages.mjs b/test/browser/pages.mjs index 0ab64e1..ede39c4 100644 --- a/test/browser/pages.mjs +++ b/test/browser/pages.mjs @@ -55,6 +55,24 @@ const wait = async expression => { } throw Error(`Timed out: ${expression}; state=${JSON.stringify(await evaluate('window.cobaltReport'))}`) } +// The page's module, workbench.mjs, attaches the form's handler; until it has run, the Start button +// is disabled, so that an early click cannot submit the form natively and reload the page (which a +// driver on a loaded machine once did: its fill and click landed before the module, and the page it +// then watched was a fresh one, status ready, run 0). The server below serves the module late, so +// that every navigation crosses that window; loaded() checks the button there and waits the module. +const moduleDelay = 1000 +const loaded = async () => { + const until = Date.now() + 15000 + while (Date.now() < until) { + const state = await evaluate(`({started: !!document.querySelector('#start'), + disabled: document.querySelector('#start')?.disabled, + module: window.cobaltReport !== undefined})`) + if (state.module) return + if (state.started) assert.equal(state.disabled, true, 'Start is enabled before the page module ran') + await delay(20) + } + throw Error('Timed out: the page module did not run') +} const click = async selector => { const point = await evaluate(`(() => { const r = document.querySelector(${JSON.stringify(selector)}).getBoundingClientRect(); return {x:r.x+r.width/2,y:r.y+r.height/2}; })()`) await send('Input.dispatchMouseEvent', {type: 'mousePressed', button: 'left', clickCount: 1, ...point}) @@ -77,7 +95,10 @@ const terminal = async status => { } try { - server = createServer((request, response) => { + server = createServer(async (request, response) => { + if (request.url.startsWith('/workbench.mjs') || request.url.startsWith('/unisolated/workbench.mjs')) { + await delay(moduleDelay) + } try { const requested = decodeURIComponent(new URL(request.url, 'http://localhost').pathname) const unisolated = requested.startsWith('/unisolated/') @@ -159,7 +180,7 @@ try { await send('Emulation.setDeviceMetricsOverride', {width: 1240, height: 900, deviceScaleFactor: 1, mobile: false}) const page = `${base}/index.html?program=/build/${basename(program)}&variant=${variant}&threads=${multi ? 'multi' : 'single'}&kind=${workers ? 'spsc' : 'timer'}` await send('Page.navigate', {url: page}) - await wait('!!document.querySelector("#start")') + await loaded() if (workers) { await wait('window.cobaltReport?.status === "unavailable"') assert.match(await evaluate('document.querySelector("#explanation").textContent'), /can lose values/) @@ -250,7 +271,7 @@ try { // Actual response headers, rather than a mocked crossOriginIsolated value. if (multi) { await send('Page.navigate', {url: page.replace('/index.html?', '/unisolated/index.html?')}) - await wait('!!document.querySelector("#start")') + await loaded() assert.equal(await evaluate('crossOriginIsolated'), false) await click('#start') await wait('!document.querySelector("#error").hidden') @@ -259,7 +280,7 @@ try { } if (variant === 'jspi') { await send('Page.navigate', {url: page}) - await wait('!!document.querySelector("#start")') + await loaded() await evaluate('delete WebAssembly.Suspending') await click('#start') await wait('!document.querySelector("#error").hidden')