Bird
Raised Fist0
Cypresstesting~15 mins

cy.trigger() for custom events in Cypress - Build an Automation Script

Choose your learning style10 modes available
Trigger a custom event and verify event handler execution
Preconditions (2)
Step 1: Visit the web page containing the button.
Step 2: Use cy.get() to select the button with id 'custom-event-btn'.
Step 3: Trigger the custom event 'myCustomEvent' on the button using cy.trigger().
Step 4: Verify that the button text changes to 'Event Triggered!'.
✅ Expected Result: The button text changes to 'Event Triggered!' after the custom event 'myCustomEvent' is triggered.
Automation Requirements - Cypress
Assertions Needed:
Verify the button text changes to 'Event Triggered!' after triggering the custom event.
Best Practices:
Use cy.get() with id selector for element selection.
Use cy.trigger() to fire the custom event.
Use should() assertion to verify the text change.
Avoid hardcoded waits; rely on Cypress automatic waits.
Automated Solution
Cypress
describe('Custom Event Trigger Test', () => {
  it('should trigger custom event and verify button text change', () => {
    cy.visit('/');
    cy.get('#custom-event-btn')
      .trigger('myCustomEvent')
      .should('have.text', 'Event Triggered!');
  });
});

This test visits the page, selects the button by its id, triggers the custom event 'myCustomEvent' on it, and then asserts that the button's text changes to 'Event Triggered!'.

Using cy.get('#custom-event-btn') ensures we select the correct element. The cy.trigger('myCustomEvent') fires the custom event as if it happened naturally. The should('have.text', 'Event Triggered!') assertion waits automatically until the text changes, making the test reliable without manual waits.

Common Mistakes - 3 Pitfalls
{'mistake': 'Using incorrect selector like class instead of id when the element has an id.', 'why_bad': 'This can select the wrong element or no element, causing the test to fail or be flaky.', 'correct_approach': "Use the exact id selector with cy.get('#custom-event-btn') to select the button reliably."}
{'mistake': 'Triggering the event on the wrong element or without selecting any element.', 'why_bad': "The event won't fire on the intended element, so the event handler won't run and the test will fail.", 'correct_approach': 'Always chain cy.trigger() after cy.get() to ensure the event fires on the correct element.'}
Using cy.wait() with fixed time instead of relying on Cypress automatic waits.
Bonus Challenge

Now add data-driven testing to trigger the custom event on three different buttons with ids 'custom-event-btn1', 'custom-event-btn2', and 'custom-event-btn3', verifying each changes text to 'Event Triggered!'.

Show Hint

Practice

(1/5)
1. What does cy.trigger() do in Cypress when testing web pages?
easy
A. It simulates user or custom events on page elements.
B. It creates a new database entry.
C. It styles the page elements dynamically.
D. It fetches data from an API.

Solution

  1. Step 1: Understand the purpose of cy.trigger()

    cy.trigger() is used in Cypress to simulate events like clicks or custom events on elements.
  2. Step 2: Compare options with the purpose

    Only the option 'It simulates user or custom events on page elements.' correctly describes simulating events. Other options describe unrelated actions.
  3. Final Answer:

    It simulates user or custom events on page elements. -> Option A
  4. Quick Check:

    cy.trigger() = simulate events [OK]
Hint: Remember: cy.trigger() fires events on elements [OK]
Common Mistakes:
  • Thinking cy.trigger() fetches data
  • Confusing cy.trigger() with styling functions
  • Assuming it modifies database entries
2. Which of the following is the correct syntax to trigger a custom event named myEvent on an element with id #btn in Cypress?
easy
A. cy.get('#btn').trigger('myEvent')
B. cy.trigger('#btn', 'myEvent')
C. cy.get('#btn').fire('myEvent')
D. cy.triggerEvent('#btn', 'myEvent')

Solution

  1. Step 1: Recall correct method chaining in Cypress

    To trigger an event on an element, first select it with cy.get(), then call .trigger() with the event name.
  2. Step 2: Check each option's syntax

    cy.get('#btn').trigger('myEvent') matches the correct pattern. The other options use invalid Cypress commands.
  3. Final Answer:

    cy.get('#btn').trigger('myEvent') -> Option A
  4. Quick Check:

    Use cy.get().trigger() for events [OK]
