2023-07-13 03:52:06 +08:00
|
|
|
const puppeteer = require('puppeteer');
|
|
|
|
const { spawn } = require('child_process');
|
|
|
|
const res = spawn("python", ["-m", "http.server", "8000"], { shell: true });
|
|
|
|
|
|
|
|
async function timeout(time) {
|
|
|
|
return new Promise((resolve) => setTimeout(resolve, time));
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanup(err) {
|
|
|
|
res.kill();
|
|
|
|
if(err != null) {
|
|
|
|
console.error(err);
|
2023-08-01 01:35:50 +08:00
|
|
|
process.exit(1);
|
2023-07-13 03:52:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function waitForText(selector, text) {
|
|
|
|
let n = 0;
|
|
|
|
let ready = false;
|
|
|
|
while (n < 10) {
|
|
|
|
const res = await (await selector.getProperty("textContent")).jsonValue();
|
|
|
|
console.log(`waiting for text ${text} got ${res}`);
|
|
|
|
if(res == text) {
|
|
|
|
ready = true;
|
|
|
|
break
|
|
|
|
}
|
2023-08-01 01:35:50 +08:00
|
|
|
await timeout(2000);
|
2023-07-13 03:52:06 +08:00
|
|
|
n += 1
|
|
|
|
}
|
|
|
|
return ready;
|
|
|
|
}
|
|
|
|
|
|
|
|
puppeteer.launch({ headless: false, args: ["--enable-unsafe-webgpu"]}).then(async browser => {
|
|
|
|
const page = await browser.newPage();
|
|
|
|
page.on("console", message => console.log(`message from console ${message.text()}`))
|
|
|
|
.on("pageerror", ({ message }) => console.log(`error from page ${message}`))
|
|
|
|
|
|
|
|
const res = await page.goto("http://localhost:8000/examples/webgpu/index.html");
|
|
|
|
if(res.status() != 200) throw new Error("Failed to load page");
|
|
|
|
const textSelector = await page.waitForSelector("#result");
|
|
|
|
const buttonSelector = await page.waitForSelector("input[type=button]");
|
|
|
|
const ready = await waitForText(textSelector, "ready");
|
|
|
|
if(!ready) throw new Error("Failed to load page");
|
|
|
|
await buttonSelector.evaluate(e => e.click());
|
|
|
|
const done = await waitForText(textSelector, "hen");
|
|
|
|
if(!done) throw new Error("failed to get hen");
|
|
|
|
browser.close();
|
|
|
|
cleanup(null);
|
|
|
|
}).catch(err => {
|
|
|
|
cleanup(err);
|
|
|
|
});
|