【问题标题】:Cypress - How do I wait to select dorpdown to load all options?赛普拉斯 - 我如何等待选择下拉列表以加载所有选项?
【发布时间】:2020-12-01 10:00:41
【问题描述】:
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 断言将失败,因为并非所有选项都已加载。

  • expected '<select#severities.form-control>' to have property 'childElementCount' of 7, but got 1

有没有办法用柏树来做呢?如果没有,那么使用 jQuery?

【问题讨论】:

  • var 名称不合适,请更改它。另外请添加信息如何触发下拉列表以便我们为您提供帮助,您的问题没有提供足够的信息

标签: automation cypress


【解决方案1】:

要等待选项加载,请在.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

超时
.should() 将继续重试其指定的断言,直到超时。

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')
})

【讨论】:

  • 我完全同意你的观点,只是澄清一下,.then() 只尝试一次,.should() 尝试断言直到它们通过或直到超时
  • 干杯,这就是我想说的,但可能还不够清楚。
猜你喜欢
  • 1970-01-01
  • 2022-10-02
  • 1970-01-01
  • 2020-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-13
相关资源
最近更新 更多