代码之家  ›  专栏  ›  技术社区  ›  dushkin

如何记录元素长度列表?

  •  1
  • dushkin  · 技术社区  · 2 年前

    这个代码没有为我记录任何内容。为什么?

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

    实际日志:

    loglength, {specwindow: <window>, chainerid: ch-https://192.168.51.12:8080-14}
    
    loglength = ${length}.
    
    loglength = ${items.length}.
    
    1 回复  |  直到 2 年前
        1
  •  0
  •   Ale_Bianco    2 年前

    在Cypress中, cy.get('[role="cell"]') 返回一个jQuery对象,该对象有自己的一组方法。当您使用 .its('length') ,它不会立即返回长度;相反,它创建了一个可链接的命令。要获得实际价值,您需要 .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
    });