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

How to log a list of elements length?

This code logs nothing for me. Why?

  const items = cy.get('[role="cell"]');
  let length = items.its('length');
  cy.log('length', length)
  cy.log('length = ${length}.');
  cy.log('length = ${items.length}.');

Actual log:

loglength, {specwindow: <window>, chainerid: ch-https://192.168.51.12:8080-14}

loglength = ${length}.

loglength = ${items.length}.

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 :

In Cypress, cy.get('[role="cell"]') returns a jQuery object, which has its own set of methods. When you use .its('length'), it doesn’t return the length immediately; instead, it creates a chainable command. To get the actual value, you need to chain .then().

cy.get('[role="cell"]').then(items => {
  let length = items.length; // Get the length here
  cy.log('length', length); // Log the length
  cy.log(`length = ${length}.`); // Use backticks for template literals
  cy.log(`length = ${items.length}.`); // This should work too
});
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