Skip to main content

prevAll

Get all previous siblings of each DOM element in a set of matched DOM elements.

info

The querying behavior of this command matches exactly how .prevAll() works in jQuery.

Syntax

.prevAll()
.prevAll(selector)
.prevAll(options)
.prevAll(selector, options)

Usage

Correct Usage

cy.get('.active').prevAll() // Yield all links previous to `.active`

Incorrect Usage

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

Arguments

selector (String selector)

A selector used to filter matching DOM elements.

options (Object)

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

OptionDefaultDescription
logtrueDisplays the command in the Command log
timeoutdefaultCommandTimeoutTime to wait for .prevAll() to resolve before timing out

Yields

  • `.prevAll()` yields the new DOM element(s) it found.

Examples

No Args

Find all of the element's siblings before .third

<ul>
<li>apples</li>
<li>oranges</li>
<li class="third">bananas</li>
<li>pineapples</li>
<li>grapes</li>
</ul>
// yields [<li>apples</li>, <li>oranges</li>]
cy.get('.third').prevAll()

Selector

Find all of the previous siblings of each li. Keep only the ones with a class selected

<ul>
<li>apples</li>
<li>oranges</li>
<li>bananas</li>
<li class="selected">pineapples</li>
<li>grapes</li>
</ul>
// yields <li>pineapples</li>
cy.get('li').prevAll('.selected')

Rules

Requirements

  • `.prevAll()` requires being chained off a command that yields DOM element(s).

Assertions

  • `.prevAll()` will automatically [retry](/guides/core-concepts/retry-ability) until the element(s) [exist in the DOM](/guides/core-concepts/introduction-to-cypress#Default-Assertions)
  • `.prevAll()` will automatically [retry](/guides/core-concepts/retry-ability) until all chained assertions have passed

Timeouts

  • `.prevAll()` can time out waiting for the element(s) to [exist in the DOM](/guides/core-concepts/introduction-to-cypress#Default-Assertions).
  • `.prevAll()` can time out waiting for assertions you've added to pass.

Command Log

Find all elements before the .active li

cy.get('.left-nav').find('li.active').prevAll()

The commands above will display in the Command Log as:

Command Log prevAll

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

Console Log prevAll

See also