Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cypress How can I get the same-named element count

in selenium I just creating web element list to learn that, in cypress I use .each() method to iterate in same-named elements but Some times I just need to know the same-named elements number to use this number somewhere else.

How can I do that in cypress?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can store the length of the yielded list of elements via an alias.

cy.get('foo')
  .its('length')
  .as('listLength');
... // later
cy.get('@listLength').should('be.gte', 1);

If you need to access the variable value synchronously, you could store it in a Cypress environment variable, or a regular JS variable.

let lengthVar;
cy.get('foo')
  .its('length')
  .then((length) => {
    // If storing in a Cypress.env() variable
    Cypress.env('listLength', length);
    // If storing in a traditional JS variable
    lengthVar = length;
  });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading