2023-12-13 19:03:36 +00:00
|
|
|
import { test, expect } from '@playwright/test';
|
2022-06-06 20:22:51 +00:00
|
|
|
|
|
|
|
test.beforeEach(async ({ page }) => {
|
|
|
|
await page.goto('http://localhost:8000/');
|
|
|
|
});
|
|
|
|
|
|
|
|
const MAX_TIMEOUT_FOR_TEST = 300_000;
|
|
|
|
|
2024-07-31 21:16:05 +00:00
|
|
|
test.describe('Wasm example', () => {
|
2023-12-13 20:20:24 +00:00
|
|
|
test('Wait for success', async ({ page }, testInfo) => {
|
2022-06-06 20:22:51 +00:00
|
|
|
let start = new Date().getTime();
|
|
|
|
|
|
|
|
let found = false;
|
|
|
|
while (new Date().getTime() - start < MAX_TIMEOUT_FOR_TEST) {
|
2023-12-13 20:20:24 +00:00
|
|
|
let msg = await promiseWithTimeout(100, onConsole(page), "no log found");
|
2022-06-06 20:22:51 +00:00
|
|
|
if (msg.includes("no log found")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
console.log(msg);
|
|
|
|
if (msg.includes("Test successful")) {
|
|
|
|
let prefix = process.env.SCREENSHOT_PREFIX === undefined ? "screenshot" : process.env.SCREENSHOT_PREFIX;
|
2023-12-13 20:20:24 +00:00
|
|
|
await page.screenshot({ path: `${prefix}-${testInfo.project.name}.png`, fullPage: true });
|
2022-06-06 20:22:51 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(found).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2023-12-13 20:20:24 +00:00
|
|
|
function onConsole(page) {
|
2022-06-06 20:22:51 +00:00
|
|
|
return new Promise(resolve => {
|
|
|
|
page.on('console', msg => resolve(msg.text()));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-13 20:20:24 +00:00
|
|
|
async function promiseWithTimeout(timeLimit, task, failureValue) {
|
2022-06-06 20:22:51 +00:00
|
|
|
let timeout;
|
2023-12-13 20:20:24 +00:00
|
|
|
const timeoutPromise = new Promise((resolve, reject) => {
|
2022-06-06 20:22:51 +00:00
|
|
|
timeout = setTimeout(() => {
|
2023-12-13 20:20:24 +00:00
|
|
|
resolve(failureValue);
|
|
|
|
}, timeLimit);
|
2022-06-06 20:22:51 +00:00
|
|
|
});
|
2023-12-13 20:20:24 +00:00
|
|
|
const response = await Promise.race([task, timeoutPromise]);
|
2022-06-06 20:22:51 +00:00
|
|
|
if (timeout) {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|