From e8788cf2d0e478778ebc6fed2f11da5d7f10a763 Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 5 Feb 2025 08:32:32 +0100 Subject: [PATCH] test: refs #6695 e2e better selectOption --- test/cypress/support/commands.js | 88 ++++++++++++-------------------- 1 file changed, 32 insertions(+), 56 deletions(-) diff --git a/test/cypress/support/commands.js b/test/cypress/support/commands.js index 36a38cfb6..438f0ce8d 100755 --- a/test/cypress/support/commands.js +++ b/test/cypress/support/commands.js @@ -108,74 +108,50 @@ Cypress.Commands.add('selectOption', (selector, option, timeout = 2500) => { .invoke('attr', 'aria-controls') // Obtiene el valor del atributo aria-controls .then((ariaControl) => { cy.log('ARIA', ariaControl); // Muestra el valor en la consola de Cypress - let retries = 0; - cy.retryCheckItem(ariaControl, selector, option, retries); + getItems(ariaControl).then((items) => { + cy.log('items: ', items); + const matchingItem = items + .toArray() + .find((item) => item.innerText.includes(option)); + if (!matchingItem) return findOption(selector, option, ariaControl); + cy.wrap(matchingItem).click(); + }); }); } }); -Cypress.Commands.add('retryCheckItem', (ariaControl, selector, option, retries) => { - if (retries == 10) throw new Error('Maximum number of retries exceeded → ', option); - cy.get('#' + ariaControl) +function getItems(ariaControl, startTime = Cypress._.now(), timeout = 2500) { + return cy + .get('#' + ariaControl, { timeout }) // Se asegura de que el selector aparezca en tiempo razonable .should('exist') .find('.q-item') .should('exist') .then(($items) => { - cy.log('ASDASD', $items?.length); - if (!$items?.length) - return cy - .wait(50) - .then(() => - cy.retryCheckItem(ariaControl, selector, option, retries + 1), + if (!$items?.length || $items.first().text().trim() === '') { + // 🔹 Si ha pasado más tiempo que el límite, falla el test + if (Cypress._.now() - startTime > timeout) { + throw new Error( + `getItems: Tiempo de espera (${timeout}ms) excedido.`, ); - const data = $items.toArray().map((item) => item.innerText); - const dataString = JSON.stringify(data); - cy.log('OPTIONS', dataString); - if (data[0] == '') - return cy - .wait(50) - .then(() => - cy.retryCheckItem(ariaControl, selector, option, retries + 1), - ); - cy.log('PASSED'); - const optionFinded = $items - .toArray() - .some((item) => item.innerText.includes(option)); - - if (optionFinded) { - const itemAClickear = $items - .filter((_, item) => item.innerText.includes(option)) - .first(); - cy.wrap(itemAClickear).click(); // Haz clic en el elemento encontrado - } else { - // throw new Error('Option not found → ', option); - cy.get(selector, { timeout }).clear().type(option); - let retries = 0; - cy.retrySelectOption(selector, option, timeout, retries); + } + return getItems(ariaControl, startTime, timeout); } - }); -}); -Cypress.Commands.add('retrySelectOption', (selector, option, timeout, retries) => { - cy.log('RETRY', retries); - if (retries == 10) throw new Error('Maximum number of retries exceeded → ', option); - cy.get('.q-menu', { timeout }) - .should('exist') - .then(($menu) => { - if ($menu.is(':visible')) { - cy.get('.q-item') - .should('exist') - .should('be.visible') - .contains(option) - .click(); - cy.get(selector).blur(); - } else { - cy.wait(100).then(() => { - cy.retrySelectOption(selector, option, timeout, retries + 1); - }); - } + return cy.wrap($items); }); -}); +} + +function findOption(selector, option, ariaControl) { + cy.get(selector).clear().type(option); + // cy.get('.q-menu').should('not.be.visible'); + getItems(ariaControl).then((items) => { + cy.log('findOption items: ', items); + const matchingItem = items + .toArray() + .find((item) => item.innerText.includes(option)); + cy.wrap(matchingItem).click(); + }); +} Cypress.Commands.add('countSelectOptions', (selector, option) => { cy.waitForElement(selector);