Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clicking over hidden element in Cypress is not working

I am running into this problem without finding a good solution. I tried every post that I found in Google but without success. I am isolating the problem so I can share you the exact point. I am searching an specific product in "mercadolibre.com" and I want to sort the list from the lower price to the maximum. To do so you can enter directly here and clicking over the option "Menor Precio".

enter image description here

Doing manually this just works fine but with Cypress I am not being able to do that. This script just runs fine but the last step seems to have no effect.

// 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})
    });
});;

Maybe Am I using a bad approach to refer the object?

Best regards!

like image 822
Nico Avatar asked Oct 22 '21 20:10

Nico


2 Answers

You may have noticed the polite discussion about dismissing the popups, is it necessary or not.

I believe not, and to rely on dismissing the popups will give a flaky test.

I also think the issue is as @DizzyAl says, the event handler on the dropdown button is not attached until late in the page load.

This is what I tried, using retries to give a bit more time for page loading.


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})
});
like image 187
user16695029 Avatar answered Sep 19 '22 23:09

user16695029


I would retry menu until options become visible

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})
})
like image 24
Mihi Avatar answered Sep 20 '22 23:09

Mihi