Refactor evaluateStartPakket.js and iterateOverDashboardTable.js

This commit is contained in:
bdaneels 2024-11-22 14:47:09 +01:00
parent a554020123
commit b92f1c8c29
2 changed files with 104 additions and 96 deletions

View File

@ -1,6 +1,6 @@
const IFRAME_SELECTOR = 'iframe[title="Hoofdinhoud"]'; const IFRAME_SELECTOR = 'iframe[title="Hoofdinhoud"]';
const CELL_SELECTOR = 'a[title="Sectie uitvouwen Algemene opleidingsonderdelen"]'; const CELL_SELECTOR =
'a[title="Sectie uitvouwen Algemene opleidingsonderdelen"]';
/** /**
* Extracted function to get the text content of a node. * Extracted function to get the text content of a node.
@ -8,7 +8,7 @@ const CELL_SELECTOR = 'a[title="Sectie uitvouwen Algemene opleidingsonderdelen"]
* @returns {Promise<string>} The text content of the node. * @returns {Promise<string>} The text content of the node.
*/ */
async function getNodeTextContent(node) { async function getNodeTextContent(node) {
return await node.evaluate(node => node.textContent); return await node.evaluate((node) => node.textContent);
} }
/** /**
@ -20,9 +20,9 @@ async function getContentFrame(page) {
await page.waitForSelector(IFRAME_SELECTOR); await page.waitForSelector(IFRAME_SELECTOR);
const iframe = await page.$(IFRAME_SELECTOR); const iframe = await page.$(IFRAME_SELECTOR);
if (!iframe) { if (!iframe) {
throw new Error('Could not find iframe on the AA-page'); throw new Error("Could not find iframe on the AA-page");
} }
console.log('iframe found on AA-page') console.log("iframe found on AA-page");
return await iframe.contentFrame(); return await iframe.contentFrame();
} }
@ -37,18 +37,21 @@ async function isStartPakketAvailable(page) {
const contentFrame = await getContentFrame(page); const contentFrame = await getContentFrame(page);
await contentFrame.waitForSelector(CELL_SELECTOR); await contentFrame.waitForSelector(CELL_SELECTOR);
const cell = await contentFrame.$(CELL_SELECTOR); const cell = await contentFrame.$(CELL_SELECTOR);
console.log(cell);
if (!cell) { if (!cell) {
console.error('Cell not found'); throw new Error(
return false; "Cell inside function isStartPakketAvailable is not defined"
);
} }
const isCollapsed = await isAriaExpandedFalse(cell); const isCollapsed = await isAriaExpandedFalse(cell);
console.log(`Aria-expanded: ${isCollapsed}`); console.log(`Aria-expanded: ${isCollapsed}`);
return isCollapsed; return {
isCollapsed: isCollapsed,
// Add more properties here in the future as needed
};
} }
/** /**
@ -59,9 +62,11 @@ async function isStartPakketAvailable(page) {
*/ */
async function isAriaExpandedFalse(node) { async function isAriaExpandedFalse(node) {
if (!node) { if (!node) {
throw new Error('Node is not defined'); throw new Error("Node is not defined");
} }
return await node.evaluate(element => element.getAttribute('aria-expanded') === 'false'); return await node.evaluate(
(element) => element.getAttribute("aria-expanded") === "false"
);
} }
module.exports = isStartPakketAvailable ; module.exports = isStartPakketAvailable;

View File

