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

Aut*_*Dev 6 javascript cypress

我是新手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));
        });

    });
});
Run Code Online (Sandbox Code Playgroud)

测试失败,日志为:

CypressError:重试超时:cy.wait() 等待第一个路由请求 5000 毫秒超时:“route_login”。从未发生过任何请求。

有人可以帮忙吗?

Dzm*_*nka 3

尝试这个 :

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));
    });

});

});
Run Code Online (Sandbox Code Playgroud)