我想在外部范围内使用 .then()
中设置的 var 值
我试过了
let numOfDropdownFields = 0;
cy.get("dropdown")
.find("button")
.its("length")
.as("len")
.then(($len) => {
cy.log("No. of buttons:" + $len);
numOfDropdownFields = $len;
cy.get("@len").should("eq", numOfDropdownFields);
cy.log(numOfDropdownFields);
});
cy.log(numOfDropdownFields);
第一个日志打印: 元素数量:14
第二个日志:14
第三个日志:0
如何在 .then()
回调之外看到值 14?
最佳答案
您要实现的目标被认为是 Cypress 的反模式。通常,您应该在 .then()
中使用该变量做任何您想做的事情。
确保使用如下语法:.then() => {all your code in here}
。
查看此处以获取引用:https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Aliases
您也可以像使用 .as('len')
一样使用别名。
关于javascript - 如何使用.then() 之外的.then() 内设置的var 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66471070/