testing wait alternatives

This commit is contained in:
Karsten Hassel
2022-01-13 00:13:29 +01:00
parent 65fada7ef1
commit 42b80b18f8
4 changed files with 52 additions and 27 deletions

View File

@@ -23,10 +23,12 @@ exports.stopApplication = function () {
};
exports.getDocument = function (callback, ms) {
if (!ms || ms < 1000) ms = 1000;
const url = "http://" + (config.address || "localhost") + ":" + (config.port || "8080");
jsdom.JSDOM.fromURL(url, { resources: "usable", runScripts: "dangerously" }).then((dom) => {
dom.window.name = "jsdom";
dom.window.onload = function () {
global.MutationObserver = dom.window.MutationObserver;
global.document = dom.window.document;
setTimeout(() => {
callback();
@@ -34,3 +36,23 @@ exports.getDocument = function (callback, ms) {
};
});
};
exports.waitForElement = function(selector) {
return new Promise(resolve => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}
const observer = new MutationObserver(() => {
if (document.querySelector(selector)) {
resolve(document.querySelector(selector));
observer.disconnect();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
};