【发布时间】:2021-09-09 06:40:24
【问题描述】:
下面是我正在使用的代码。
我正在使用 Cypress + Cucumber + Typescript。
场景:我需要使用 for 循环获取唯一值列表。然后我将此值传递给 API 以验证某些条件,如果满足条件,我想退出循环。
为了退出循环,我在某处阅读了一个解决方案,如果我在 if 条件中使用“return false”作为第一行,那么循环将退出,这似乎工作正常。
这里的问题是,当我尝试从 for-if 循环内部为实例变量设置一个标志时,if 条件读取的值(用于退出循环)没有选择实例变量的更新值。并且循环继续运行。
下面是sn-p的代码:
class test {
static isVinavailable: boolean = false;
static setEligibleVehicleVinTest() {
cy.xpath(eligibleForSaleVehicleVin).then((esv) => {
const listingCount = Cypress.$(esv).length;
for (let i = 0; i < listingCount; i++) {
let text123 = esv.eq(i).text();
genericAction.getAuthenticationKey();
cy.fixture("authResp.json")
.then((authResp) => {
cy.request({
method: "GET",
url: vehicleCheckEligibility + text123,
headers: {
Authorization: authResp.access_token,
},
});
})
.then((response: any) => {
cy.wait(5000);
let responseDataelig = response.body;
if (
(responseDataelig.val1 =
"Y" &&
responseDataelig.val2 === "N" &&
responseDataelig.val3 === "N")
) {
this.isVinavailable = true;
}
});
if (this.isVinavailable) {
return false;
}
}
});
}
}
【问题讨论】:
标签: cypress