clicked after the test runs?let clicked = false; cy.document().then(doc => { const button = doc.createElement('button'); button.textContent = 'Click me'; button.addEventListener('click', () => { clicked = true; }); doc.body.appendChild(button); cy.get('button').realClick(); }).then(() => { cy.wrap(null).then(() => { // Check clicked value here }); });
The realClick() command from cypress-real-events triggers the native click event on the button element. This causes the event listener to run and sets clicked to true.
pressedEnter to true. Which assertion correctly verifies this after using realPress('Enter')?let pressedEnter = false; cy.get('input').then($input => { $input[0].addEventListener('keydown', e => { if (e.key === 'Enter') pressedEnter = true; }); cy.wrap($input).realPress('Enter'); });
Since pressedEnter is a variable outside the DOM, the assertion must be wrapped in cy.wrap(null).should() to wait and check its value. Option A correctly asserts it is true.
let hovered = false; cy.get('div#hover-target').realHover().then($div => { $div[0].addEventListener('mouseover', () => { hovered = true; }); });
The event listener is added inside the then callback, but realHover() is called outside that callback, so it runs before the listener is attached. The event is missed.
Option B uses realMouseDown(), realMouseMove(), and realMouseUp() to simulate the native drag sequence. Other options either use synthetic events or invalid commands.
realClick() are preferred when testing native event handlers in web applications.Cypress's default .click() triggers synthetic events that may not behave exactly like native browser events. cypress-real-events triggers real native events, ensuring event listeners that depend on native event properties and bubbling behave correctly.