Skip to main content

parentsUntil

Get all ancestors of each DOM element in a set of matched DOM elements up to, but not including, the element provided.

info

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

Syntax

.parentsUntil(selector)
.parentsUntil(selector, filter)
.parentsUntil(selector, filter, options)
.parentsUntil(element)
.parentsUntil(element, filter)
.parentsUntil(element, filter, options)

Usage

Correct Usage

cy.get('p').parentsUntil('.article') // Yield parents of 'p' until '.article'

Incorrect Usage

cy.parentsUntil() // Errors, cannot be chained off 'cy'
cy.clock().parentsUntil('href') // Errors, 'clock' does not yield DOM elements

Arguments

selector (String selector)

The selector where you want finding parent ancestors to stop.

element (DOM node, jQuery Object)

The element where you want finding parent ancestors to stop.

filter (String selector)

A selector used to filter matching DOM elements.

options (Object)

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

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

Yields

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

Examples

Selector

Find all of the .active element's ancestors until .nav

<ul class="nav">
<li>
<a href="#">Clothes</a>
<ul class="menu">
<li>
<a href="/shirts">Shirts</a>
</li>
<li class="active">
<a href="/pants">Pants</a>
</li>
</ul>
</li>
</ul>
// yields [ul.menu, li]
cy.get('.active').parentsUntil('.nav')

Rules

Requirements

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

Assertions

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

Timeouts

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

Command Log

Find all of the active element's ancestors until .nav

cy.get('.active').parentsUntil('.nav')

The commands above will display in the Command Log as:

Command Log parentsUntil

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

Console Log parentsUntil

See also