test: refs #6695 e2e better selectOption
gitea/salix-front/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Alex Moreno 2025-02-05 14:07:49 +01:00
parent 4b09c18930
commit 1ee6469ef7
2 changed files with 22 additions and 39 deletions

View File

@ -50,5 +50,3 @@ COPY test/cypress test/cypress
ENV CYPRESS_BROWSER=chrome
ENV CHROME_BIN=/usr/bin/chromium
CMD ["npx", "cypress", "run"]

View File

@ -92,43 +92,40 @@ Cypress.Commands.add('getValue', (selector) => {
Cypress.Commands.add('selectOption', (selector, option, timeout = 2500) => {
cy.waitForElement(selector, timeout);
cy.get(selector, { timeout }) // Selecciona el elemento que tiene el atributo data-cy
.should('exist') // Verifica que el input exista
.should('be.visible') // Verifica que el input exista
cy.get(selector, { timeout })
.should('exist')
.should('be.visible')
.click()
.then(($el) => {
if ($el.is('input')) {
return checkAriaControl($el);
}
checkAriaControl($el.find('input'));
cy.wrap($el.is('input') ? $el : $el.find('input'))
.invoke('attr', 'aria-controls')
.then((ariaControl) => selectItem(selector, option, ariaControl));
});
function checkAriaControl(input) {
cy.wrap(input)
.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
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();
});
});
}
});
function selectItem(selector, option, ariaControl, hasWrite = true) {
if (!hasWrite) cy.wait(100);
getItems(ariaControl).then((items) => {
const matchingItem = items
.toArray()
.find((item) => item.innerText.includes(option));
if (matchingItem) return cy.wrap(matchingItem).click();
if (hasWrite) cy.get(selector).clear().type(option, { delay: 0 });
return selectItem(selector, option, ariaControl, false);
});
}
function getItems(ariaControl, startTime = Cypress._.now(), timeout = 2500) {
// Se intenta obtener la lista de opciones del desplegable de manera recursiva
return cy
.get('#' + ariaControl, { timeout }) // Se asegura de que el selector aparezca en tiempo razonable
.get('#' + ariaControl, { timeout })
.should('exist')
.find('.q-item')
.should('exist')
.then(($items) => {
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.`,
@ -141,18 +138,6 @@ function getItems(ariaControl, startTime = Cypress._.now(), timeout = 2500) {
});
}
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);
cy.get(selector).click({ force: true });