【问题标题】:How to iterate of web elements in cypress?如何在柏树中迭代网页元素?
【发布时间】:2020-07-23 16:10:18
【问题描述】:

我正在学习 cypress,我有这样一个测试用例:我在网站上有 12 个按钮,我想检查每个按钮是否都被禁用。所以我就这样做了:

按钮:<button id="add-product-5f188b5e68f9e" class="btn btn-sm" role="button" data-add-to-basket="" data-product-price="15.54" data-product-name="Okulary" disabled="">Dodaj</button>

这是我的代码:

it.only('T.C.1. - 添加按钮 - 如果所有按钮都被禁用,则在没有添加任何内容时添加按钮验证', () => {

    cy.visit('https://buggy-testingcup.pgs-soft.com/task_1')
    cy.get('.caption').find('[role="button"]').should('have.length', '12').then( listOfAdds => {
        cy.wrap(listOfAdds)
            .first()
            .should('have.attr', 'disabled')
        cy.wrap(listOfAdds)
            .eq(1)
            .should('have.attr', 'disabled')
        cy.wrap(listOfAdds)
            .eq(2)
            .should('have.attr', 'disabled')
        cy.wrap(listOfAdds)
            .eq(3)
            .should('have.attr', 'disabled')
        cy.wrap(listOfAdds)
            .eq(4)
            .should('have.attr', 'disabled')
        cy.wrap(listOfAdds)
            .eq(5)
            .should('have.attr', 'disabled')
        cy.wrap(listOfAdds)
            .eq(6)
            .should('have.attr', 'disabled')
        cy.wrap(listOfAdds)
            .eq(7)
            .should('have.attr', 'disabled')
        cy.wrap(listOfAdds)
            .eq(8)
            .should('have.attr', 'disabled')
        cy.wrap(listOfAdds)
            .eq(9)
            .should('have.attr', 'disabled')
        cy.wrap(listOfAdds)
            .eq(10)
            .should('have.attr', 'disabled')
        cy.wrap(listOfAdds)
            .eq(11)
            .should('have.attr', 'disabled')
    })

})

我想知道是否可以通过使用某种迭代来做同样的事情。你有什么想法吗?

【问题讨论】:

    标签: cypress


    【解决方案1】:

    有更简单的解决方案 - 使用 each()。首先确保有12个,然后使用each()确保每个都有disabled属性。

    cy.get('.caption').find('[role="button"]').should('have.length', '12')
    cy.get('.caption').find('[role="button"]').each((button) => {
        cy.wrap(button).should('have.attr', 'disabled')
    })
    

    【讨论】:

      猜你喜欢
      • 2021-01-27
      • 2021-12-29
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多