From fc10c250c95a698163b765e9b91ab647da89e7aa Mon Sep 17 00:00:00 2001 From: nsreed Date: Wed, 2 Jun 2021 19:23:47 -0400 Subject: [PATCH] add load-browser-scripts utility (#1077) --- test/panic/util/load-browser-scripts.js | 41 +++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 test/panic/util/load-browser-scripts.js diff --git a/test/panic/util/load-browser-scripts.js b/test/panic/util/load-browser-scripts.js new file mode 100644 index 00000000..d40f77e4 --- /dev/null +++ b/test/panic/util/load-browser-scripts.js @@ -0,0 +1,41 @@ +module.exports = { + /** + * Loads scripts in PANIC clients + * @param browsers a Panic.ClientList of browsers + * @param paths an array of paths to desired .js files + * @returns Promise which will resolve when all browsers have loaded all dependencies + */ + loadBrowserScripts: function (browsers, paths) { + var promises = []; + browsers.each(function (client, id) { + promises.push(client.run(function (test) { + test.async(); + var env = test.props; + var imports = env.paths || []; + /** Loads a single script in the browser */ + function load(src, cb) { + var script = document.createElement('script'); + script.onload = cb; script.src = src; + document.head.appendChild(script); + } + /** Loads scripts in order, waiting on each to load before proceeding */ + function loadAll(src, cb) { + if (src.length === 0) { + cb(); + return; + } + var cur = src.shift(); + // console.log('loading library:', cur); + load(cur, function () { + loadAll(src, cb); + }); + } + + loadAll(imports, function () { + test.done(); + }); + }, {paths: paths})); + }); + return Promise.all(promises); + } +}; \ No newline at end of file