我使用赛普拉斯(v 4.12.1)为我的React App编写了一些集成测试。我在AWS上有一张CI / CD,需要将它们集成。涉及的产品是GitHub(源存储库),AWS CodePipeline,AWS CodeBuild。
如果必须测试一页的外观,则赛普拉斯测试成功。如果测试涉及一个API调用,而我必须使用cy.wait()
函数,那么我得到的错误是:
CypressError: `cy.wait()` timed out waiting `5000ms` for the 1st request to the route: `agreements`. No request ever occurred.
我检查了服务器日志,看到收到了API请求,因此问题是赛普拉斯无法理解是否进行了API调用。然后我在GitHub https://github.com/cypress-io/cypress/issues/3427上发现了这个问题,但是我发现的建议是我正在使用的建议:
- 确保在cy.visit()之前或在执行将导致XHR请求失效的操作之前,先定义cy.server()和cy.route()。
- 请确保根据需要在cy.route()上定义方法。 cy.wait()默认情况下仅自动侦听GET请求。
在这里您可以找到为我的集成测试编写的代码:
context("Test Page", () => {
before(() => {
cy.server();
cy.route({
method: "POST",
url: `https://myserver.com/${Cypress.env("ENV")}/login`,
status: 201,
}).as("login");
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
cy.visit("http://localtest.myserver.com:3000/test_page");
// Dismiss cookie message
cy.get("[data-cy=dialog-actions]").find("button").click();
cy.log('Cookie message dismissed');
});
it("landed on the agreements page", () => {
cy.bypassLogin().then((xhr) => {
LOGIN_ID = xhr.body["loginId"];
cy.route({
method: "GET",
url: `https://myserver.com/${Cypress.env("ENV")}/login/${LOGIN_ID}/agreements`,
status: 200,
}).as("agreements");
});
cy.wait("@agreements").then((xhr) => {
NUM_AGREEMENTS = xhr.response.body.length;
let requiredAgreements = [];
let notRequiredAgreements = [];
xhr.response.body.forEach((elem, idx) => {
elem.isRequired
? requiredAgreements.push(idx)
: notRequiredAgreements.push(idx);
});
IDX_REQUIRED_AGREEMENTS = requiredAgreements;
IDX_NOT_REQUIRED_AGREEMENTS = notRequiredAgreements;
expect(xhr.status).to.eq(200);
});
});
有人可以帮助我解决这个问题吗?