Variables Cypress |不能在每个循环内更改变量
好的,我有这个代码:Variables Cypress |不能在每个循环内更改变量,variables,integer,each,cypress,var,Variables,Integer,Each,Cypress,Var,好的,我有这个代码: Cypress.Commands.add ('MethodName', (argument) => { var Fails = 0 cy.get('anything').each(Id => { if (blablabla) { Fails += 1 cy.log("Inside the each: " + Fails) //prints 1 } })
Cypress.Commands.add ('MethodName', (argument) => {
var Fails = 0
cy.get('anything').each(Id => {
if (blablabla) {
Fails += 1
cy.log("Inside the each: " + Fails) //prints 1
}
})
cy.log("Outside the each: " + Fails) //prints 0
});
我想测试每个项目,如果条件错误,我想在变量“Fails”中添加1
最后,如果Fails为0,则没有错误,我希望它记录消息“no Fails”。问题是,即使变量在内部变为1,但在外部变为0
这对我来说太令人沮丧了,因为我习惯于用C语言编写C代码,而在C语言中,这是可行的,因为变量的声明不在each的范围内
你们有什么建议吗?JavaScript是异步运行的,这意味着代码不是按顺序运行的。因此,在您的案例中发生的情况是,each:首先执行,然后each:内部执行。要确保每个外部在每个内部之后运行,必须使用
then()
我看到了!非常感谢你!
Cypress.Commands.add('MethodName', (argument) => {
var Fails = 0
cy.get('anything').each(Id => {
if (blablabla) {
Fails += 1
cy.log("Inside the each: " + Fails)
}
}).then(() => {
cy.log("Outside the each: " + Fails)
})
})