QA Interview Hub · Knowledge base · Quizzes · Українська

Cypress › Extra questions from common lists

How do you do hover and drag-and-drop?

Cypress has no native hover() because it fires synthetic JS events, while the CSS :hover pseudo-class depends on the real OS cursor. So .trigger('mouseover') runs handlers but does not open a menu held open only by CSS. For a real hover use the cypress-real-events plugin, and HTML5 drag-and-drop needs a chain of events with dataTransfer. Dropping a file onto a zone is covered by selectFile.

Cypress · Practice and details

A menu opens on hover, and the test «hovers» and sees nothing. What is wrong?

The menu has a rule .menu:hover .submenu { display: block }. The test calls .trigger('mouseover') and the submenu stays hidden. Same with dragging a card between columns: the events fire, the card does not move. Cypress has no native hover(), and the reason explains what to do.

How it works and why

Cypress runs inside the page and fires synthetic DOM events through JavaScript. mouseover handlers run, but the CSS :hover pseudo-class depends on the real operating-system cursor, which a synthetic event does not set. So .trigger('mouseover') runs JS logic but does not show a menu held open only by CSS. Drag-and-drop on the HTML5 API (draggable) also needs a chain of events with a dataTransfer object, while pointer-event libraries expect their own coordinates and sequence.

Synthetic event and real cursor

not always enough

trigger('mouseover')

A JS event in the page. CSS :hover is not switched on.

works

realHover()

The cypress-real-events plugin sends real browser events through the debugging protocol, so :hover applies.

Figure 1. The plugin works through the browser debugging protocol; see the plugin README for supported browsers.

Example: before → after

// before: a JS event, the CSS menu did not open
cy.get('[data-cy=menu]').trigger('mouseover');
cy.get('[data-cy=submenu]').should('be.visible'); // fails

// option 1: the cypress-real-events plugin
cy.get('[data-cy=menu]').realHover();
cy.get('[data-cy=submenu]').should('be.visible');

// option 2: show the element directly (a workaround, not a hover check)
cy.get('[data-cy=submenu]').invoke('show').should('be.visible');

Option 2 only proves the submenu can be shown, not that it appears on hover. For the hover itself you need option 1.

// HTML5 drag-and-drop: events with dataTransfer
const dataTransfer = new DataTransfer();
cy.get('[data-cy=card]').trigger('dragstart', { dataTransfer });
cy.get('[data-cy=column-done]')
  .trigger('dragover', { dataTransfer })
  .trigger('drop', { dataTransfer });
cy.get('[data-cy=column-done]').should('contain', 'Task 1');

For libraries built on mouse events you need the sequence mousedown → mousemove with coordinates → mouseup (pointer-event libraries need pointerdown → pointermove → pointerup: trigger dispatches only the event you name), often with several intermediate mousemove events. It is easy to lose hours here: a ready plugin or real events are usually faster.

// a mouse-event library: a sequence with coordinates
cy.get('[data-cy=handle]')
  .trigger('mousedown', { button: 0, clientX: 100, clientY: 200 })
  .trigger('mousemove', { clientX: 140, clientY: 200 }) // intermediate step
  .trigger('mousemove', { clientX: 300, clientY: 200 })
  .trigger('mouseup', { force: true });
cy.get('[data-cy=slider-value]').should('have.text', '60');

The cypress-real-events plugin sends events through the browser debugging protocol, so it works only in Chromium-based browsers (the README says Firefox is not supported): before building a suite on it, check your CI browsers.

Limits and alternatives

  • Dropping a file onto a drop zone does not need trigger: cy.get(...).selectFile('file.pdf', { action: 'drag-drop' }).
  • A hover that only shows a tooltip can often be covered by a unit or component test, and e2e checks the end effect.
  • Playwright has it built in through real browser events: locator.hover() and locator.dragTo(target).

Common mistakes

  • Believing trigger('mouseover') equals hovering: for a CSS menu it is useless.
  • Forgetting dataTransfer in HTML5 drag-and-drop: drop fires without data and the library logic gets nothing.
  • Hiding the problem with { force: true } or invoke('show') and never checking real user behaviour.

Check yourself

Why does trigger('mouseover') not open a menu driven by CSS :hover?

Answer

It is a synthetic JS event: it runs handlers but does not change the cursor state that :hover depends on. Real events (cypress-real-events) are needed.

What does invoke('show') verify instead of hover and what does it not verify?

Answer

It forces the element to show and lets you check content or click the submenu. It does not prove that the menu opens on hover: that link stays unverified.

In an interview. Junior: Cypress has no native hover: trigger('mouseover') runs JS handlers but not CSS :hover. I do drag-and-drop with trigger and the needed mouse events. Middle/Senior: trigger only fires synthetic JS events, so it does not switch on a real CSS hover. For that I use cypress-real-events, and for HTML5 drag I add dataTransfer. In Playwright hover() and dragTo() are built in.

More articles in this chapter