mirror of
https://github.com/SerenityOS/serenity
synced 2026-05-13 18:37:37 +02:00
For example, in the following abbreviated test HTML:
<span>some text</span>
<script>println("whf")</script>
We would have to craft the expectation file to include the "some text"
segment, usually with some leading whitespace. This is a bit annoying,
and makes it difficult to manually craft expectation files.
So instead of comparing the expectation against the entire DOM inner
text, we now send the inner text of just the <pre> element containing
the test output when we invoke `internals.signalTextTestIsDone`.
(cherry picked from commit bf668696de9a68bbcd6adfeaac809a475da015dc;
amended a bit but not as much as one might think -- see PR for details)
101 lines
2.4 KiB
JavaScript
101 lines
2.4 KiB
JavaScript
var __outputElement = null;
|
|
let __alreadyCalledTest = false;
|
|
let __originalURL = null;
|
|
function __preventMultipleTestFunctions() {
|
|
if (__alreadyCalledTest) {
|
|
throw new Error("You must only call test() or asyncTest() once per page");
|
|
}
|
|
__alreadyCalledTest = true;
|
|
}
|
|
|
|
if (globalThis.internals === undefined) {
|
|
internals = {
|
|
signalTextTestIsDone: function () {},
|
|
spoofCurrentURL: function (url) {},
|
|
};
|
|
}
|
|
|
|
function __finishTest() {
|
|
if (__originalURL) {
|
|
internals.spoofCurrentURL(__originalURL);
|
|
}
|
|
internals.signalTextTestIsDone(__outputElement.innerText);
|
|
}
|
|
|
|
function spoofCurrentURL(url) {
|
|
if (__originalURL === null) {
|
|
__originalURL = document.location.href;
|
|
}
|
|
internals.spoofCurrentURL(url);
|
|
}
|
|
|
|
function println(s) {
|
|
__outputElement.appendChild(document.createTextNode(s + "\n"));
|
|
}
|
|
|
|
function printElement(e) {
|
|
let element_string = `<${e.nodeName} `;
|
|
if (e.id) element_string += `id="${e.id}" `;
|
|
element_string += ">";
|
|
println(element_string);
|
|
}
|
|
|
|
function animationFrame() {
|
|
const { promise, resolve } = Promise.withResolvers();
|
|
requestAnimationFrame(resolve);
|
|
return promise;
|
|
}
|
|
|
|
function timeout(ms) {
|
|
const { promise, resolve } = Promise.withResolvers();
|
|
setTimeout(resolve, ms);
|
|
return promise;
|
|
}
|
|
|
|
const __testErrorHandlerController = new AbortController();
|
|
window.addEventListener(
|
|
"error",
|
|
event => {
|
|
println(`Uncaught Error In Test: ${event.message}`);
|
|
__finishTest();
|
|
},
|
|
{ signal: __testErrorHandlerController.signal }
|
|
);
|
|
|
|
function removeTestErrorHandler() {
|
|
__testErrorHandlerController.abort();
|
|
}
|
|
|
|
document.addEventListener("DOMContentLoaded", function () {
|
|
__outputElement = document.createElement("pre");
|
|
__outputElement.setAttribute("id", "out");
|
|
document.body.appendChild(__outputElement);
|
|
});
|
|
|
|
function test(f) {
|
|
__preventMultipleTestFunctions();
|
|
document.addEventListener("DOMContentLoaded", f);
|
|
window.addEventListener("load", () => {
|
|
__finishTest();
|
|
});
|
|
}
|
|
|
|
function asyncTest(f) {
|
|
const done = () => {
|
|
__preventMultipleTestFunctions();
|
|
__finishTest();
|
|
};
|
|
document.addEventListener("DOMContentLoaded", () => {
|
|
f(done);
|
|
});
|
|
}
|
|
|
|
function promiseTest(f) {
|
|
document.addEventListener("DOMContentLoaded", () => {
|
|
f().then(() => {
|
|
__preventMultipleTestFunctions();
|
|
__finishTest();
|
|
});
|
|
});
|
|
}
|