【问题标题】:Is it possible to use two objects: Force and Multiple in Cypress Test?是否可以在 Cypress 测试中使用两个对象:Force 和 Multiple?
【发布时间】:2019-09-08 07:59:30
【问题描述】:

我正在使用cypress test来检查页面中的所有按钮是否可以点击。

我用过这行代码:

cy.get('button').click({ force: true }).should('have.attr', 'href')

并给出错误

CypressError: cy.click() 只能在单个元素上调用。您的 主题包含5个元素。如果你想传递 { multiple: true } 连续点击每个元素。

之后更改代码:

cy.get('button').click({ multiple: true }).should('have.attr', 'href')

又遇到一个错误

CypressError: Timed out retrying: cy.click() failed 因为这个 元素不可见:

...

这个元素 '' 不可见,因为它具有 CSS 属性:'display: none'

修复此问题,或使用 {force: true} 禁用错误检查。

https://on.cypress.io/element-cannot-be-interacted-with

有没有办法同时使用这两个对象来解决问题?

【问题讨论】:

    标签: automation automated-tests cypress


    【解决方案1】:

    这应该可以工作(我没有在两者都需要时对其进行测试的情况,但它不会导致错误):

    cy.get('button')
      .click({ multiple: true, force: true })
      .should('have.attr', 'href')
    

    【讨论】:

    • 另一个测试也有同样的问题,使用上面的代码,终于成功了。谢谢
    • 只是一个建议,不要使用多个true,您可以为您的按钮提供唯一的id或class或testid属性,然后通过id定位该元素。这样,您将永远不会收到此错误。
    【解决方案2】:

    这是一种通用的方法-

    cy.get('button').each(($btn) => {
            if ($btn.hasClass('disabled')) {
                // logic to deal with disabled button
            }
            else {
                // click button or do whatever
                cy.wrap($btn).should('have.attr', 'href').click();
            }
        })
    

    each 将帮助您遍历每个按钮,无论计数如何。这使您不必担心强制单击元素(在本例中为按钮)。

    【讨论】:

      猜你喜欢
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-20
      • 1970-01-01
      • 2017-07-26
      • 2021-11-05
      • 1970-01-01
      相关资源
      最近更新 更多