Mic*_*ael 4 cypress cypress-each
接下来是简单的逻辑:点击“li”元素后,发送请求,并根据“contacts”的响应值,选择它是否大于0,一旦找到这样的元素,我需要打破每个循环。但目前,尽管我设置了值,但它应该会在下一次迭代时中断每个循环(返回 false)。count[] 已恢复为旧值,有什么问题吗?
cy.get('div[id=company_select]').then($el => {
const count = []
cy.wrap($el).click().find('li').each((element, index) => {
cy.intercept('GET', '**/company/view-json**').as('getCompanyProps')
cy.log(count.length)
if (count.length > 0) {
return false
} else {
cy.wrap(element).click().wait('@getCompanyProps').then((interception) => {
if (interception.response.body.contacts.length === 0) {
cy.wrap($el).find('button[vs__clear]').click()
} else {
count.push(1)
cy.log(count.length)
}
})
}
})
})
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您不能提前退出return false
,但您可以使用count
来阻止之后的内部执行body.contacts.length > 0
。
cy.intercept('GET', '**/company/view-json**').as('getCompanyProps') // this can be here
cy.get('div[id=company_select]').then($el => {
const count = []
cy.wrap(count).as('count') // make an alias value of the count variable
cy.wrap($el).click().find('li')
.each((element, index) => {
cy.get('@count').then(count => { // retrieve count asynchronously
if (count.length === 0) { // no result yet?
cy.wrap(element).click().wait('@getCompanyProps')
.then((interception) => {
if (interception.response.body.contacts.length === 0) {
cy.wrap($el).find('button[vs__clear]').click()
} else {
count.push(1)
console.log(count.length)
}
})
}
})
})
})
Run Code Online (Sandbox Code Playgroud)
出现此行为的原因是异步命令(例如 .wait('@getCompanyProps')
用于检查提前退出的同步代码)的混合。
如果您使用console.log()
而不是cy.log()
调试值,您将看到提前退出之前的日志全部在之后的日志之前运行count.push(1)
。
归档时间: |
|
查看次数: |
162 次 |
最近记录: |