Challenge - 5 Problems
Custom Event Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this Cypress command?
Consider the following Cypress test code snippet that triggers a custom event on a button element. What will be the value of the variable
eventTriggered after running this code?Cypress
let eventTriggered = false; cy.get('button#submit').then($btn => { $btn[0].addEventListener('myCustomEvent', () => { eventTriggered = true; }); }); cy.get('button#submit').trigger('myCustomEvent');
Attempts:
2 left
💡 Hint
Remember that
trigger fires the event listeners attached to the element.✗ Incorrect
The
trigger command fires the custom event myCustomEvent on the button element, which sets eventTriggered to true.📝 Syntax
intermediate2:00remaining
Which option correctly triggers a custom event with additional data?
You want to trigger a custom event named
dataReady on a div with id container and pass an object { loaded: true } as event detail. Which of the following Cypress commands is correct?Attempts:
2 left
💡 Hint
Custom event data should be passed inside the
detail property.✗ Incorrect
The
trigger command passes event data inside an object with the detail key for custom events.🔧 Debug
advanced2:00remaining
Why does this custom event not trigger the listener?
Given this code, why does the event listener not run when
cy.trigger() is called?
cy.get('#box').trigger('customEvent', { detail: { value: 42 } });
cy.get('#box').then($el => {
$el[0].addEventListener('customEvent', () => {
console.log('Event fired');
});
});Attempts:
2 left
💡 Hint
Think about the timing of attaching listeners and triggering events in Cypress commands.
✗ Incorrect
The listener is attached inside a
then callback, which runs asynchronously. The trigger command may run before the listener is attached, so the event is not caught.❓ optimization
advanced2:00remaining
How to optimize triggering multiple custom events on the same element?
You need to trigger three different custom events
start, progress, and end on a div with id progressBar. Which approach is the most efficient in Cypress?Attempts:
2 left
💡 Hint
Minimize DOM queries and use Cypress chaining.
✗ Incorrect
Chaining triggers after a single
get reduces DOM queries and keeps commands in Cypress chain, improving efficiency.🧠 Conceptual
expert3:00remaining
What is the correct way to listen for a custom event triggered by
cy.trigger() in Cypress?You want to verify that a custom event
dataLoaded is triggered on an element during a test. Which approach correctly listens and asserts the event firing?Attempts:
2 left
💡 Hint
Attach an event listener that sets a flag before triggering and assert the flag after.
✗ Incorrect
Option A correctly attaches an event listener inside a
then callback that sets a flag variable, triggers the event, then asserts the flag to verify the event fired.