Cypress似乎在cy.visit之后重新启动了测试



我使用柏树进行了以下测试:

// myTest.spec.ts
console.log("the test is starting");
describe("My Test Describe", () => {
const testEmail = makeRandomEmail();
console.log("test email", testEmail);
it("should set up the profile", () => {
// setupProfile just makes some requests and returns a promise
cy.wrap(setupProfile(testEmail), {
timeout: 15000,
});
});
it("should test the thing", () => {
// makeAppUrl just returns a string
cy.visit(makeAppURL());
/* test stuff happens here which relies on the generated testEmail */
});
});

当我在dev-env上运行时,这很好(因为它在443上,所以在url中没有端口(。

然而,我遇到了一个奇怪的场景,当我对本地服务器(在端口3000上(运行测试时,会发生以下情况:

  1. 它在浏览器控制台中记录"the test is starting""test email generatedTestEmail"
  2. 它运行CCD_ 3精细并且该测试通过
  3. 然后,它似乎重新加载了整个测试,并重新记录了(1(中的内容(使用新生成的电子邮件(,但(2(仍然显示为通过
  4. 它试图运行我的it("should test the thing")块,但失败了,因为现在我有了一个新的用户测试电子邮件

当我只切换主机以指向我的dev-env而不是本地时,它运行良好,不会像(3(中所述那样重新加载。

以前有人遇到过这样的事情吗?这可能与我在URL中有端口这一事实有关吗?

此解决方案适用于我:您需要在配置文件中放入真正的baseUrl。请参阅下面的示例。

常见问题未设置baseUrl请确保您没有意外地将baseUrl或其他顶级配置变量放入env块中。以下配置不正确,将不起作用:

//不起作用

{
"env": {
"baseUrl": "http://localhost:3030",
"FOO": "bar"
}
}

解决方案:将baseUrl属性放在env对象外部的顶层。

//的正确方式

{
"baseUrl": "https://.....",
"env": {
"FOO": "bar"
}
}

请参阅此处了解更多详细信息https://docs.cypress.io/guides/references/configuration#Common-问题

问题出在baseUrl配置上。这是针对dev-env的,针对本地运行的脚本没有覆盖此配置,如下所述:https://docs.cypress.io/guides/references/configuration#Command-线路

我会尝试利用mocha的before()功能,在describe()块中的所有测试中维护相同的数据。

// myTest.spec.ts
describe("My Test Describe", () => {
let testEmail;
before(() => {
console.log("the test is starting");
testEmail = makeRandomEmail();
console.log("test email", testEmail);
});
it("should set up the profile", () => {
cy.wrap(setupProfile(testEmail), {
timeout: 15000,
});
});
it("should test the thing", () => {
cy.visit(makeAppURL());
/* test stuff happens here which relies on the generated testEmail */
});
});

最新更新