@ -8,10 +8,9 @@ const puppeteer = require("puppeteer");
const isStartPakketAvailable = require("./evaluateStartPakket"); const isStartPakketAvailable = require("./evaluateStartPakket");
async function iterateOverDashboardTable() { async function iterateOverDashboardTable() {
/*connection to local host */ /*connection to local host */
const browser = await puppeteer.connect({ const browser = await puppeteer.connect({
browserURL: 'http://localhost:9222', // Connect to the browser's debugging address browserURL: "http://localhost:9222", // Connect to the browser's debugging address
}); });
// Get all open pages (tabs) // Get all open pages (tabs)
@ -20,43 +19,51 @@ async function iterateOverDashboardTable() {
// Interact with the last tab (or any tab of choice) // Interact with the last tab (or any tab of choice)
let page = pages[pages.length - 1]; // Choose the last opened tab let page = pages[pages.length - 1]; // Choose the last opened tab
console.log('Current URL:', page.url()); console.log("Current URL:", page.url());
// Select the table body by ID // Select the table body by ID
const iframeElement = await page.waitForSelector('iframe[title="Hoofdinhoud"]'); const iframeElement = await page.waitForSelector(
'iframe[title="Hoofdinhoud"]'
);
const iframe = await iframeElement.contentFrame(); const iframe = await iframeElement.contentFrame();
if (!iframe) { if (!iframe) {
console.log('Iframe on dasbhoard inschrijvingen page not found!'); console.log("Iframe on dasbhoard inschrijvingen page not found!");
return; return;
} }
// Select the table body within the iframe // Select the table body within the iframe
const tableBodySelector = '#QQ_RPT_OIS_TEMP\\$scroll\\$0 tbody'; const tableBodySelector = "#QQ_RPT_OIS_TEMP\\$scroll\\$0 tbody";
// Check if the table exists inside the iframe // Check if the table exists inside the iframe
const tableExists = await iframe.$(tableBodySelector); const tableExists = await iframe.$(tableBodySelector);
if (!tableExists) { if (!tableExists) {
console.log('Table not found inside iframe!'); console.log("Table not found inside iframe!");
return; return;
} }
console.log('Table dashboard found inside iframe!'); console.log("Table dashboard found inside iframe!");
// Get all rows within the table body // Get all rows within the table body
const links = await iframe.$$(`${tableBodySelector} tr span[title="AA-rapport"] a`); const links = await iframe.$$(
`${tableBodySelector} tr span[title="AA-rapport"] a`
);
console.log(`Found ${links.length} AA-links to process.`); console.log(`Found ${links.length} AA-links to process.`);
for (let i = 0; i < links.length; i++) { for (let i = 0; i < links.length; i++) {
console.log(`Processing link ${i + 1}`); console.log(`Processing link ${i + 1}`);
const iframeElement = await page.waitForSelector('iframe[title="Hoofdinhoud"]'); const iframeElement = await page.waitForSelector(
'iframe[title="Hoofdinhoud"]'
);
const iframe = await iframeElement.contentFrame(); const iframe = await iframeElement.contentFrame();
const links = await iframe.$$(`${tableBodySelector} tr span[title="AA-rapport"] a`); const links = await iframe.$$(
`${tableBodySelector} tr span[title="AA-rapport"] a`
);
const [newPagePromise] = await Promise.all([ const [newPagePromise] = await Promise.all([
new Promise((resolve) => new Promise((resolve) =>
browser.once('targetcreated', async (target) => { browser.once("targetcreated", async (target) => {
const newPage = await target.page(); const newPage = await target.page();
resolve(newPage); resolve(newPage);
}) })
@ -69,7 +76,7 @@ async function iterateOverDashboardTable() {
try { try {
const evaluationResult = await isStartPakketAvailable(newPage); const evaluationResult = await isStartPakketAvailable(newPage);
if (evaluationResult) { if (evaluationResult.isCollapsed) {
console.log(`Evaluation succeeded for link ${i + 1}`); console.log(`Evaluation succeeded for link ${i + 1}`);
} else { } else {
console.log(`Evaluation failed for link ${i + 1}`); console.log(`Evaluation failed for link ${i + 1}`);
@ -81,10 +88,6 @@ async function iterateOverDashboardTable() {
await newPage.close(); await newPage.close();
} }
console.log('All links processed.'); console.log("All links processed.");
} }
module.exports = iterateOverDashboardTable(); module.exports = iterateOverDashboardTable();