Files
cypress/docs/source/api/commands/prev.md
T
2017-05-26 16:10:23 -04:00

2.7 KiB

title, comments, description
title comments description
prev true

Get the immediately preceding sibling of each element in a set of the elements.

Syntax

.prev()
.prev(selector)
.prev(options)
.prev(selector, options)

Usage

.prev() requires being chained off another cy command that yields a DOM element or set of DOM elements.

{% fa fa-check-circle green %} Valid Usage

cy.get('tr.highlight').prev() // Yield previous 'tr'

{% fa fa-exclamation-triangle red %} Invalid Usage

cy.prev()                // Errors, cannot be chained off 'cy'
cy.getCookies().prev()   // Errors, 'getCookies' does not yield DOM element

Arguments

{% fa fa-angle-right %} selector (String selector)

A selector used to filter matching DOM elements.

{% fa fa-angle-right %} options (Object)

Pass in an options object to change the default behavior of .prev().

Option Default Notes
log true whether to display command in command log
timeout defaultCommandTimeout Total time to retry getting the element

Yields

.prev() yields the new DOM element(s) found by the command.

Timeout

.prev() will continue to look for the previous element for the duration of the defaultCommandTimeout

Examples

Previous

Find the previous element of the element with class of active

<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()

Selector

Find the previous element with a class of active

<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')

Command Log

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:

screen shot 2015-11-29 at 12 46 57 pm

When clicking on prev within the command log, the console outputs the following:

screen shot 2015-11-29 at 12 47 09 pm

See also