检查赛普拉斯 beforeEach 中元素的长度
Checking length of element in beforeEach on Cypress
我正在尝试设置我的 beforeEach
以在开始我的测试之前创建一封新电子邮件,如果 none 存在,但是当我尝试计算我的 [=11] 中有多少电子邮件时=],它总是记录为 0
,即使有更多。如果我在 it
块中记录 count
,它会显示正确的计数。
这看起来正确吗?我怎样才能正确检查 beforeEach
中的 count
以便它不会总是为每个测试创建一封新电子邮件?
describe("email", () => {
beforeEach(() => {
cy.visit("/");
let count = Cypress.$('#emails').length
// always logs 0
cy.log(count)
if(count == 0) {
createNewEmail()
}
});
it("email", () => {
let count = Cypress.$('#emails').length
// logs correct amount
cy.log(count)
});
});
Cypress commands are asynchronous。所以当测试执行时cy.visit("/")
并不意味着下一行将在页面真正加载后执行。
您可以按如下方式解决此问题:
beforeEach(() => {
cy.visit("/").then(() => {
let count = Cypress.$('#emails').length
cy.log(count)
if(count == 0) {
createNewEmail()
}
})
}
我正在尝试设置我的 beforeEach
以在开始我的测试之前创建一封新电子邮件,如果 none 存在,但是当我尝试计算我的 [=11] 中有多少电子邮件时=],它总是记录为 0
,即使有更多。如果我在 it
块中记录 count
,它会显示正确的计数。
这看起来正确吗?我怎样才能正确检查 beforeEach
中的 count
以便它不会总是为每个测试创建一封新电子邮件?
describe("email", () => {
beforeEach(() => {
cy.visit("/");
let count = Cypress.$('#emails').length
// always logs 0
cy.log(count)
if(count == 0) {
createNewEmail()
}
});
it("email", () => {
let count = Cypress.$('#emails').length
// logs correct amount
cy.log(count)
});
});
Cypress commands are asynchronous。所以当测试执行时cy.visit("/")
并不意味着下一行将在页面真正加载后执行。
您可以按如下方式解决此问题:
beforeEach(() => {
cy.visit("/").then(() => {
let count = Cypress.$('#emails').length
cy.log(count)
if(count == 0) {
createNewEmail()
}
})
}