From 9f4257ae2ef7b7d96e54294ff7e42a5cf382233f Mon Sep 17 00:00:00 2001 From: Pier-Paolo Mammi Date: Thu, 18 Jun 2026 16:05:20 +0200 Subject: [PATCH] Selenium - add headless option - add function to wait for downloaded file - add logging --- tools/selenium/export-module.js | 71 ++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 6 deletions(-) diff --git a/tools/selenium/export-module.js b/tools/selenium/export-module.js index 059d73c..6918afe 100644 --- a/tools/selenium/export-module.js +++ b/tools/selenium/export-module.js @@ -1,6 +1,25 @@ const { Builder, Browser, By, until } = require('selenium-webdriver'); const { Options } = require('selenium-webdriver/chrome'); const path = require('path'); +const fs = require('fs'); + +// Taken from: ; feel free to make a better implementation, if you want to! +async function checkFileExist(path, timeout = 2000) +{ + let totalTime = 0; + let checkTime = timeout / 10; + + return await new Promise((resolve, reject) => { + const timer = setInterval(function() { + totalTime += checkTime; + let fileExists = fs.existsSync(path); + if (fileExists || totalTime >= timeout) { + clearInterval(timer); + resolve(fileExists); + } + }, checkTime); + }); +} (async function exportElixFormsModule() { // Override with environment variables, if they exist @@ -23,6 +42,7 @@ const path = require('path'); 'download.directory_upgrade': true, 'safebrowsing.enabled': true // Avoid blocking }); + options.addArguments("--headless"); let driver = await new Builder() .forBrowser(Browser.CHROME) @@ -34,8 +54,25 @@ const path = require('path'); // Implicit timeouts await driver.manage().setTimeouts({ implicit: 3000 }); + console.log(`Starting export for module: ${moduleName}`); + + // Cleanup download directory before starting + if (!fs.existsSync(downloadDir)) { + console.log(`Creating download directory: ${downloadDir}`); + fs.mkdirSync(downloadDir); + } else { + console.log(`Cleaning up download directory: ${downloadDir}`); + fs.readdirSync(downloadDir).forEach(file => { + fs.unlinkSync(path.join(downloadDir, file)); + }); + } + + console.log(`Navigating to login page...`); + await driver.get('https://console-unipr.elixforms.it/backoffice/'); + console.log(`Logging in with username: ${username}`); + await driver .findElement(By.id('username')) .sendKeys(username); // 'Automation_User' @@ -48,7 +85,8 @@ const path = require('path'); .findElement(By.name('INSERT_BTN00000')) .click(); - // Wait for "Benvenuto" (quit if not found, likely login failed) + console.log(`Waiting for "Benvenuto" (quit if not found, likely login failed)`); + await driver .wait(until.elementLocated(By.id('title_0')), 10000) .catch(() => { @@ -57,6 +95,8 @@ const path = require('path'); process.exit(1); }); + console.log(`Navigating to module export page...`); + await driver .navigate() .to('https://console-unipr.elixforms.it/rwe2/admin_console.jsp'); @@ -66,19 +106,27 @@ const path = require('path'); await moduleSearchTextbox.sendKeys(moduleName); await moduleSearchTextbox.sendKeys('\n'); + console.log(`Waiting for search results...`); + // Needed to wait for the search results to load before clicking the export button await driver.sleep(2000); + console.log(`Clicking export button for module: ${moduleName}`); + await driver .findElement(By.xpath('//div[contains(@class, "item")][1]')) .findElement(By.xpath('.//input[starts-with(@name, "INSERT_BTN_USER_")]')) .click() + console.log(`Clicking "Esporta il modulo" link...`); + await driver .findElement(By.linkText('Esporta il modulo')) .click(); - // Wait for the new tab to open and switch to it + + console.log(`Waiting for the new tab to finish loading content...`); + await driver.wait(async () => (await driver.getAllWindowHandles()).length === 2, 5000); const windows = await driver.getAllWindowHandles(); windows.forEach(async handle => { @@ -87,22 +135,33 @@ const path = require('path'); } }); - // Wait for the new tab to finish loading content let moduleProtocol = await driver.findElement(By.id('moduleProtocol')); if (!await moduleProtocol.isSelected()) { await moduleProtocol.click(); } + let moduleId = await driver.findElement(By.id('moduleId')).getAttribute('value'); + let moduleFilename ="elxforms_" + moduleId + ".elx"; + await driver .findElement(By.id('download')) .click(); let downloadLink = await driver.wait(until.elementLocated(By.linkText('Scarica il modulo'))); let href = await downloadLink.getAttribute('href'); - downloadLink.click(); - // Wait for the download to complete (this is a simple approach, adjust as needed) - await driver.sleep(5000); + console.log(`Found download link: ${href}; clicking to download the module as ${moduleFilename}...`); + + await downloadLink.click(); + + console.log(`Waiting for the file to be downloaded to ${downloadDir}...`); + + let downloadedFilePath = path.join(downloadDir, moduleFilename); + await checkFileExist(downloadedFilePath, 5000); + + console.log(`File downloaded!`); await driver.quit(); + + return downloadedFilePath; })();