From b88a47f4a047c2c8e4b814f37799b60d8a6df884 Mon Sep 17 00:00:00 2001 From: Gregg Tavares Date: Thu, 14 Dec 2023 23:42:05 -0800 Subject: [PATCH] test examples --- test/puppeteer.js | 31 ++++++++++++++++++++++++++++--- test/src/js/example-inject.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 test/src/js/example-inject.js diff --git a/test/puppeteer.js b/test/puppeteer.js index 12bd14e..1ec0678 100755 --- a/test/puppeteer.js +++ b/test/puppeteer.js @@ -2,6 +2,7 @@ import puppeteer from 'puppeteer'; import path from 'path'; +import fs from 'fs'; import express from 'express'; import url from 'url'; const app = express(); @@ -23,6 +24,16 @@ function makePromiseInfo() { return info; } +const exampleInjectJS = fs.readFileSync('test/src/js/example-inject.js', {encoding: 'utf-8'}); + +function getExamples(port) { + return fs.readdirSync('examples') + .filter(f => f.endsWith('.html')) + .map(f => ({ + url: `http://localhost:${port}/examples/${f}`, + js: exampleInjectJS, + })); +} async function test(port) { const browser = await puppeteer.launch({ @@ -55,16 +66,30 @@ async function test(port) { waitingPromiseInfo.resolve(); }); - const urls = [ - `http://localhost:${port}/test/index.html?reporter=spec`, + const testPages = [ + {url: `http://localhost:${port}/test/index.html?reporter=spec` }, + ...getExamples(port), ]; - for (const url of urls) { + for (const {url, js} of testPages) { waitingPromiseInfo = makePromiseInfo(); + console.log(`===== [ ${url} ] =====`); + if (js) { + await page.evaluateOnNewDocument(js); + } await page.goto(url); + await page.waitForNetworkIdle(); + if (js) { + await page.evaluate(() => { + setTimeout(() => { + window.testsPromiseInfo.resolve(0); + }, 10); + }); + } await waitingPromiseInfo.promise; } + await browser.close(); server.close(); diff --git a/test/src/js/example-inject.js b/test/src/js/example-inject.js new file mode 100644 index 0000000..d002aa1 --- /dev/null +++ b/test/src/js/example-inject.js @@ -0,0 +1,35 @@ +/* global GPUAdapter */ +// eslint-disable-next-line strict +"use strict"; + +function makePromise() { + const info = {}; + const promise = new Promise((resolve, reject) => { + Object.assign(info, {resolve, reject}); + }); + info.promise = promise; + return info; +} + +window.testsPromiseInfo = makePromise(); + +window.addEventListener('error', (event) => { + console.error(event); + window.testsPromiseInfo.reject(1); +}); + +// eslint-disable-next-line no-lone-blocks +{ + GPUAdapter.prototype.requestDevice = (function (origFn) { + return async function (...args) { + const device = await origFn.call(this, args); + if (device) { + device.addEventListener('uncapturederror', function (e) { + console.error(e.error.message); + window.testsPromiseInfo.reject(1); + }); + } + return device; + }; + })(GPUAdapter.prototype.requestDevice); +}