我如何计算元素以便稍后使用该值?
how can i count the elements so i can use the value later?
Html:
<tbody class="ant-table-tbody">
<tr class="ant-table-row"> 1 </tr>
<tr class="ant-table-row"> 2 </tr>
...
<tr class="ant-table-row"> n </tr>
</tbody>
JS:
let count = cy.get('.ant-table-tbody').find('.ant-table-row')
if (count >= 0) {
cy.log(`There are ${count} elements`)
} else {
cy.log('There are no elements')
}
当我得到 .ant-table-tbody
个元素时,我需要计算 .ant-table-row
个元素。如果计数超过“0”cy.log 个元素,如果“0”个元素应该 cy.log - 没有元素。
我该怎么做?
您可以使用与去年 7 月的问题相同的方法,使用别名
cy.get('.ant-table-tbody')
.find('.ant-table-row')
.its('length')
.as('rowCount')
cy.get('@rowCount')
.then(count => {
if (count) {
cy.log(`There are ${count} elements`)
...
})
或者直接在find
之后
cy.get('.ant-table-tbody')
.find('.ant-table-row')
.its('length')
.then(count => {
if (count) { // not count >= 0, because 0 means no elements
cy.log(`There are ${count} elements`)
...
})
所有使用 count
的代码必须在 .then()
内
您可以应用大于 0 的断言并添加自定义日志消息,如下所示:
cy.get('.ant-table-tbody')
.find('.ant-table-row')
.its('length')
.should('be.gt', 0, 'The element count is greater than 0')
或者,如果你想断言计数大于等于0,你可以
cy.get('.ant-table-tbody')
.find('.ant-table-row')
.its('length')
.should('be.gte', 0, 'The number of elements is greater and equal to 0')
好的,我想你想知道什么时候有零行以及 > 0,所以
cy.get('.ant-table-tbody')
.then($tbody => {
const count = $tbody.find('.ant-table-row').length // jquery find won't fail
if (count > 0) {
cy.log(`There are ${count} elements`)
} else {
cy.log('There are no elements')
}
})
如果零行不可能,其他答案更简单。
Html:
<tbody class="ant-table-tbody">
<tr class="ant-table-row"> 1 </tr>
<tr class="ant-table-row"> 2 </tr>
...
<tr class="ant-table-row"> n </tr>
</tbody>
JS:
let count = cy.get('.ant-table-tbody').find('.ant-table-row')
if (count >= 0) {
cy.log(`There are ${count} elements`)
} else {
cy.log('There are no elements')
}
当我得到 .ant-table-tbody
个元素时,我需要计算 .ant-table-row
个元素。如果计数超过“0”cy.log 个元素,如果“0”个元素应该 cy.log - 没有元素。
我该怎么做?
您可以使用与去年 7 月的问题相同的方法,使用别名
cy.get('.ant-table-tbody')
.find('.ant-table-row')
.its('length')
.as('rowCount')
cy.get('@rowCount')
.then(count => {
if (count) {
cy.log(`There are ${count} elements`)
...
})
或者直接在find
之后cy.get('.ant-table-tbody')
.find('.ant-table-row')
.its('length')
.then(count => {
if (count) { // not count >= 0, because 0 means no elements
cy.log(`There are ${count} elements`)
...
})
所有使用 count
的代码必须在 .then()
您可以应用大于 0 的断言并添加自定义日志消息,如下所示:
cy.get('.ant-table-tbody')
.find('.ant-table-row')
.its('length')
.should('be.gt', 0, 'The element count is greater than 0')
或者,如果你想断言计数大于等于0,你可以
cy.get('.ant-table-tbody')
.find('.ant-table-row')
.its('length')
.should('be.gte', 0, 'The number of elements is greater and equal to 0')
好的,我想你想知道什么时候有零行以及 > 0,所以
cy.get('.ant-table-tbody')
.then($tbody => {
const count = $tbody.find('.ant-table-row').length // jquery find won't fail
if (count > 0) {
cy.log(`There are ${count} elements`)
} else {
cy.log('There are no elements')
}
})
如果零行不可能,其他答案更简单。