Bird
Raised Fist0
Cypresstesting~10 mins

cy.trigger() for custom events in Cypress - Test Execution Trace

Choose your learning style10 modes available
Test Overview

This test triggers a custom event on a button and verifies that the event handler updates the page content as expected.

Test Code - Cypress
Cypress
describe('Custom event trigger test', () => {
  it('should trigger a custom event and update text', () => {
    cy.visit('https://example.cypress.io/commands/actions')
    cy.get('#action-canvas')
      .trigger('myCustomEvent', { detail: { message: 'Hello Cypress!' } })
    cy.get('#custom-event-output').should('contain.text', 'Hello Cypress!')
  })
})
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test startsCypress test runner is ready—PASS
2Browser opens and navigates to 'https://example.cypress.io/commands/actions'Page with various action examples is loadedPage loaded successfullyPASS
3Find element with id 'action-canvas'Element '#action-canvas' is visible on the pageElement exists and is visiblePASS
4Trigger custom event 'myCustomEvent' on '#action-canvas' with detail { message: 'Hello Cypress!' }Custom event dispatched on the elementEvent handler updates '#custom-event-output' textPASS
5Find element with id 'custom-event-output' and check text contains 'Hello Cypress!'Element '#custom-event-output' shows updated textText contains 'Hello Cypress!'PASS
Failure Scenario
Failing Condition: The custom event handler is not attached or does not update the output element
Execution Trace Quiz - 3 Questions
Test your understanding
What does cy.trigger('myCustomEvent') do in this test?
AIt dispatches a custom event named 'myCustomEvent' on the selected element
BIt clicks the element with id 'myCustomEvent'
CIt types 'myCustomEvent' into the element
DIt waits for the element to appear
Key Result
Always verify that custom events trigger the expected changes by asserting the updated UI elements, ensuring your event handlers work correctly.

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