2024-11-20 14:33:02 +00:00
|
|
|
/* TEST MODUS
|
|
|
|
open chrome in debug modus
|
|
|
|
start chrome --remote-debugging-port=9222 --user-data-dir="C:\ChromeDebug"
|
|
|
|
navigeer naar het dashboard en roep de lijst met startpakket studenten op die relevant zijn voor de test
|
|
|
|
start dan het script
|
|
|
|
*/
|
|
|
|
const puppeteer = require("puppeteer");
|
|
|
|
const isStartPakketAvailable = require("./evaluateStartPakket");
|
2024-12-03 13:16:44 +00:00
|
|
|
const fs = require("fs");
|
|
|
|
const saveResultsToExcel = require("./saveResultsToExcel");
|
2024-12-03 14:16:45 +00:00
|
|
|
const appendToExcel = require("./appendResultsToExcel");
|
|
|
|
const wait = require("./wait");
|
2024-11-20 14:33:02 +00:00
|
|
|
|
|
|
|
async function iterateOverDashboardTable() {
|
2024-11-22 13:47:09 +00:00
|
|
|
/*connection to local host */
|
|
|
|
const browser = await puppeteer.connect({
|
|
|
|
browserURL: "http://localhost:9222", // Connect to the browser's debugging address
|
|
|
|
});
|
|
|
|
|
2024-12-03 13:16:44 +00:00
|
|
|
//array to store results for Excel export
|
|
|
|
let results = []
|
2024-12-03 14:16:45 +00:00
|
|
|
const filename = "DashboardResults.xlsx"
|
2024-12-03 13:16:44 +00:00
|
|
|
|
2024-11-22 13:47:09 +00:00
|
|
|
// Get all open pages (tabs)
|
|
|
|
const pages = await browser.pages();
|
|
|
|
console.log(`Found ${pages.length} open pages on the browser.`);
|
|
|
|
|
|
|
|
// Interact with the last tab (or any tab of choice)
|
|
|
|
let page = pages[pages.length - 1]; // Choose the last opened tab
|
|
|
|
console.log("Current URL:", page.url());
|
|
|
|
|
|
|
|
// Select the table body by ID
|
|
|
|
const iframeElement = await page.waitForSelector(
|
|
|
|
'iframe[title="Hoofdinhoud"]'
|
|
|
|
);
|
|
|
|
const iframe = await iframeElement.contentFrame();
|
|
|
|
|
|
|
|
if (!iframe) {
|
|
|
|
console.log("Iframe on dasbhoard inschrijvingen page not found!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select the table body within the iframe
|
|
|
|
const tableBodySelector = "#QQ_RPT_OIS_TEMP\\$scroll\\$0 tbody";
|
|
|
|
|
|
|
|
// Check if the table exists inside the iframe
|
|
|
|
const tableExists = await iframe.$(tableBodySelector);
|
|
|
|
if (!tableExists) {
|
|
|
|
console.log("Table not found inside iframe!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log("Table dashboard found inside iframe!");
|
|
|
|
|
|
|
|
// Get all rows within the table body
|
|
|
|
const links = await iframe.$$(
|
|
|
|
`${tableBodySelector} tr span[title="AA-rapport"] a`
|
|
|
|
);
|
|
|
|
console.log(`Found ${links.length} AA-links to process.`);
|
|
|
|
|
2024-12-03 14:16:45 +00:00
|
|
|
// Initialize Excel file if it doesn't exist
|
|
|
|
if (!fs.existsSync(filename)) {
|
|
|
|
saveResultsToExcel([], filename); // Create empty file
|
|
|
|
console.log(`Initialized new Excel file: ${filename}`);
|
|
|
|
}
|
|
|
|
|
2024-12-03 13:16:44 +00:00
|
|
|
// process links
|
2024-11-22 13:47:09 +00:00
|
|
|
for (let i = 0; i < links.length; i++) {
|
|
|
|
console.log(`Processing link ${i + 1}`);
|
|
|
|
|
|
|
|
const iframeElement = await page.waitForSelector(
|
|
|
|
'iframe[title="Hoofdinhoud"]'
|
|
|
|
);
|
2024-11-20 14:33:02 +00:00
|
|
|
const iframe = await iframeElement.contentFrame();
|
|
|
|
|
2024-11-22 13:47:09 +00:00
|
|
|
const links = await iframe.$$(
|
|
|
|
`${tableBodySelector} tr span[title="AA-rapport"] a`
|
|
|
|
);
|
|
|
|
|
2024-12-03 15:39:11 +00:00
|
|
|
if ((i + 1) % 10 === 0) {
|
|
|
|
console.log('Simulating break, waiting 10 seconds to stay under the radar :)')
|
|
|
|
await wait(10000)
|
|
|
|
|
|
|
|
}
|
2024-12-03 14:16:45 +00:00
|
|
|
|
2024-11-22 13:47:09 +00:00
|
|
|
const [newPagePromise] = await Promise.all([
|
|
|
|
new Promise((resolve) =>
|
|
|
|
browser.once("targetcreated", async (target) => {
|
|
|
|
const newPage = await target.page();
|
|
|
|
resolve(newPage);
|
|
|
|
})
|
|
|
|
),
|
|
|
|
links[i].click(), // Simulate the click
|
|
|
|
]);
|
2024-12-03 14:16:45 +00:00
|
|
|
let result;
|
2024-11-22 13:47:09 +00:00
|
|
|
|
|
|
|
const newPage = await newPagePromise;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const evaluationResult = await isStartPakketAvailable(newPage);
|
|
|
|
|
2024-12-03 13:16:44 +00:00
|
|
|
//Save results for Excel
|
2024-12-03 14:16:45 +00:00
|
|
|
result = ({
|
2024-12-03 13:16:44 +00:00
|
|
|
LinkNumber: i +1,
|
|
|
|
StudentName: evaluationResult.studName || 'N/A',
|
|
|
|
StudentID: evaluationResult.studId || "N/A",
|
|
|
|
IsCollapsed: evaluationResult.isCollapsed ? "Yes" : "No",
|
|
|
|
IsCollapsedKeuze: evaluationResult.isCollapsed_keuze ? "Yes" : "No",
|
|
|
|
IsCollapsedMain: evaluationResult.isCollapsed_main ? "Yes" : "No",
|
|
|
|
})
|
|
|
|
console.log(`Link ${i + 1 } processed successfully.`);
|
|
|
|
|
2024-11-22 13:47:09 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error(`Error processing link ${i + 1}:`, error.message);
|
2024-12-03 13:16:44 +00:00
|
|
|
// Save error for Excel
|
2024-12-03 14:16:45 +00:00
|
|
|
result = ({
|
2024-12-03 13:16:44 +00:00
|
|
|
LinkNumber: i + 1,
|
|
|
|
StudentName: "Error",
|
|
|
|
StudentID: "Error",
|
|
|
|
IsCollapsed: "Error",
|
|
|
|
IsCollapsedKeuze: "Error",
|
|
|
|
IsCollapsedMain: "Error",
|
2024-12-03 15:39:11 +00:00
|
|
|
ErrorStr: error.message,
|
2024-12-03 13:16:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
await newPage.close();
|
2024-11-20 14:33:02 +00:00
|
|
|
}
|
2024-12-03 14:16:45 +00:00
|
|
|
results.push(result);
|
|
|
|
appendToExcel(result, filename);
|
2024-11-22 13:47:09 +00:00
|
|
|
}
|
2024-11-20 14:33:02 +00:00
|
|
|
|
2024-11-22 13:47:09 +00:00
|
|
|
console.log("All links processed.");
|
2024-12-03 13:16:44 +00:00
|
|
|
saveResultsToExcel(results, "DashboardResults.xlsx");
|
|
|
|
console.log("Results saved successfully to DashboardResults.xlsx");
|
|
|
|
|
2024-11-20 14:33:02 +00:00
|
|
|
}
|
2024-12-03 13:16:44 +00:00
|
|
|
|
|
|
|
|
2024-11-22 13:47:09 +00:00
|
|
|
module.exports = iterateOverDashboardTable();
|