Me encuentro con este problema sin encontrar una buena solución. Intenté todas las publicaciones que encontré en Google pero sin éxito. Estoy aislando el problema para poder compartirte el punto exacto. Estoy buscando un producto específico en "mercadolibre.com" y quiero ordenar la lista desde el precio más bajo hasta el máximo. Para ello puede entrar directamente aquí y pinchando sobre la opción "Menor Precio".
Hacer esto manualmente funciona bien, pero con Cypress no puedo hacerlo. Este script funciona bien, pero el último paso parece no tener efecto.
// Activate intelligence /// <reference types="Cypress" /> describe("Main test suite for price control", () => { it("search scanners and order them by ascending price", () => { // Web access cy.visitAndWait("https://listado.mercadolibre.com.ar/scanner-blueetooth#D[A:scanner%20blueetooth"); // Order by ascending price cy.get(':nth-child(2) > .andes-list__item-first-column > .andes-list__item-text > .andes-list__item-primary').click({force: true}) }); });;¿Quizás estoy usando un mal enfoque para referir el objeto?
¡Saludos!
Es posible que haya notado la discusión cortés sobre descartar las ventanas emergentes, si es necesario o no.
Creo que no, y confiar en descartar las ventanas emergentes será una prueba inestable.
También creo que el problema es que, como dice @DizzyAl, el controlador de eventos en el botón desplegable no se adjunta hasta el final de la carga de la página.
Esto es lo que probé, usando reintentos para dar un poco más de tiempo para cargar la página.
Cypress.on('uncaught:exception', () => false) before(() => { cy.visit('http://mercadolibre.com.ar') cy.get(".nav-search-input").type("scanner bluetooth para auto{enter}"); cy.get('.ui-search-result__wrapper') // instead of cy.wait(3000), wait for the list }) it('gets the dropdown menu on 1st retry', {retries: 2}, () => { // Don't dismiss the popups cy.get('button.andes-dropdown__trigger').click() cy.contains('a.andes-list__item', 'Menor precio').click() // page reload happens cy.contains('.ui-search-result__wrapper:nth-child(1)', '$490', {timeout:20000}) });Volvería a intentar el menú hasta que las opciones se vuelvan visibles
Cypress.on('uncaught:exception', () => false) before(() => { cy.visit('https://listado.mercadolibre.com.ar/scanner-blueetooth#D%5BA:scanner%20blueetooth%5D') }) it('retries the menu open command', () => { function openMenu(attempts = 0) { if (attempts > 6) throw 'Failed open menu' return cy.get('button.andes-dropdown__trigger').click() .then(() => { const option = Cypress.$('.andes-list:visible') // is list visible if (!option.length) { openMenu(++attempts) // try again, up to 6 attempts } }) } openMenu().then(() => { cy.get('a.andes-list__item:contains(Menor precio)').click() }) // Verification cy.contains('.ui-search-result__wrapper:nth-child(1)', '$490', {timeout:20000}) })Parece que el evento de clic se agrega tarde al botón desplegable y el clic falla.
Consulte¿Cuándo puede comenzar la prueba? para una discusión.
Intenté adaptar el código del artículo, pero no pude hacerlo funcionar.
Sin embargo, funciona agregar un cy.wait() o un cy.intercept() para la última llamada de red.
cy.intercept('POST', 'https://bam-cell.nr-data.net/events/**').as('events') cy.visit('https://listado.mercadolibre.com.ar/scanner-blueetooth#D%5BA:scanner%20blueetooth%5D') cy.wait('@events', {timeout: 20000}) // Page has loaded, verify the top item price cy.contains('.ui-search-result__wrapper:nth-child(1)', '$1.385') // Open the sort-by dropdown cy.get('button.andes-dropdown__trigger').click() // Choose sort-by lowest price cy.contains('a.andes-list__item', 'Menor precio').click() // Verify first item has different price, long timeout to wait for page re-load cy.contains('.ui-search-result__wrapper:nth-child(1)', '$490', {timeout:20000})Es posible que pueda acortar la espera de carga de la página eligiendo una llamada de red diferente para esperar.