Skip to content

Commit

Permalink
chore: enhance count elements method with retry logic
Browse files Browse the repository at this point in the history
Signed-off-by: Yordan Iliev <[email protected]>
  • Loading branch information
yiliev0 committed Jan 15, 2025
1 parent 94d5971 commit 545200f
Showing 1 changed file with 41 additions and 9 deletions.
50 changes: 41 additions & 9 deletions automation/pages/BasePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,20 +601,52 @@ class BasePage {
// --------------------------

/**
* Counts the number of elements that match a selector prefix.
* Counts the number of elements that match a selector prefix
*
* @param {string} selectorPrefix - The prefix of the selector.
* @param {number} [timeout=this.LONG_TIMEOUT] - The maximum time (in ms) to keep retrying.
* @returns {Promise<number>} - The count of matching elements.
*/
async countElements(selectorPrefix) {
async countElements(selectorPrefix, timeout = this.LONG_TIMEOUT) {
console.log(`Looking for elements with prefix: ${selectorPrefix}`);
const selector = this.isCssSelector(selectorPrefix)
? `${selectorPrefix}`
: `[data-testid^="${selectorPrefix}"]`;
const elements = this.window.locator(selector);
const count = await elements.count();
console.log(`Found ${count} elements with prefix: ${selectorPrefix}`);
return count;

// Determine how often (in ms) to check for elements.
const interval = 500;

// Calculate how many times we'll attempt based on the timeout and interval.
const maxAttempts = Math.floor(timeout / interval);
let attempts = 0;

while (attempts < maxAttempts) {
attempts++;

const selector = this.isCssSelector(selectorPrefix)
? selectorPrefix
: `[data-testid^="${selectorPrefix}"]`;

const elements = this.window.locator(selector);

// Count elements.
const count = await elements.count();

Check failure on line 630 in automation/pages/BasePage.js

View workflow job for this annotation

GitHub Actions / Automation | Settings

[Transaction tool] › tests/settingsTests.test.js:88:3 › Settings tests › Verify user can delete key

1) [Transaction tool] › tests/settingsTests.test.js:88:3 › Settings tests › Verify user can delete key Error: locator.count: Target page, context or browser has been closed at pages/BasePage.js:630 628 | 629 | // Count elements. > 630 | const count = await elements.count(); | ^ 631 | 632 | if (count > 0) { 633 | console.log( at SettingsPage.countElements (/home/runner/_work/hedera-transaction-tool/hedera-transaction-tool/automation/pages/BasePage.js:630:36) at SettingsPage.getKeyRowCount (/home/runner/_work/hedera-transaction-tool/hedera-transaction-tool/automation/pages/SettingsPage.js:115:23) at /home/runner/_work/hedera-transaction-tool/hedera-transaction-tool/automation/tests/settingsTests.test.js:118:52

Check failure on line 630 in automation/pages/BasePage.js

View workflow job for this annotation

GitHub Actions / Automation | Workflow

[Transaction tool] › tests/workflowTests.test.js:336:3 › Workflow tests › Verify user can unlink multiple files

1) [Transaction tool] › tests/workflowTests.test.js:336:3 › Workflow tests › Verify user can unlink multiple files Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: locator.count: Target crashed at pages/BasePage.js:630 628 | 629 | // Count elements. > 630 | const count = await elements.count(); | ^ 631 | 632 | if (count > 0) { 633 | console.log( at FilePage.countElements (/home/runner/_work/hedera-transaction-tool/hedera-transaction-tool/automation/pages/BasePage.js:630:36) at FilePage.findFileByIndex (/home/runner/_work/hedera-transaction-tool/hedera-transaction-tool/automation/pages/FilePage.js:166:30) at FilePage.isFileCardVisible (/home/runner/_work/hedera-transaction-tool/hedera-transaction-tool/automation/pages/FilePage.js:182:30) at /home/runner/_work/hedera-transaction-tool/hedera-transaction-tool/automation/tests/workflowTests.test.js:353:31

Check failure on line 630 in automation/pages/BasePage.js

View workflow job for this annotation

GitHub Actions / Automation | Workflow

[Transaction tool] › tests/workflowTests.test.js:336:3 › Workflow tests › Verify user can unlink multiple files

1) [Transaction tool] › tests/workflowTests.test.js:336:3 › Workflow tests › Verify user can unlink multiple files Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: locator.count: Target crashed at pages/BasePage.js:630 628 | 629 | // Count elements. > 630 | const count = await elements.count(); | ^ 631 | 632 | if (count > 0) { 633 | console.log( at FilePage.countElements (/home/runner/_work/hedera-transaction-tool/hedera-transaction-tool/automation/pages/BasePage.js:630:36) at FilePage.findFileByIndex (/home/runner/_work/hedera-transaction-tool/hedera-transaction-tool/automation/pages/FilePage.js:166:30) at FilePage.isFileCardVisible (/home/runner/_work/hedera-transaction-tool/hedera-transaction-tool/automation/pages/FilePage.js:182:30) at /home/runner/_work/hedera-transaction-tool/hedera-transaction-tool/automation/tests/workflowTests.test.js:353:31

if (count > 0) {
console.log(
`Found ${count} elements with prefix: "${selectorPrefix}" on attempt ${attempts}.`
);
return count;
}

console.log(
`No elements found with prefix: "${selectorPrefix}" on attempt ${attempts}. Retrying in ${interval}ms...`
);
await new Promise((resolve) => setTimeout(resolve, interval));
}

throw new Error(

Check failure on line 645 in automation/pages/BasePage.js

View workflow job for this annotation

GitHub Actions / Automation | Workflow

[Transaction tool] › tests/workflowTests.test.js:336:3 › Workflow tests › Verify user can unlink multiple files

1) [Transaction tool] › tests/workflowTests.test.js:336:3 › Workflow tests › Verify user can unlink multiple files Error: Unable to find elements with prefix "p-file-id-" within 5500 ms at pages/BasePage.js:645 643 | } 644 | > 645 | throw new Error( | ^ 646 | `Unable to find elements with prefix "${selectorPrefix}" within ${timeout} ms` 647 | ); 648 | } at FilePage.countElements (/home/runner/_work/hedera-transaction-tool/hedera-transaction-tool/automation/pages/BasePage.js:645:11) at FilePage.findFileByIndex (/home/runner/_work/hedera-transaction-tool/hedera-transaction-tool/automation/pages/FilePage.js:166:19) at FilePage.isFileCardVisible (/home/runner/_work/hedera-transaction-tool/hedera-transaction-tool/automation/pages/FilePage.js:182:19) at /home/runner/_work/hedera-transaction-tool/hedera-transaction-tool/automation/tests/workflowTests.test.js:353:31
`Unable to find elements with prefix "${selectorPrefix}" within ${timeout} ms`
);
}

}

module.exports = BasePage;

0 comments on commit 545200f

Please sign in to comment.