0
0
Cypresstesting~10 mins

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

Choose your learning style9 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.