Skip to content

Commit

Permalink
test examples
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Dec 15, 2023
1 parent b5a601e commit b88a47f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
31 changes: 28 additions & 3 deletions test/puppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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({
Expand Down Expand Up @@ -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();

Expand Down
35 changes: 35 additions & 0 deletions test/src/js/example-inject.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit b88a47f

Please sign in to comment.