Cypress拦截,检测API被调用了多少次
Cypress intercept, detect how many times API was called
我正在尝试检测 API 端点在使用 Cypress 进行 运行ning 测试时被调用的次数,我正在使用 cy.intercept() 删除端点。我的代码是这样的:
cy.intercept("POST", "api/cancel/**", {
statusCode: 200,
}).as("cancel_contribution");
cy.intercept("PATCH", "/api/case/**", {
statusCode: 200,
body: {"message": "success"}
}).as("create_case_in_salesforce");
cy.visit("/");
cy.findByText("Manage recurring contribution").click();
cy.get('[data-cy="Cancel recurring contribution"]').click();
cy.findByText("Confirm cancellation").click();
cy.wait("@create_case_in_salesforce");
cy.wait("@cancel_contribution");
cy.get('[data-cy="cancellation_message"]');
expect('@create_case_in_salesforce').to.have.been.calledOnce;
expect('@cancel_contribution').to.have.been.calledOnce;
我试图确保这些端点在测试期间只被调用一次 运行,但最后两行无效,我该如何实现?
您可以为此使用 Cypress 的 @alias.all
功能。
cy.wait("@create_case_in_salesforce");
cy.wait("@cancel_contribution");
cy.get("@create_case_in_salesforce.all").should('have.length', 1);
cy.get("@cancel_contribution.all").should('have.length', 1);
您的代码混合了异步 (cy.
) 和同步 (expect
) 代码。您可以简单地将 expect
包装在 .then()
命令中。
cy.intercept("POST", "api/cancel/**", {
statusCode: 200,
}).as("cancel_contribution");
cy.intercept("PATCH", "/api/case/**", {
statusCode: 200,
body: {"message": "success"}
}).as("create_case_in_salesforce");
cy.visit("/");
cy.findByText("Manage recurring contribution").click();
cy.get('[data-cy="Cancel recurring contribution"]').click();
cy.findByText("Confirm cancellation").click();
// add .then() to check was called once
cy.wait("@create_case_in_salesforce").then(req => {
expect('@create_case_in_salesforce').to.have.been.calledOnce;
})
// add .then() to check was called once
cy.wait("@cancel_contribution").then(req => {
expect('@cancel_contribution').to.have.been.calledOnce;
})
cy.get('[data-cy="cancellation_message"]');
我正在尝试检测 API 端点在使用 Cypress 进行 运行ning 测试时被调用的次数,我正在使用 cy.intercept() 删除端点。我的代码是这样的:
cy.intercept("POST", "api/cancel/**", {
statusCode: 200,
}).as("cancel_contribution");
cy.intercept("PATCH", "/api/case/**", {
statusCode: 200,
body: {"message": "success"}
}).as("create_case_in_salesforce");
cy.visit("/");
cy.findByText("Manage recurring contribution").click();
cy.get('[data-cy="Cancel recurring contribution"]').click();
cy.findByText("Confirm cancellation").click();
cy.wait("@create_case_in_salesforce");
cy.wait("@cancel_contribution");
cy.get('[data-cy="cancellation_message"]');
expect('@create_case_in_salesforce').to.have.been.calledOnce;
expect('@cancel_contribution').to.have.been.calledOnce;
我试图确保这些端点在测试期间只被调用一次 运行,但最后两行无效,我该如何实现?
您可以为此使用 Cypress 的 @alias.all
功能。
cy.wait("@create_case_in_salesforce");
cy.wait("@cancel_contribution");
cy.get("@create_case_in_salesforce.all").should('have.length', 1);
cy.get("@cancel_contribution.all").should('have.length', 1);
您的代码混合了异步 (cy.
) 和同步 (expect
) 代码。您可以简单地将 expect
包装在 .then()
命令中。
cy.intercept("POST", "api/cancel/**", {
statusCode: 200,
}).as("cancel_contribution");
cy.intercept("PATCH", "/api/case/**", {
statusCode: 200,
body: {"message": "success"}
}).as("create_case_in_salesforce");
cy.visit("/");
cy.findByText("Manage recurring contribution").click();
cy.get('[data-cy="Cancel recurring contribution"]').click();
cy.findByText("Confirm cancellation").click();
// add .then() to check was called once
cy.wait("@create_case_in_salesforce").then(req => {
expect('@create_case_in_salesforce').to.have.been.calledOnce;
})
// add .then() to check was called once
cy.wait("@cancel_contribution").then(req => {
expect('@cancel_contribution').to.have.been.calledOnce;
})
cy.get('[data-cy="cancellation_message"]');