第一次尝试使用 cypress 失败,但第二次总是通过而不重试
Using cypress fails on the first attempt but always passes on the second without retrying
澄清
我是故意让测试失败的。我对失败的测试启用了 1 次重试。
问题
如果测试在 Attempt 1
上失败,我预计它也会在 Attempt 2
上失败。
实际行为
测试在 Attempt 1
上失败,但随后在 Attempt 2
上被标记为通过,而没有实际重试。
一些细节
我有一些赛普拉斯的测试。在此特定测试中,我正在检查 URL
的一部分以包含特定字符串并故意使其失败。第一次尝试 timeout
失败,但第二次尝试总是通过而不重试。
为什么它不重试或第二次尝试失败?我相信这与我的断言有关。
我也尝试过 expect
和 then
,因为它们是 运行 异步
cy.url().then(url => {
expect(url).to.include("homssse.faces")
});
设置详细信息
在我的 command.js
文件中,我添加了一个 login
命令
Cypress.Commands.add('login', (username, password) => {
cy.visit('signOn.faces')
.get(".global-header__logo")
.should("be.visible")
cy
.get('input[name="username"]')
.type(Cypress.env('username'))
.should('have.value', Cypress.env('username'));
cy
.get('input[name="password"]')
.type(Cypress.env('password'))
.should('have.value', Cypress.env('password'));
cy
.get('[name="submit"]')
.click();
})
在我的 critical_user_journeys.spec.js
中,我有多个测试,但我在这里展示的是第一个。在这个测试中,在我调用 login
之后,我想断言我已经登陆了我的主页,但在这里我故意让测试失败,以验证它在实际中断时确实失败了。我已经对来自 cypress.json
的所有测试启用了重试,所以自然地,当测试失败时,它会再次尝试 运行。我也设置了超时,详情在下面。
describe("Critical User Journeys", function () {
it("signs in user and lands on 'HomePage'", () => {
cy
.login()
cy.location().should((loc) => {
expect(loc.href).to.include(
'this_doesnt_exist_on_the_url' // break tests on purpose
)
})
})
})
cypress.json
文件中的一些设置
...
"pageLoadTimeout": 30000,
"defaultCommandTimeout": 20000,
"chromeWebSecurity": false,
"retries": {
"runMode": 1,
"openMode": 1
}
...
重要提示
当断言更改为 expect(true).to.be.false;
时,两次尝试都失败了,这也是我希望在另一个断言中看到的行为。
谢谢
有几种不同的方法可以添加断言。他们第一次尝试都失败了吗?
cy.url().should('eq', 'http://localhost:8000/users/1/edit') // => true
或
cy.url().should('include', '/index.html')
您可以添加超时以确保 url 得到解决。默认超时为 4000 毫秒。
cy.url({timeout: 10000}).should('include', 'home.faces')
我通过在 describe
中添加一个空的 beforeEach
钩子来实现预期的行为。我不确定为什么添加这个会改变行为,但是当我在 documentation 上看到它时,我添加了一个来检查结果并得到了我正在寻找的结果。不过,仍然不确定这是设计使然还是错误。
这是 critical_user_journeys.spec.js
文件现在的样子
describe("Critical User Journeys", function () {
beforeEach(function () {})
it("signs in user and lands on 'HomePage'", () => {
cy
.login()
cy.location().should((loc) => {
expect(loc.href).to.include(
'this_doesnt_exist_on_the_url' // break tests on purpose
)
})
})
})
使用此实现 Attempt 1
失败,然后 Attempt 2
也失败。结果,整个测试被标记为 'Failed'.
澄清
我是故意让测试失败的。我对失败的测试启用了 1 次重试。
问题
如果测试在 Attempt 1
上失败,我预计它也会在 Attempt 2
上失败。
实际行为
测试在 Attempt 1
上失败,但随后在 Attempt 2
上被标记为通过,而没有实际重试。
一些细节
我有一些赛普拉斯的测试。在此特定测试中,我正在检查 URL
的一部分以包含特定字符串并故意使其失败。第一次尝试 timeout
失败,但第二次尝试总是通过而不重试。
为什么它不重试或第二次尝试失败?我相信这与我的断言有关。
我也尝试过 expect
和 then
,因为它们是 运行 异步
cy.url().then(url => {
expect(url).to.include("homssse.faces")
});
设置详细信息
在我的 command.js
文件中,我添加了一个 login
命令
Cypress.Commands.add('login', (username, password) => {
cy.visit('signOn.faces')
.get(".global-header__logo")
.should("be.visible")
cy
.get('input[name="username"]')
.type(Cypress.env('username'))
.should('have.value', Cypress.env('username'));
cy
.get('input[name="password"]')
.type(Cypress.env('password'))
.should('have.value', Cypress.env('password'));
cy
.get('[name="submit"]')
.click();
})
在我的 critical_user_journeys.spec.js
中,我有多个测试,但我在这里展示的是第一个。在这个测试中,在我调用 login
之后,我想断言我已经登陆了我的主页,但在这里我故意让测试失败,以验证它在实际中断时确实失败了。我已经对来自 cypress.json
的所有测试启用了重试,所以自然地,当测试失败时,它会再次尝试 运行。我也设置了超时,详情在下面。
describe("Critical User Journeys", function () {
it("signs in user and lands on 'HomePage'", () => {
cy
.login()
cy.location().should((loc) => {
expect(loc.href).to.include(
'this_doesnt_exist_on_the_url' // break tests on purpose
)
})
})
})
cypress.json
文件中的一些设置
...
"pageLoadTimeout": 30000,
"defaultCommandTimeout": 20000,
"chromeWebSecurity": false,
"retries": {
"runMode": 1,
"openMode": 1
}
...
重要提示
当断言更改为 expect(true).to.be.false;
时,两次尝试都失败了,这也是我希望在另一个断言中看到的行为。
谢谢
有几种不同的方法可以添加断言。他们第一次尝试都失败了吗?
cy.url().should('eq', 'http://localhost:8000/users/1/edit') // => true
或
cy.url().should('include', '/index.html')
您可以添加超时以确保 url 得到解决。默认超时为 4000 毫秒。
cy.url({timeout: 10000}).should('include', 'home.faces')
我通过在 describe
中添加一个空的 beforeEach
钩子来实现预期的行为。我不确定为什么添加这个会改变行为,但是当我在 documentation 上看到它时,我添加了一个来检查结果并得到了我正在寻找的结果。不过,仍然不确定这是设计使然还是错误。
这是 critical_user_journeys.spec.js
文件现在的样子
describe("Critical User Journeys", function () {
beforeEach(function () {})
it("signs in user and lands on 'HomePage'", () => {
cy
.login()
cy.location().should((loc) => {
expect(loc.href).to.include(
'this_doesnt_exist_on_the_url' // break tests on purpose
)
})
})
})
使用此实现 Attempt 1
失败,然后 Attempt 2
也失败。结果,整个测试被标记为 'Failed'.