赛普拉斯似乎在 cy.visit 后重新开始测试
Cypress seems to restart the test after cy.visit
我使用 cypress 进行了以下测试:
// 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 */
});
});
当我 运行 针对我的开发环境(它在 url 中没有端口,因为它在 443 上时,这工作正常)。
但是,我 运行 遇到了一个奇怪的场景,当我 运行 对我的本地服务器(在端口 3000 上)进行测试时,会发生以下情况:
- 它在浏览器控制台中记录
"the test is starting"
和 "test email generatedTestEmail"
- 运行
setupProfile
很好,测试通过了。
- 然后,它似乎重新加载整个测试并重新记录 (1) 中的内容(使用新生成的电子邮件),但 (2) 仍显示为通过。
- 它试图 运行 我的
it("should test the thing")
阻止失败,因为现在我有一个新用户测试电子邮件。
当我仅切换主机以指向我的开发环境而不是本地时,它工作正常并且不会按照 (3) 中所述重新加载。
有没有人运行以前遇到过类似的事情?这可能与我在 URL 中有端口有关吗?
我会尝试利用 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 */
});
});
问题出在 baseUrl
配置上。这指向开发环境,运行 针对本地的脚本没有覆盖此配置,如下所述:https://docs.cypress.io/guides/references/configuration#Command-Line
这个解决方案对我有用:
您需要将真实的 baseUrl 放入配置文件中。请参阅下面的示例。
常见问题
未设置 baseUrl
确保您没有意外地将 baseUrl 或另一个 top-level 配置变量放入 env 块中。以下配置不正确,将无法工作:
//不起作用
{
"env": {
"baseUrl": "http://localhost:3030",
"FOO": "bar"
}
}
解决方案:将 baseUrl 属性 放在顶层,在 env 对象之外。
//正确的方法
{
"baseUrl": "https://.....",
"env": {
"FOO": "bar"
}
}
详情请看这里
https://docs.cypress.io/guides/references/configuration#Common-problems
我使用 cypress 进行了以下测试:
// 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 */
});
});
当我 运行 针对我的开发环境(它在 url 中没有端口,因为它在 443 上时,这工作正常)。
但是,我 运行 遇到了一个奇怪的场景,当我 运行 对我的本地服务器(在端口 3000 上)进行测试时,会发生以下情况:
- 它在浏览器控制台中记录
"the test is starting"
和"test email generatedTestEmail"
- 运行
setupProfile
很好,测试通过了。 - 然后,它似乎重新加载整个测试并重新记录 (1) 中的内容(使用新生成的电子邮件),但 (2) 仍显示为通过。
- 它试图 运行 我的
it("should test the thing")
阻止失败,因为现在我有一个新用户测试电子邮件。
当我仅切换主机以指向我的开发环境而不是本地时,它工作正常并且不会按照 (3) 中所述重新加载。
有没有人运行以前遇到过类似的事情?这可能与我在 URL 中有端口有关吗?
我会尝试利用 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 */
});
});
问题出在 baseUrl
配置上。这指向开发环境,运行 针对本地的脚本没有覆盖此配置,如下所述:https://docs.cypress.io/guides/references/configuration#Command-Line
这个解决方案对我有用: 您需要将真实的 baseUrl 放入配置文件中。请参阅下面的示例。
常见问题 未设置 baseUrl 确保您没有意外地将 baseUrl 或另一个 top-level 配置变量放入 env 块中。以下配置不正确,将无法工作:
//不起作用
{
"env": {
"baseUrl": "http://localhost:3030",
"FOO": "bar"
}
}
解决方案:将 baseUrl 属性 放在顶层,在 env 对象之外。
//正确的方法
{
"baseUrl": "https://.....",
"env": {
"FOO": "bar"
}
}
详情请看这里 https://docs.cypress.io/guides/references/configuration#Common-problems