Skip to content

Cypress - Mouse Actions

Cypress provides several commands to simulate mouse interactions like hovers, clicks on elements that might be hidden or require specific states, and right-clicks. These are crucial for testing dynamic user interfaces.

Interacting with Elements Revealed on Hover

Section titled “Interacting with Elements Revealed on Hover”

Many web applications have elements, such as dropdown menus or tooltips, that only become visible when the mouse hovers over a trigger element.

The most common way to simulate a hover is by using the .trigger('mouseover') command on the element that reveals other elements. After triggering the hover, you can then interact with the newly visible element.

describe('Hover Interactions Test', () => {
it('should reveal and click a submenu item on hover', () => {
cy.visit('your-page-with-hover-menu.html'); // Replace with your test page
// Assuming '#menu-trigger' reveals '#submenu-item' on hover
cy.get('#menu-trigger').trigger('mouseover');
cy.get('#submenu-item').should('be.visible').click();
});
});

2. Using .invoke('show') for CSS-Hidden Elements

Section titled “2. Using .invoke('show') for CSS-Hidden Elements”

If an element is hidden using CSS (e.g., display: none;), and hover makes it appear (e.g., by changing display to block), you can use jQuery’s show() method via Cypress’s .invoke() command. This forces the element to become visible.

This method directly manipulates the DOM element’s style, which might not perfectly replicate user interaction but is effective for elements hidden this way.

describe('jQuery Show for Hidden Elements', () => {
it('should show and click a hidden element', () => {
cy.visit('your-page-with-hidden-elements.html'); // Replace
// Assuming '#hidden-content' is styled with 'display: none'
// and '.hover-container' is its parent or trigger
cy.get('.hover-container .hidden-content')
.invoke('show') // Force display:block or similar
.should('be.visible')
.click();
});
});

This is particularly useful if the hover mechanism is complex or if .trigger('mouseover') doesn’t consistently work for a specific implementation.

Cypress performs actionability checks before executing commands like .click(). This means an element must be visible, not disabled, not covered, etc. Sometimes, you might need to bypass these checks, for example, to click an element that is covered by another element (though this is generally a sign of a potential UI issue or a test that doesn’t fully mimic user behavior).

cy.get('#my-button').click({ force: true });

Caution: Use { force: true } sparingly. It can hide issues that a real user might face. It’s often better to ensure the element is truly actionable as a user would find it.

  • .rightclick(): Simulates a right-click on an element.
  • .dblclick(): Simulates a double-click on an element.
  • .trigger('mousedown'), .trigger('mouseup'): Simulate specific mouse button events.
  • .check(), .uncheck(): For checkboxes and radio buttons, these are preferred over .click() for clarity and specific behavior.
  • .select(): For selecting options in a <select> dropdown.
describe('Context Menu Test', () => {
it('should open context menu on right-click', () => {
cy.visit('your-page-with-context-menu.html'); // Replace
cy.get('#context-area').rightclick();
cy.get('#context-menu-option').should('be.visible').click();
});
});
  • Mimic User Behavior: Try to interact with elements as a user would. Use .trigger('mouseover') before clicking revealed items.
  • Visibility: Ensure elements are visible before interaction, leveraging Cypress’s built-in assertions (.should('be.visible')).
  • Avoid { force: true } if Possible: Only use it when you understand why the actionability check is failing and have a valid reason to bypass it.

Mastering these mouse action simulations is key to testing complex and interactive web applications with Cypress.