Files
cypress/docs/source/api/commands/prev.md
T

2.2 KiB

title, comments, description
title comments description
prev true

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

Returns the new DOM element(s) found by the command.
Timeout cy.prev will retry for the duration of the defaultCommandTimeout

cy.prev()

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

cy.prev( selector )

Get the immediately preceding sibling of each element in the set of matched elements filtered by selector.

Options

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

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

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

Usage

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>
// returns <li>Lorikeets</li>
cy.get(".active").prev()

Selector Usage

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>
// returns <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

Related

  1. next