【问题标题】:How can I receive count of elements?我怎样才能收到元素的数量?
【发布时间】:2023-01-04 06:45:26
【问题描述】:

测试.js:

cy.get('.ant-input-number-input')
  .wait(2000)
  .then(($input_field) => {
    const count = $input_field.find('.ant-input-number-input').length;
    cy.log(count)
  })

cy.log:

log 0

我需要计算元素数量。但我收到了“0”。我怎样才能收到元素的数量?

【问题讨论】:

    标签: cypress


    【解决方案1】:

    假设您只查找元素.ant-input-number-input 的长度,您可以这样做:

    1. 获取长度
      cy.get('.ant-input-number-input')
        .should('be.visible')
        .its('length')
        .then((len) => {
          cy.log(len) //prints length
        })
      
      1. 如果你想添加断言,你可以这样做:
      //Length equal to 2
      cy.get('.ant-input-number-input')
        .should('be.visible')
        .its('length')
        .should('eq', 2)
      
      //Length greater than 2
      cy.get('.ant-input-number-input')
        .should('be.visible')
        .its('length')
        .should('be.gt', 2)
      
      //Length greater than or equal to 2
      cy.get('.ant-input-number-input')
        .should('be.visible')
        .its('length')
        .should('be.gte', 2)
      
      //Length less than 2
      cy.get('.ant-input-number-input')
        .should('be.visible')
        .its('length')
        .should('be.lt', 2)
      
      //Length less than or equal to 2
      cy.get('.ant-input-number-input')
        .should('be.visible')
        .its('length')
        .should('be.lte', 2)
      

    【讨论】:

      【解决方案2】:

      您可以使用

      const count = $input_field.find('.ant-input-number-input').its('length')
      

      【讨论】:

        【解决方案3】:

        这不适用于元素数量为零的情况。那样的话会怎样? 我需要检查该元素是否存在,但在某些情况下它会存在,而在其他情况下则不会。对于当元素不存在时,我将转到下一页(分页)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-11-03
          • 2017-09-23
          • 1970-01-01
          • 2016-04-07
          • 1970-01-01
          • 2012-09-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多