26 lines
2.3 KiB
JavaScript
26 lines
2.3 KiB
JavaScript
/** Build and run real-browser computed-style checks; no jsdom style substitute.
|
|
* CHROMIUM_PATH=/path/to/chrome node scripts/test-task-popconfirm-browser.mjs
|
|
* --build-only creates a portable HTML fixture for an available browser.
|
|
* Chromium sandbox remains enabled. Runtime failure is NOT a passing test.
|
|
*/
|
|
import { build } from 'esbuild'
|
|
import { mkdtemp, writeFile, readFile } from 'node:fs/promises'
|
|
import { resolve } from 'node:path'
|
|
import { pathToFileURL } from 'node:url'
|
|
import { spawnSync } from 'node:child_process'
|
|
const dir = await mkdtemp(resolve('.tmp/task-popconfirm-browser-'))
|
|
await build({ entryPoints: ['scripts/task-popconfirm-browser-fixture.tsx'], bundle: true, platform: 'browser', format: 'iife', jsx: 'automatic', outfile: dir + '/fixture.js', loader: {'.svg':'dataurl', '.woff2':'dataurl', '.woff':'dataurl'}, logLevel: 'warning' })
|
|
const js = await readFile(dir + '/fixture.js', 'utf8'), css = await readFile(dir + '/fixture.css', 'utf8')
|
|
await writeFile(dir + '/index.html', `<!doctype html><html><head><meta charset="utf-8"><style>${css}</style></head><body><button id="anchor" style="position:absolute;left:30px;top:100px">Task</button><main id="root"></main><script>${js.replaceAll('</script', '<\\/script')}</script></body></html>`)
|
|
console.log('Browser fixture: ' + dir + '/index.html')
|
|
if (!process.argv.includes('--build-only')) {
|
|
const proc = spawnSync(process.env.CHROMIUM_PATH || 'chromium', ['--headless', '--disable-gpu', '--no-first-run', '--user-data-dir=' + dir + '/profile', '--virtual-time-budget=20000', '--dump-dom', pathToFileURL(dir + '/index.html').href], { encoding:'utf8', timeout:60000, maxBuffer:20*1024*1024 })
|
|
await writeFile(dir + '/browser.log', (proc.stderr || '') + '\n' + (proc.error || ''))
|
|
const match = proc.stdout?.match(/<pre id="result">([\s\S]*?)<\/pre>/)
|
|
if (proc.status !== 0 || !match) throw Error(`Browser did not finish; status=${proc.status}; see ${dir}/browser.log`)
|
|
const report = JSON.parse(match[1].replaceAll('"', '"').replaceAll('<', '<').replaceAll('>', '>').replaceAll('&', '&'))
|
|
await writeFile(dir + '/report.json', JSON.stringify(report, null, 2))
|
|
if (!report.pass) throw Error(report.error)
|
|
console.log(`Task confirmation real Chromium: ${report.results.length} assertions passed; ${dir}/report.json`)
|
|
}
|