Refactor parameter passing for the startBrowser(s) functions in test.mjs

This commit prepares for the introduction of extra options in later
commits by changing the function signatures of the `startBrowser(s)`
functions to take parameter objects instead of plain parameters. This
makes the call sites explicitly state which parameters they pass,
improving overall readability as well.
This commit is contained in:
Tim van der Meij 2023-11-04 19:15:44 +01:00
parent 24fcc042f4
commit a1d84f8ce1
No known key found for this signature in database
GPG Key ID: 8C3FD2925A5F2762

View File

@ -250,8 +250,10 @@ function updateRefImages() {
function examineRefImages() {
startServer();
const startUrl = `http://${host}:${server.port}/test/resources/reftest-analyzer.html#web=/test/eq.log`;
startBrowser("firefox", startUrl).then(function (browser) {
startBrowser({
browserName: "firefox",
startUrl: `http://${host}:${server.port}/test/resources/reftest-analyzer.html#web=/test/eq.log`,
}).then(function (browser) {
browser.on("disconnected", function () {
stopServer();
process.exit(0);
@ -339,8 +341,9 @@ function startRefTest(masterMode, showRefImages) {
server.hooks.POST.push(refTestPostHandler);
onAllSessionsClosed = finalize;
const baseUrl = `http://${host}:${server.port}/test/test_slave.html`;
await startBrowsers(function (session) {
await startBrowsers({
baseUrl: `http://${host}:${server.port}/test/test_slave.html`,
initializeSession: session => {
session.masterMode = masterMode;
session.taskResults = {};
session.tasks = {};
@ -358,7 +361,8 @@ function startRefTest(masterMode, showRefImages) {
session.numEqNoSnapshot = 0;
session.numEqFailures = 0;
monitorBrowserTimeout(session, handleSessionTimeout);
}, baseUrl);
},
});
}
function checkRefsTmp() {
if (masterMode && fs.existsSync(refsTmpDir)) {
@ -798,11 +802,13 @@ async function startUnitTest(testUrl, name) {
startServer();
server.hooks.POST.push(unitTestPostHandler);
const baseUrl = `http://${host}:${server.port}${testUrl}`;
await startBrowsers(function (session) {
await startBrowsers({
baseUrl: `http://${host}:${server.port}${testUrl}`,
initializeSession: session => {
session.numRuns = 0;
session.numErrors = 0;
}, baseUrl);
},
});
}
async function startIntegrationTest() {
@ -810,9 +816,12 @@ async function startIntegrationTest() {
startServer();
const { runTests } = await import("./integration-boot.mjs");
await startBrowsers(function (session) {
await startBrowsers({
baseUrl: null,
initializeSession: session => {
session.numRuns = 0;
session.numErrors = 0;
},
});
global.integrationBaseUrl = `http://${host}:${server.port}/build/generic/web/viewer.html`;
global.integrationSessions = sessions;
@ -888,7 +897,7 @@ function unitTestPostHandler(req, res) {
return true;
}
async function startBrowser(browserName, startUrl = "") {
async function startBrowser({ browserName, startUrl }) {
const options = {
product: browserName,
headless: false,
@ -958,7 +967,7 @@ async function startBrowser(browserName, startUrl = "") {
return browser;
}
async function startBrowsers(initSessionCallback, baseUrl = null) {
async function startBrowsers({ baseUrl, initializeSession }) {
// Remove old browser revisions from Puppeteer's cache. Updating Puppeteer can
// cause new browser revisions to be downloaded, so trimming the cache will
// prevent the disk from filling up over time.
@ -997,10 +1006,10 @@ async function startBrowsers(initSessionCallback, baseUrl = null) {
startUrl = baseUrl + queryParameters;
}
await startBrowser(browserName, startUrl)
await startBrowser({ browserName, startUrl })
.then(function (browser) {
session.browser = browser;
initSessionCallback?.(session);
initializeSession(session);
})
.catch(function (ex) {
console.log(`Error while starting ${browserName}: ${ex.message}`);