Search code examples
cypresse2e-testing

Assert any one element in multiple


In Cypress, is it possible to assert that any one of a number of elements matches a specific rule?

For example, something like this:

cy.get('nav').children().any.should('have.value', 'My value');

Solution

  • It's simpler than I thought...

    cy.get('nav').contains('div', 'My value');
    

    This will get the nav element and assert that it contains a div tag with the text My value.

    Thanks to @JoshuaWade for this solution in his answer to How to get data displaying in a table in Cypress.io test?