我有一个自定义的 cypress 命令,它执行一些异步任务,并且根据命令的结果,我想执行一些断言
但问题是调用命令后的测试要么在命令完成之前运行,要么有错误的值。
下面是我的代码
命令
Cypress.Commands.add("testCommand", () => {
cy.request("http://localhost:3000/testApi").then(({ body }) => {
cy.request(`http://localhost:3000/testApi${body.query}`).then(() => {
console.log("success");
});
});
});
测试
describe("Summary Page", () => {
it("my demo test", () => {
console.log("before command runs");
cy.testCommand();
console.log("after command runs");
});
});
实际结果
before command runs
after command runs
success
所需结果
before command runs
success
after command runs
如您所见,输出after command runs
将在命令完成之前运行
在继续测试之前有什么方法可以等待命令完成