How to test 'HOVER' using Cypress(.trigger('mouseover') doesn't work)

17.4k views Asked by At

I wonder if it's possible to test coverable dropdown menu bar using Cypress.

For example, when you go to Ebay(https://www.ebay.com), you'll see the alert icon at the top-right corner of the page and if you hover on that element, sign-in notification box will show up.

Here is my code and I followed what Cypress told me to do but I got an error like...

AssertionError Timed out retrying: Expected to find element: #ghn-err, but never found it.

 it('ebay hover test', () => {
        cy.visit('https://www.ebay.com')
        // 1. alert icon hover test.
        cy.get('#gh-Alerts-i').trigger('mouseover')
        cy.get('#ghn-err').should('be.visible')
        // 2. This is my another test.
        // cy.get('#gh-eb-My > .gh-menu > .gh-eb-li-a > .gh-sprRetina').trigger('mouseover')
        //cy.get('#gh-eb-My-o > .gh-eb-oa thrd').should('be.visible')
    })

4

There are 4 answers

1
Pedro Bezanilla On

For me mouseover works. It's a known Cypress bug pending to be solved. However, try these:

cy.get('#gh-Alerts-i').trigger('onmouseover')
cy.get('#gh-Alerts-i').trigger('mouseenter')
cy.get('#gh-Alerts-i').invoke('trigger', 'contextmenu')
cy.get('#gh-Alerts-i').rightclick();
0
kano On

If .trigger() does not work for you, it could also be that your display logic is controlled via CSS instead (for example with .class:hover). You could then rework the display logic to toggle a class with mouseenter and mouseleave events instead.

1
Carlos Oliva On

I had the same issue some time ago and this method worked for me:

cy.get('myElementLocator').rightclick();

0
Don Diego On

I had the same problem, I finally found this plugin that does exactly what I was looking for: do real events instead of simulated ones (simulated == launched with javascript).

https://github.com/dmtrKovalenko/cypress-real-events#cyrealhover

Just install it, add a line to cypress/support/index.js file (do not forget! See 'install' paragraph at the top of the page) and use cy.get(<element>).realHover().