Selenium
- add headless option - add function to wait for downloaded file - add logging
This commit is contained in:
@@ -1,6 +1,25 @@
|
|||||||
const { Builder, Browser, By, until } = require('selenium-webdriver');
|
const { Builder, Browser, By, until } = require('selenium-webdriver');
|
||||||
const { Options } = require('selenium-webdriver/chrome');
|
const { Options } = require('selenium-webdriver/chrome');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
|
// Taken from: <https://stackoverflow.com/a/70040209>; 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() {
|
(async function exportElixFormsModule() {
|
||||||
// Override with environment variables, if they exist
|
// Override with environment variables, if they exist
|
||||||
@@ -23,6 +42,7 @@ const path = require('path');
|
|||||||
'download.directory_upgrade': true,
|
'download.directory_upgrade': true,
|
||||||
'safebrowsing.enabled': true // Avoid blocking
|
'safebrowsing.enabled': true // Avoid blocking
|
||||||
});
|
});
|
||||||
|
options.addArguments("--headless");
|
||||||
|
|
||||||
let driver = await new Builder()
|
let driver = await new Builder()
|
||||||
.forBrowser(Browser.CHROME)
|
.forBrowser(Browser.CHROME)
|
||||||
@@ -34,8 +54,25 @@ const path = require('path');
|
|||||||
// Implicit timeouts
|
// Implicit timeouts
|
||||||
await driver.manage().setTimeouts({ implicit: 3000 });
|
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/');
|
await driver.get('https://console-unipr.elixforms.it/backoffice/');
|
||||||
|
|
||||||
|
console.log(`Logging in with username: ${username}`);
|
||||||
|
|
||||||
await driver
|
await driver
|
||||||
.findElement(By.id('username'))
|
.findElement(By.id('username'))
|
||||||
.sendKeys(username); // 'Automation_User'
|
.sendKeys(username); // 'Automation_User'
|
||||||
@@ -48,7 +85,8 @@ const path = require('path');
|
|||||||
.findElement(By.name('INSERT_BTN00000'))
|
.findElement(By.name('INSERT_BTN00000'))
|
||||||
.click();
|
.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
|
await driver
|
||||||
.wait(until.elementLocated(By.id('title_0')), 10000)
|
.wait(until.elementLocated(By.id('title_0')), 10000)
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
@@ -57,6 +95,8 @@ const path = require('path');
|
|||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log(`Navigating to module export page...`);
|
||||||
|
|
||||||
await driver
|
await driver
|
||||||
.navigate()
|
.navigate()
|
||||||
.to('https://console-unipr.elixforms.it/rwe2/admin_console.jsp');
|
.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(moduleName);
|
||||||
await moduleSearchTextbox.sendKeys('\n');
|
await moduleSearchTextbox.sendKeys('\n');
|
||||||
|
|
||||||
|
console.log(`Waiting for search results...`);
|
||||||
|
|
||||||
// Needed to wait for the search results to load before clicking the export button
|
// Needed to wait for the search results to load before clicking the export button
|
||||||
await driver.sleep(2000);
|
await driver.sleep(2000);
|
||||||
|
|
||||||
|
console.log(`Clicking export button for module: ${moduleName}`);
|
||||||
|
|
||||||
await driver
|
await driver
|
||||||
.findElement(By.xpath('//div[contains(@class, "item")][1]'))
|
.findElement(By.xpath('//div[contains(@class, "item")][1]'))
|
||||||
.findElement(By.xpath('.//input[starts-with(@name, "INSERT_BTN_USER_")]'))
|
.findElement(By.xpath('.//input[starts-with(@name, "INSERT_BTN_USER_")]'))
|
||||||
.click()
|
.click()
|
||||||
|
|
||||||
|
console.log(`Clicking "Esporta il modulo" link...`);
|
||||||
|
|
||||||
await driver
|
await driver
|
||||||
.findElement(By.linkText('Esporta il modulo'))
|
.findElement(By.linkText('Esporta il modulo'))
|
||||||
.click();
|
.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);
|
await driver.wait(async () => (await driver.getAllWindowHandles()).length === 2, 5000);
|
||||||
const windows = await driver.getAllWindowHandles();
|
const windows = await driver.getAllWindowHandles();
|
||||||
windows.forEach(async handle => {
|
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'));
|
let moduleProtocol = await driver.findElement(By.id('moduleProtocol'));
|
||||||
if (!await moduleProtocol.isSelected()) {
|
if (!await moduleProtocol.isSelected()) {
|
||||||
await moduleProtocol.click();
|
await moduleProtocol.click();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let moduleId = await driver.findElement(By.id('moduleId')).getAttribute('value');
|
||||||
|
let moduleFilename ="elxforms_" + moduleId + ".elx";
|
||||||
|
|
||||||
await driver
|
await driver
|
||||||
.findElement(By.id('download'))
|
.findElement(By.id('download'))
|
||||||
.click();
|
.click();
|
||||||
|
|
||||||
let downloadLink = await driver.wait(until.elementLocated(By.linkText('Scarica il modulo')));
|
let downloadLink = await driver.wait(until.elementLocated(By.linkText('Scarica il modulo')));
|
||||||
let href = await downloadLink.getAttribute('href');
|
let href = await downloadLink.getAttribute('href');
|
||||||
downloadLink.click();
|
|
||||||
|
|
||||||
// Wait for the download to complete (this is a simple approach, adjust as needed)
|
console.log(`Found download link: ${href}; clicking to download the module as ${moduleFilename}...`);
|
||||||
await driver.sleep(5000);
|
|
||||||
|
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();
|
await driver.quit();
|
||||||
|
|
||||||
|
return downloadedFilePath;
|
||||||
})();
|
})();
|
||||||
|
|||||||
Reference in New Issue
Block a user