重试 if 语句,直到在 Cypress 中找到 'Withdrawn' 元素

Retry the if statement until the 'Withdrawn' element is found in Cypress

cy.get('body').then(($body) => { 
            if ($body.text().includes('Withdrawn')) {
                cy.contains('Withdrawn')
                .click({force:true})
            } else if ($body.text().includes('More')) { 
                cy.contains('More')
                .click({force:true})
                cy.wait(10000)
                cy.contains('Withdrawn')
                .click({force:true})
              
        })

您好, 我是柏树的新手,我希望再次重复 if 语句,直到通过单击更多按钮在正文中找到元素 'Withdrawn' 。谁能告诉我执行此操作的最佳方法是什么?

不确定这是否是正确的代码,但想法是递归调用您拥有的代码以进行单次迭代。

const clickUntilWithdrawn = ($parent, attempts = 0) => {
  if (attempts > 10) throw 'Failed to find'

  if ($parent.text().includes('Withdrawn')) {
    cy.contains('Withdrawn').click()
    return
  }
  cy.contains('More').should('be.visible').click()
  cy.wait(10000)    // probably can be shorter wait with visibility check above
  
  clickUntilWithdrawn($parent, ++attempts)
}

cy.get('parent-element-of-withdrawn')
  .then($el => clickUntilWithdrawn($el))