【问题标题】:Click in Button until it disappears in Cypress单击 Button 直到它在 Cypress 中消失
【发布时间】:2021-10-18 18:18:32
【问题描述】:

读了很多书,但我尝试过的东西都没有奏效..

我有一个用例是: 我需要单击一个按钮(例如 10 次)以在一个页面中加载更多项目,但如果我们已经加载了所有项目,则该按钮会消失。加载的次数总是不一样的,所以在某些情况下我可以点击 10 次,其他时候我只能点击 5 次。

我们如何使用 Cypress 做到这一点?

请注意:加载按钮在加载新项目时仍然存在,但会进入禁用状态,然后再次启用或消失。

【问题讨论】:

  • 请添加您尝试过的代码。

标签: cypress


【解决方案1】:

你看到递归函数模式了吗?

类似

function clickUntilGone(selector, attempt = 0) {
  if (attempt === 11) {
    throw 'It didnt work'
  }

  cy.get(selector).click()
    .then($button => {
      const exists = Cypress.$(selector).length > 0
      if (exists) {
        // wait for disabled/not disabled cycle
        cy.get(selector:disabled).then(() => { 
          cy.get(selector:not(:disabled)).then(() => {
            clickUntilGone(selector, ++attempt))    
          })
        })    
      })
  })
}
clickUntilGone('my-button`)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    相关资源
    最近更新 更多