title |
---|
trigger |
Trigger an event on a DOM element.
.trigger(eventName)
.trigger(eventName, position)
.trigger(eventName, options)
.trigger(eventName, x, y)
.trigger(eventName, position, options)
.trigger(eventName, x, y, options)
{% fa fa-check-circle green %} Correct Usage
cy.get('a').trigger('mousedown') // Trigger mousedown event on link
{% fa fa-exclamation-triangle red %} Incorrect Usage
cy.trigger('touchstart') // Errors, cannot be chained off 'cy'
cy.location().trigger('mouseleave') // Errors, 'location' does not yield DOM element
{% fa fa-angle-right %} eventName (String)
The name of the event
to be triggered on the DOM element.
{% fa fa-angle-right %} position (String)
The position where the event should be triggered. The center
position is the default position. Valid positions are topLeft
, top
, topRight
, left
, center
, right
, bottomLeft
, bottom
, and bottomRight
.
{% imgTag /img/api/coordinates-diagram.jpg "cypress-command-positions-diagram" %}
{% fa fa-angle-right %} x (Number)
The distance in pixels from element's left to trigger the event.
{% fa fa-angle-right %} y (Number)
The distance in pixels from element's top to trigger the event.
{% fa fa-angle-right %} options (Object)
Pass in an options object to change the default behavior of .trigger()
.
Option | Default | Description |
---|---|---|
log |
true |
{% usage_options log %} |
force |
false |
{% usage_options force trigger %} |
bubbles |
true |
Whether the event bubbles |
cancelable |
true |
Whether the event is cancelable |
timeout |
{% url defaultCommandTimeout configuration#Timeouts %} |
{% usage_options timeout .trigger %} |
You can also include arbitrary event properties (e.g. clientX
, shiftKey
) and they will be attached to the event. Passing in coordinate arguments (clientX
, pageX
, etc) will override the position coordinates.
{% yields same_subject .trigger %}
The DOM element must be in an "interactable" state prior to the triggered event happening (it must be visible and not disabled).
cy.get('button').trigger('mouseover') // yields 'button'
cy.get('.target').trigger('mousedown')
cy.wait(1000)
cy.get('.target').trigger('mouseleave')
To simulate drag and drop using jQuery UI sortable requires pageX
and pageY
properties along with which:1
.
cy.get('[data-cy=draggable]')
.trigger('mousedown', { which: 1, pageX: 600, pageY: 100 })
.trigger('mousemove', { which: 1, pageX: 600, pageY: 600 })
.trigger('mouseup')
{% note info %} {% url 'Check out our example recipe triggering mouse and drag events to test dragging and dropping' recipes#Drag-and-Drop %} {% endnote %}
To interact with a range input (slider), we need to set its value and then trigger the appropriate event to signal it has changed.
Below we invoke jQuery's val()
method to set the value, then trigger the change
event.
Note that some implementations may rely on the input
event instead, which is fired as a user moves the slider, but is not supported by some browsers.
cy.get('input[type=range]').as('range')
.invoke('val', 25)
.trigger('change')
cy.get('@range').siblings('p').should('have.text', '25')
cy.get('button').trigger('mousedown', 'topRight')
cy.get('button').trigger('contextmenu', 15, 40)
By default, the event will bubble up the DOM tree. This will prevent the event from bubbling.
cy.get('button').trigger('mouseover', { bubbles: false })
This overrides the default auto-positioning based on the element itself. Useful for events like mousemove
where you need the position to be outside the element itself.
cy.get('button').trigger('mousemove', { clientX: 200, clientY: 300 })
.trigger()
is an "action command" that follows all the rules {% url 'defined here' interacting-with-elements %}.
cy.trigger()
is meant to be a low-level utility that makes triggering events easier than manually constructing and dispatching them. Since any arbitrary event can be triggered, Cypress tries not to make any assumptions about how it should be triggered. This means you'll need to know the implementation details (which may be in a 3rd party library) of the event handler(s) receiving the event and provide the necessary properties.
In other words, what's the difference between:
cy.get('button').trigger('focus')
cy.get('button').focus()
// ... or ...
cy.get('button').trigger('click')
cy.get('button').click()
Both types commands will first verify element actionability, but only the "true" action commands will implement all of the default actions of the browser, and additionally perform low level actions to fulfill what's defined in the spec.
.trigger()
will only fire the corresponding event and do nothing else.
That means that your event listener callbacks will be invoked, but don't expect the browser to actually "do" anything for these events. For the most part, it shouldn't matter, which is why .trigger()
is an excellent stop-gap if the command / event you're looking for hasn't been implemented yet.
{% requirements dom .trigger %}
{% assertions actions .trigger %}
{% timeouts actions .trigger %}
Trigger a change
event on input type='range'
cy.get('.trigger-input-range')
.invoke('val', 25)
.trigger('change')
The commands above will display in the Command Log as:
{% imgTag /img/api/trigger/command-log-trigger.png "command log trigger" %}
When clicking on trigger
within the command log, the console outputs the following:
{% imgTag /img/api/trigger/console-log-trigger.png "console log trigger" %}
{% history %}
{% url "0.20.0" changelog#0-20-0 %} | .trigger()
command added
{% endhistory %}
- {% url
.blur()
blur %} - {% url
.check()
check %} - {% url
.click()
click %} - {% url
.focus()
focus %} - {% url
.select()
select %} - {% url
.submit()
submit %} - {% url
.type()
type %} - {% url
.uncheck()
uncheck %}