Skip to content

Latest commit

 

History

History
128 lines (86 loc) · 2.55 KB

File metadata and controls

128 lines (86 loc) · 2.55 KB
title
prev

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

{% note info %} The querying behavior of this command matches exactly how {% url .prev() http://api.jquery.com/prev %} works in jQuery. {% endnote %}

Syntax

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

Usage

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

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

{% fa fa-exclamation-triangle red %} Incorrect 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 Description
log true {% usage_options log %}
timeout {% url defaultCommandTimeout configuration#Timeouts %} {% usage_options timeout .prev %}

Yields {% helper_icon yields %}

{% yields changes_dom_subject_or_subjects .prev %}

Examples

No Args

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

Rules

Requirements {% helper_icon requirements %}

{% requirements dom .prev %}

Assertions {% helper_icon assertions %}

{% assertions existence .prev %}

Timeouts {% helper_icon timeout %}

{% timeouts existence .prev %}

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:

{% imgTag /img/api/prev/find-prev-element-in-list-of-els.png "Command Log prev" %}

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

{% imgTag /img/api/prev/previous-element-in-console-log.png "Console Log prev" %}

See also

  • {% url .next() next %}
  • {% url .prevAll() prevall %}
  • {% url .prevUntil() prevuntil %}