Cypress does not have a cy.hover() command. See Issue #10.
If cy.hover() is used, an error will display and redirect you to this page.
Sometimes an element has specific logic on hover and you do need to "hover" in Cypress. Maybe the element doesn't even display to be clickable until you hover over another element.
Oftentimes you can use .trigger(), .invoke() or cy.wrap() to show the element before you perform the action.
Check out our example recipe on testing hover and working with hidden elements. Also take a look at the cypress-real-events plugin that provides native events like hover and swipe in Chromium browsers.
If the hover behavior depends on a JavaScript event like mouseover, you can trigger the event to achieve that behavior.
Using
.trigger()will only affect events in JavaScript and will not trigger any effects in CSS.
As a workaround, check out the recipe leveraging Chrome remote debugging to set pseudo classes like hover.
mouseover event to get popover to displaycy.get('.menu-item').trigger('mouseover')
cy.get('.popover').should('be.visible')
cy.get('.hidden').invoke('show').click()
You can also force the action to be performed on the element regardless of whether the element is visible or not.
cy.get('.hidden').click({ force: true })
cy.get('.checkbox').check({ force: true })
Although Cypress does not have a built-in cy.hover(), it is a reserved command. Therefore, to add cy.hover() as a custom command; Cypress.Commands.overwrite() should be used instead of Cypress.Commands.add().
Cypress.Commands.overwrite('hover', (originalFn, ...otherArgs) => {})
We show how to use the .trigger command and the cypress-real-events plugin to test elements with hover states in the video Cypress hover example, with the source code available in the bahmutov/cy-hover-example repo.
.invoke().trigger()cy.wrap()Cypress.Commands.overwrite()
© 2017 Cypress.io
Licensed under the MIT License.
https://docs.cypress.io/api/commands/hover