简体   繁体   中英

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

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

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

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

Use the above package, it should work.

Note: this works only with chrome browser.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM