title |
---|
parents |
Get the parent DOM elements of a set of DOM elements.
{% note info %}
The querying behavior of this command matches exactly how {% url .parents()
http://api.jquery.com/parents %} works in jQuery.
{% endnote %}
.parents()
.parents(selector)
.parents(options)
.parents(selector, options)
{% fa fa-check-circle green %} Correct Usage
cy.get('aside').parents() // Yield parents of aside
{% fa fa-exclamation-triangle red %} Incorrect Usage
cy.parents() // Errors, cannot be chained off 'cy'
cy.go('back').parents() // Errors, 'go' does not yield DOM element
{% 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 .parents()
.
Option | Default | Description |
---|---|---|
log |
true |
{% usage_options log %} |
timeout |
{% url defaultCommandTimeout configuration#Timeouts %} |
{% usage_options timeout .parents %} |
{% yields changes_dom_subject_or_subjects .parents %}
cy.get('li.active').parents()
cy.get('li.active').parents('.nav')
{% requirements dom .parents %}
{% assertions existence .parents %}
{% timeouts existence .parents %}
Get the parents of the active li
cy.get('li.active').parents()
{% imgTag /img/api/parents/get-all-parents-of-a-dom-element.png "Command Log parents" %}
When clicking on the parents
command within the command log, the console outputs the following:
{% imgTag /img/api/parents/parents-elements-displayed-in-devtools-console.png "Console Log parents" %}
- {% url
.children()
children %} - {% url
.parent()
parent %} - {% url
.parentsUntil()
parentsuntil %}