2.6 KiB
title, comments, description
| title | comments | description |
|---|---|---|
| next | true |
Get the immediately following sibling of each DOM element within a set of DOM elements.
Syntax
.next()
.next(selector)
.next(options)
.next(selector, options)
Usage
.next() requires being chained off another cy command that yields a DOM element or set of DOM elements.
{% fa fa-check-circle green %} Valid Usage
cy.get('nav a:first').next() // Yield next link in nav
{% fa fa-exclamation-triangle red %} Invalid Usage
cy.next() // Errors, cannot be chained off 'cy'
cy.getCookies().next() // Errors, 'getCookies' 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 .next().
| Option | Default | Notes |
|---|---|---|
log |
true |
Whether to display command in Command Log |
timeout |
defaultCommandTimeout |
Total time to retry getting the element |
Yields
.next() yields the new DOM element(s) found by the command.
Timeout
.next() will continue to look for the next element for the duration of the defaultCommandTimeout.
Examples
Next
Find the element next to .second
<ul>
<li>apples</li>
<li class="second">oranges</li>
<li>bananas</li>
</ul>
// yields <li>bananas</li>
cy.get('.second').next()
Selector
Find the very next sibling 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>
</ul>
// yields <li>pineapples</li>
cy.get('li').next('.selected')
Command Log
Find the element next to the .active li
cy.get('.left-nav').find('li.active').next()
The commands above will display in the command log as:
When clicking on next within the command log, the console outputs the following: