赛普拉斯 - 如何等待 select 下拉列表加载所有选项?

Cypress - How do I wait to select dorpdown to load all options?

cy.get("#severities").then(($optionsArray) => {
           expect($optionsArray.get(0)).to.have.property('childElementCount', 6) 
       let optionVal = new Array()
       optionVal = $optionsArray.children()
       var randomChoice = optionVal[Math.floor(Math.random() * optionVal.length) + 1]
       addAppointment.selectSeverity(randomChoice.text)
       })

expect 断言将失败,因为未加载所有选项。

有什么办法可以用柏树做到这一点吗?如果不是,则使用 jQuery?

要等待加载选项,请在 .get().then() 之间插入一个 .should()。或者甚至将 .then() 更改为 .should() 也可以解决问题。

.should() 的关键是它会在条件成功之前退出前面的命令,因此非常适合等待异步数据。

所以

cy.get("#severities")
  .should(($optionsArray) => {
     expect($optionsArray.get(0)).to.have.property('childElementCount', 6)
   })

会一直重新获取#severities并刷新$optionsArray,直到expect()成功,或者超时。

我会把等待的部分和处理的部分分开,像这样

cy.get("#severities")
  .should($optionsArray => {
     expect($optionsArray.get(0)).to.have.property('childElementCount', 6)
   })
  .then($optionsArray => {
     let optionVal = new Array()
     optionVal = $optionsArray.children()
     var randomChoice = optionVal[Math.floor(Math.random() * optionVal.length) + 1]
     addAppointment.selectSeverity(randomChoice.text)
  });

对于reference

Timeouts
.should() will continue to retry its specified assertions until it times out.

cy.get('input', { timeout: 10000 }).should('have.value', '10')
// timeout here will be passed down to the '.should()'
// and it will retry for up to 10 secs


cy.get('input', { timeout: 10000 }).should(($input) => {
  // timeout here will be passed down to the '.should()'
  // unless an assertion throws earlier,
  // ALL of the assertions will retry for up to 10 secs
  expect($input).to.not.be('disabled')
  expect($input).to.not.have.class('error')
  expect($input).to.have.value('US')
})