Categories
discuss

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!

Answer

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})
})
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..