提问人:Sorbon I. 提问时间:2/17/2023 更新时间:2/17/2023 访问量:177
Cypress - 将输入值存储为别名,并比较以前和当前值
Cypress - Storing input values as alias and compare previous and current values
问:
我想通过别名存储所有输入的值,然后在更改数据后获取当前值并将以前和当前的别名相互比较。
我的行动: 首先,获取所有输入值
for (let i = 0; i < 5; i++) {
cy.get('.input').eq(i).invoke('val').then(val => cy.log(val)).as(`previous${i}`);
}
其次,通过在下拉列表中选择实例来更改数据
cy.get('.select').select(1);
第三,获取当前所有输入值
for (let i = 0; i < 5; i++) {
cy.get('.input').eq(i).invoke('val').then(val => cy.log(val )).as(`current${i}`);
}
然后我创建了一个比较方法。
for (let i = 0; i < 5; i++) {
instanceConf.verifyingInputChanging(`@previous${i}`, `@current${i}`);
}
方法本身:
verifyingInputChanging(prevAlias, currenAlias){
cy.get(prevAlias).then((prev_content) => {
cy.get(currenAlias).then((cur_content) => {
expect(cur_content).to.not.eq(prev_content)
})
})
}
不幸的是,比较输入值不起作用。 如何处理?
答:
-1赞
agoff
2/17/2023
#1
这个问题来自您附加的语句,您正在其中获取值。如果删除这些函数,则测试应按预期运行。.then()
cy.log
为什么?当使用没有显式返回的回调速记时,您最终会返回在 中调用的函数,而不是变量。因此,别名的“value”为 null,因为这是 的产生值。.then()
val
cy.log(val)
如果需要该值,则必须返回整个语句,以及生成该值的单独 then。否则,我建议完全删除。cy.log
.then()
// removing .then()
for (let i = 0; i < 5; i++) {
cy.wrap(i)
.as(`prev${i}`);
}
// returning the `.then()`'d value
for (let i = 0; i < 5; i++) {
cy.wrap(i)
.then((val) => {
return cy.log(val).then(() => {
return val;
});
})
.as(`previous${i}`);
}
评论
1赞
Sorbon I.
2/17/2023
非常感谢您的回复。我刚刚删除了它,它可以工作)).then()
评论