2.5 KiB
title, comments, description
| title | comments | description |
|---|---|---|
| closest | true |
Get the first DOM element that matches the selector (whether it be itself or one of it's ancestors).
Syntax
.closest(selector)
.closest(selector, options)
Usage
.closest() requires being chained off another cy command that yields a DOM element.
{% fa fa-check-circle green %} Valid Usage
cy.get('td').closest('.filled') // Yield closest el with class '.filled'
{% fa fa-exclamation-triangle red %} Invalid Usage
cy.closest('.active') // Errors, cannot be chained off 'cy'
cy.url().closest() // Errors, 'url' 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 .closest().
| Option | Default | Notes |
|---|---|---|
log |
true |
whether to display command in command log |
timeout |
defaultCommandTimeout |
Total time to retry getting the element |
Yields
.closest() yields the new DOM elements found by the command.
Timeout
.closest() will continue to look for the closest element for the duration of the defaultCommandTimeout
Examples
Closest
Find the closest element of the current subject with the class nav
cy.get('li.active').closest('.nav')
Command Log
Find the closest element of the current subject with the class nav
cy.get('li.active').closest('.nav')
The commands above will display in the command log as:
When clicking on the closest command within the command log, the console outputs the following: