Files
cypress/docs/source/api/commands/closest.md
T
2017-05-19 11:51:03 -04:00

1.9 KiB

title: closest comments: true

Get the first DOM element that matches the selector whether it be itself or one of it's ancestors.

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

cy.closest( selector )

For each DOM element in the set, get the first DOM element that matches the selector by testing the DOM element itself and traversing up through its ancestors in the DOM tree.

Options

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

cy.closest( selector, options )

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

Selector Usage

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:

screen shot 2015-11-27 at 2 07 28 pm

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

screen shot 2015-11-27 at 2 07 46 pm

Related