Open In App

Cypress - nextAll() Method

Last Updated : 05 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The nextAll() method in Cypress is used to select all sibling elements that follow a specific element in the DOM. It’s helpful when you need to interact with multiple subsequent elements relative to a particular node. This method is particularly useful in situations where you need to work with a group of elements that all appear after a selected element, such as in lists, forms, or other sequentially structured HTML content.

Usage of Cypress - nextAll() Method

The nextAll() method is most useful when you want to grab all siblings that appear after the current element in the DOM structure. It can be combined with filters or assertions to ensure you are interacting with the correct group of elements.

Syntax

cy.get(selector).nextAll();
  • selector: A CSS selector to target a specific element.
  • nextAll(): Cypress method that retrieves all sibling elements that follow the targeted element.

Arguments of Cypress - nextAll() Method

The nextAll() method can take an optional argument in the form of a selector to filter the subsequent siblings to only include those that match the given selector.

cy.get(selector).nextAll(nextSelector);
  • nextSelector (optional): A CSS selector to filter which following siblings should be returned.

Example 1: Working with List Items

HTML Code

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cypress nextAll() Example - List</title>
</head>
<body>
    <ul id="itemsList">
        <li class="item">Item 1</li>
        <li class="item">Item 2</li>
        <li class="item">Item 3</li>
        <li class="item">Item 4</li>
        <li class="item">Item 5</li>
    </ul>
</body>
</html>

Cypress Test Code

JavaScript
describe('Cypress nextAll() Method Example - List Items', () => {
    it('should select all the next siblings after the first list item', () => {
        cy.visit('http://localhost:3000'); // Adjust to your local environment
        cy.get('.item').first().nextAll().should('have.length', 4);
    });
});

Output

The test will visit the page, select the first list item (Item 1), and then use the nextAll() method to select all subsequent list items (Item 2, Item 3, Item 4, and Item 5). The test asserts that the number of following siblings is 4.

output
Output

Explanation

  • HTML Structure: The HTML includes an unordered list (<ul>) with five list items, each having the class item.
  • Test Strategy: In the test, we select the first list item using .first(), then apply .nextAll() to select all the following siblings. The test checks that the length of the selected elements is 4, meaning four elements follow the first one.

Example 2: Filtering the Following Siblings by Class

HTML Code

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cypress nextAll() Example - Filtering</title>
</head>
<body>
    <div class="block important">Block 1</div>
    <div class="block">Block 2</div>
    <div class="block important">Block 3</div>
    <div class="block">Block 4</div>
    <div class="block important">Block 5</div>
</body>
</html>

Cypress Test Code

JavaScript
describe('Cypress nextAll() Method Example with Filtering', () => {
    it('should select all subsequent siblings with class "important"', () => {
        cy.visit('http://localhost:3000'); // Adjust to your local environment
        cy.get('.block').first().nextAll('.important').should('have.length', 2);
    });
});

Output

The test visits the page, selects the first div with the class block, and uses the nextAll() method to select all subsequent siblings that also have the class important. The test asserts that two following elements match the selector.

output
Output

Explanation

  • HTML Structure: The HTML contains multiple div elements with the class block. Some of these divs also have the class important.
  • Test Strategy: The test first selects the first div.block and uses .nextAll('.important') to select all subsequent siblings with the class important. The test checks that exactly two such elements exist.

Conclusion

The nextAll() method is a powerful tool in Cypress that allows you to select all siblings that follow a particular element in the DOM. It can be useful when you need to interact with multiple subsequent elements or when you want to filter a group of following elements based on a specific condition.


Next Article
Article Tags :

Similar Reads