Hint: Always select element first with cy.get(), then trigger event [OK]
Common Mistakes:
  • Calling trigger() directly on cy without get()
  • Using non-existent methods like fire() or triggerEvent()
  • Passing selector as first argument to trigger()
3. Consider the code:
cy.get('#input').trigger('customChange', { detail: { value: 42 } })

What will be the effect of this code in a Cypress test?
medium
A. It causes a syntax error because of the detail object.
B. It changes the value of #input to 42 directly.
C. It triggers a click event instead of 'customChange'.
D. It triggers a 'customChange' event on #input with extra data { value: 42 }.

Solution

  1. Step 1: Understand event triggering with extra options

    The trigger method can send custom events with additional data via the detail property.
  2. Step 2: Analyze the code effect

    The code triggers 'customChange' on '#input' and passes { value: 42 } as event detail. It does not change input value directly or cause syntax errors.
  3. Final Answer:

    It triggers a 'customChange' event on #input with extra data { value: 42 }. -> Option D
  4. Quick Check:

    trigger(event, {detail}) sends custom event data [OK]
Hint: Use detail object to pass extra event data [OK]
Common Mistakes:
  • Assuming trigger changes element properties directly
  • Confusing event name with standard events like click
  • Thinking detail causes syntax errors
4. You wrote this Cypress code:
cy.get('#form').trigger('submit', { bubbles: true })

But the form does not submit as expected. What is the likely problem?
medium
A. You must pass the event object manually; trigger() cannot send options.
B. The event name 'submit' is invalid for trigger().
C. The 'submit' event must be triggered on the form element, but it requires calling .submit() instead.
D. The bubbles option should be false for submit events.

Solution

  1. Step 1: Understand how form submission works in Cypress

    Triggering 'submit' event alone may not cause form submission; Cypress recommends using .submit() command.
  2. Step 2: Analyze the code and options

    The 'submit' event must be triggered on the form element, but it requires calling .submit() instead. correctly states that triggering 'submit' event is not enough; .submit() triggers native submission. Other options are incorrect about event validity and options usage.
  3. Final Answer:

    The 'submit' event must be triggered on the form element, but it requires calling .submit() instead. -> Option C
  4. Quick Check:

    Use .submit() to submit forms, not just trigger('submit') [OK]
Hint: Use .submit() for forms, not just trigger('submit') [OK]
Common Mistakes:
  • Assuming trigger('submit') submits form
  • Misusing bubbles option for submit event
  • Thinking trigger() cannot send options
5. You want to test a custom event dataReady on a div with id #container. This event should carry a payload with a list of items. Which code correctly triggers this event with the payload { items: [1, 2, 3] } and ensures event bubbles up?
hard
A. cy.get('#container').trigger('dataReady', { items: [1, 2, 3], bubbles: true })
B. cy.get('#container').trigger('dataReady', { detail: { items: [1, 2, 3] }, bubbles: true })
C. cy.get('#container').trigger('dataReady', { detail: [1, 2, 3], bubbles: false })
D. cy.get('#container').trigger('dataReady', { payload: { items: [1, 2, 3] }, bubbles: true })

Solution

  1. Step 1: Recall how to pass custom data in events

    Custom event data must be inside a detail object when triggering events.
  2. Step 2: Check bubbling option and payload structure

    cy.get('#container').trigger('dataReady', { detail: { items: [1, 2, 3] }, bubbles: true }) correctly uses detail with the payload and sets bubbles: true. Other options either miss detail or have wrong payload keys or bubbling.
  3. Final Answer:

    cy.get('#container').trigger('dataReady', { detail: { items: [1, 2, 3] }, bubbles: true }) -> Option B
  4. Quick Check:

    Use detail for payload and bubbles:true to bubble event [OK]
Hint: Put payload inside detail and set bubbles:true to bubble event [OK]
Common Mistakes:
  • Passing payload outside detail object
  • Forgetting to set bubbles:true for bubbling
  • Using wrong property names like payload instead of detail
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard