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-}

loglength = ${length}.

loglength = ${items.length}.

>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

Leave a Reply