cy.wait()等待路由的第一个请求时超时5000ms



我是Cypress的新手,正在检查HTML页面。我需要测试登录身份验证并记录XHR主体。所以,我写了一个测试像:

describe('Login test', function () {
it("Test description", () => {
cy.server();
cy.visit("login");
cy.route({
method: "POST",
url: '/login'
}).as("login");
cy.get('#username')
.type(Cypress.env('Account'));
cy.get('#password')
.type(Cypress.env('Password'));
cy.get('#login')
.click();
cy.wait("@login").then(xhr => {
cy.log(JSON.stringity(xhr.response.body));
});
});
});

测试失败,日志为:

CypressError:重试超时:cy.wait((等待路由"route_login"的第一个请求超时5000ms。从未发生任何请求。

有人能帮忙吗?

试试这个:

describe('Login test', function () {
it("Test description", () => {
cy.server();

cy.route({
method: "POST",
url: '/login'
}).as("login");
cy.wait("@login", {timeout: 15000});
cy.visit("login");
cy.get('#username')
.type(Cypress.env('Account'));
cy.get('#password')
.type(Cypress.env('Password'));
cy.get('#login')
.click();
cy.get("@login").then(xhr => {
cy.log(JSON.stringity(xhr.response.body));
});
});
});

cy.routecy.server都已弃用,不应使用。cy.interceptcy.wait的组合应该足够

最新更新