【发布时间】:2019-01-07 17:31:49
【问题描述】:
在 cypress 测试中,我需要通过调用外部 API 来验证操作。 API 调用将始终返回结果(来自先前的运行),因此我不能简单地调用一次并验证结果。我需要重试几次,直到找到与当前运行匹配且总体超时/失败的匹配项。获得当前结果所需的时间差异很大;在这个电话之前,我真的不能等待很长时间。
见下面 sn-p 中的 cmets;一旦我在循环中尝试一个请求,它就永远不会被调用。我使用cy.wait 得到了相同的结果。我也不能将实际请求包装在另一个返回 Cypress.Promise 或类似的函数中,这只会将问题推到一个堆栈帧上。
Cypress.Commands.add("verifyExternalAction", (someComparisonValue) => {
const options = {
"url": some_url,
"auth": { "bearer": some_apikey },
"headers": { "Accept": "application/json" }
};
//// This works fine; we hit the assertion inside then.
cy.request(options).then((resp) => {
assert.isTrue(resp.something > someComparisonValue);
});
//// We never enter then.
let retry = 0;
let foundMatch = false;
while ((retry < 1) && (!foundMatch)) {
cy.wait(10000);
retry++;
cy.request(options).then((resp) => {
if (resp.something > someComparisonValue) {
foundMatch = true;
}
});
}
assert.isTrue(foundMatch);
});
【问题讨论】:
标签: cypress