Skip to content

Latest commit

 

History

History
142 lines (98 loc) · 2.83 KB

File metadata and controls

142 lines (98 loc) · 2.83 KB
title
next

Get the immediately following sibling of each DOM element within a set of DOM elements.

{% note info %} The querying behavior of this command matches exactly how {% url .next() http://api.jquery.com/next %} works in jQuery. {% endnote %}

Syntax

.next()
.next(selector)
.next(options)
.next(selector, options)

Usage

{% fa fa-check-circle green %} Correct Usage

cy.get('nav a:first').next() // Yield next link in nav

{% fa fa-exclamation-triangle red %} Incorrect 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 Description
log true {% usage_options log %}
timeout {% url defaultCommandTimeout configuration#Timeouts %} {% usage_options timeout .next %}

Yields {% helper_icon yields %}

{% yields changes_dom_subject_or_subjects .next %}

Examples

No Args

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()

Testing a datalist

<input list="fruit" />
<datalist id="fruit">
  <option>Apple</option>
  <option>Banana</option>
  <option>Cantaloupe</option>
</datalist>
cy.get('#fruit option')
  .first().should('have.text', 'Apple')
  .next().should('have.text', 'Banana')
  .next().should('have.text', 'Cantaloupe')

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')

Rules

Requirements {% helper_icon requirements %}

{% requirements dom .next %}

Assertions {% helper_icon assertions %}

{% assertions existence .next %}

Timeouts {% helper_icon timeout %}

{% timeouts existence .next %}

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:

{% imgTag /img/api/next/find-next-element-when-testing-dom.png "Command Log next" %}

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

{% imgTag /img/api/next/elements-next-command-applied-to.png "Console Log next" %}

See also

  • {% url .nextAll() nextall %}
  • {% url .nextUntil() nextuntil %}
  • {% url .prev() prev %}