首页 > 解决方案 > 在赛普拉斯,我的餐桌数来不及评估

问题描述

我正在传递一个 var "rows" 以分配给我的赛普拉斯表长度函数(表行数),但它是在我需要在以后的评估中使用它之后设置的(所以总是失败,因为它仍然是 0 )。毫无疑问,这是由于我对 Javascript 的了解很差,但是我怎样才能确保它在使用之前被设置好呢?

代码:

    // get a count of all the current table rows
    let rows = 0;
    cy.get('table').find('tr').its('length')
        .then((l) => {
          console.log(l + ' rows detected')
          rows = l
          console.log(' rows set to ', rows)
          // NOTE this is set to 18 but only
          // after I have done the evaluation below
        })
    cy.get('table').find('tr').its('length')
      .should('be.lt', rows)
     //this is always still 0 at this point

标签: javascriptasynchronouscypress

解决方案


把它放在链中就可以了:

// get a count of all the current table rows
    let rows = 0;
    cy.get('table').find('tr').its('length')
        .then((l) => {
          console.log(l + ' rows detected')
          rows = l
          console.log(' rows set to ', rows)
          // NOTE this is set to 18 but only
          // after I have done the evaluation below

       //this should now work
       cy.get('table').find('tr').its('length')
       .should('be.lt', rows) 

    })

推荐阅读