Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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')
    })

like image 703
loone96 Avatar asked Mar 06 '26 20:03

loone96


2 Answers

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();
like image 58
Pedro Bezanilla Avatar answered Mar 09 '26 10:03

Pedro Bezanilla


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().

like image 40
Don Diego Avatar answered Mar 09 '26 09:03

Don Diego