title |
---|
last |
Get the last DOM element within a set of DOM elements.
{% note info %}
The querying behavior of this command matches exactly how {% url .last()
http://api.jquery.com/last %} works in jQuery.
{% endnote %}
.last()
.last(options)
{% fa fa-check-circle green %} Correct Usage
cy.get('nav a').last() // Yield last link in nav
{% fa fa-exclamation-triangle red %} Incorrect Usage
cy.last() // Errors, cannot be chained off 'cy'
cy.getCookies().last() // Errors, 'getCookies' does not yield DOM element
{% fa fa-angle-right %} options (Object)
Pass in an options object to change the default behavior of .last()
.
Option | Default | Description |
---|---|---|
log |
true |
{% usage_options log %} |
timeout |
{% url defaultCommandTimeout configuration#Timeouts %} |
{% usage_options timeout .last %} |
{% yields changes_dom_subject_or_subjects .last %}
<ul>
<li class="one">Knick knack on my thumb</li>
<li class="two">Knick knack on my shoe</li>
<li class="three">Knick knack on my knee</li>
<li class="four">Knick knack on my door</li>
</ul>
// yields <li class="four">Knick knack on my door</li>
cy.get('li').last()
{% requirements dom .last %}
{% assertions existence .last %}
{% timeouts existence .last %}
Find the last button in the form
cy.get('form').find('button').last()
The commands above will display in the Command Log as:
{% imgTag /img/api/last/find-the-last-button-in-a-form.png "Command Log for last" %}
When clicking on last
within the command log, the console outputs the following:
{% imgTag /img/api/last/inspect-last-element-in-console.png "Console Log for last" %}
- {% url
.eq()
eq %} - {% url
.first()
first %}