Bird
Raised Fist0
Cypresstesting~10 mins

cy.trigger() for custom events in Cypress - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to trigger a custom event named 'myEvent' on the element with id 'button'.

Cypress
cy.get('#button').[1]('myEvent')
Drag options to blanks, or click blank then click option'
Ainvoke
Bclick
Ctype
Dtrigger
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'click' instead of 'trigger' for custom events.
Using 'type' which is for typing text, not triggering events.
2fill in blank
medium

Complete the code to trigger a custom event 'myEvent' with additional data { key: 'value' } on the element with class 'item'.

Cypress
cy.get('.item').trigger('myEvent', [1])
Drag options to blanks, or click blank then click option'
A{ key: 'value' }
B[ key: 'value' ]
C'key: value'
Dkey: 'value'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing data as a string instead of an object.
Using square brackets which define arrays, not objects.
3fill in blank
hard

Fix the error in the code to correctly trigger a custom event 'customEvent' with bubbling enabled on the element with id 'box'.

Cypress
cy.get('#box').trigger('customEvent', { [1]: true })
Drag options to blanks, or click blank then click option'
AbubbleEvent
Bbubbles
Cbubble
DeventBubbles
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'bubble' instead of 'bubbles'.
Using incorrect property names that do not enable bubbling.
4fill in blank
hard

Fill both blanks to trigger a custom event 'update' with bubbling and cancelable options set to true on the element with class 'card'.

Cypress
cy.get('.card').trigger('update', { [1]: true, [2]: true })
Drag options to blanks, or click blank then click option'
Abubbles
Bcancelable
Ccancel
Dbubble
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'bubble' instead of 'bubbles'.
Using 'cancel' instead of 'cancelable'.
5fill in blank
hard

Fill all three blanks to trigger a custom event 'refresh' with bubbling and cancelable set to true, and pass detail data { id: 123 } on the element with id 'container'.

Cypress
cy.get('#container').trigger('refresh', { [1]: true, [2]: true, [3]: { id: 123 } })
Drag options to blanks, or click blank then click option'
Abubbles
Bcancelable
Cdetail
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'data' instead of 'detail' for custom event data.
Omitting 'cancelable' or 'bubbles' properties.

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