title | description | sidebar_label | slug |
---|---|---|---|
prev | Cypress Documentation |
Get the immediately preceding sibling of each element in a set of the elements in Cypress. |
prev |
/api/commands/prev |
Get the immediately preceding sibling of each element in a set of the elements.
:::info
The querying behavior of this command matches exactly how
.prev()
works in jQuery.
:::
.prev()
.prev(selector)
.prev(options)
.prev(selector, options)
Correct Usage
cy.get('tr.highlight').prev() // Yield previous 'tr'
Incorrect Usage
cy.prev() // Errors, cannot be chained off 'cy'
cy.getCookies().prev() // Errors, 'getCookies' does not yield DOM element
selector (String selector)
A selector used to filter matching DOM elements.
options (Object)
Pass in an options object to change the default behavior of .prev()
.
Option | Default | Description |
---|---|---|
log |
true |
Displays the command in the Command log |
timeout |
defaultCommandTimeout |
Time to wait for .prev() to resolve before timing out |
.prev()
yields the new DOM element(s) it found..prev()
is a query, and it is safe to chain further commands.
<ul>
<li>Cockatiels</li>
<li>Lorikeets</li>
<li class="active">Cockatoos</li>
<li>Conures</li>
<li>Eclectus</li>
</ul>
// yields <li>Lorikeets</li>
cy.get('.active').prev()
<ul>
<li>Cockatiels</li>
<li>Lorikeets</li>
<li class="active">Cockatoos</li>
<li>Conures</li>
<li>Eclectus</li>
</ul>
// yields <li>Cockatoos</li>
cy.get('li').prev('.active')
.prev()
requires being chained off a command that yields DOM element(s).
.prev()
will automatically retry until the element(s) exist in the DOM..prev()
will automatically retry until all chained assertions have passed.
.prev()
can time out waiting for the element(s) to exist in the DOM..prev()
can time out waiting for assertions you've added to pass.
Find the previous element of the active li
cy.get('.left-nav').find('li.active').prev()
The commands above will display in the Command Log as:
When clicking on prev
within the command log, the console outputs the
following: