feat: add fallback mechanism for click and waitForLoadState action

This commit is contained in:
RohitR311
2024-12-10 20:33:35 +05:30
parent 142c90ae1c
commit c7af54ebe0

View File

@@ -469,6 +469,16 @@ export default class Interpreter extends EventEmitter {
}), }),
}; };
const executeAction = async (invokee: any, methodName: string, args: any) => {
console.log("Executing action:", methodName, args);
if (!args || Array.isArray(args)) {
await (<any>invokee[methodName])(...(args ?? []));
} else {
await (<any>invokee[methodName])(args);
}
};
for (const step of steps) { for (const step of steps) {
this.log(`Launching ${String(step.action)}`, Level.LOG); this.log(`Launching ${String(step.action)}`, Level.LOG);
@@ -486,10 +496,20 @@ export default class Interpreter extends EventEmitter {
invokee = invokee[level]; invokee = invokee[level];
} }
if (!step.args || Array.isArray(step.args)) { if (methodName === 'waitForLoadState') {
await (<any>invokee[methodName])(...(step.args ?? [])); try {
await executeAction(invokee, methodName, step.args);
} catch (error) {
await executeAction(invokee, methodName, 'domcontentloaded');
}
} else if (methodName === 'click') {
try {
await executeAction(invokee, methodName, step.args);
} catch (error) {
await executeAction(invokee, methodName, [step.args[0], { force: true }]);
}
} else { } else {
await (<any>invokee[methodName])(step.args); await executeAction(invokee, methodName, step.args);
} }
} }
@@ -571,7 +591,7 @@ export default class Interpreter extends EventEmitter {
return allResults; return allResults;
} }
// Click the 'Load More' button to load additional items // Click the 'Load More' button to load additional items
await loadMoreButton.click(); await loadMoreButton.dispatchEvent('click');
await page.waitForTimeout(2000); // Wait for new items to load await page.waitForTimeout(2000); // Wait for new items to load
// After clicking 'Load More', scroll down to load more items // After clicking 'Load More', scroll down to load more items
await page.evaluate(() => window.scrollTo(0, document.body.scrollHeight)); await page.evaluate(() => window.scrollTo(0, document.body.scrollHeight));