Javascript 在Cypress中寻找cy.wait()的替代品
所以我现在在Cypress有一个不稳定的测试用例。当模态加载时,它跳过填写名称部分,但填写其他所有内容。因此,导致它第一次失败,但在重试时,它通过了。我尝试添加一个断言(见下文),但它不起作用。我试图避免使用wait命令添加,但想看看是否还有其他方法可以替代cy.wait()Javascript 在Cypress中寻找cy.wait()的替代品,javascript,testing,cypress,race-condition,Javascript,Testing,Cypress,Race Condition,所以我现在在Cypress有一个不稳定的测试用例。当模态加载时,它跳过填写名称部分,但填写其他所有内容。因此,导致它第一次失败,但在重试时,它通过了。我尝试添加一个断言(见下文),但它不起作用。我试图避免使用wait命令添加,但想看看是否还有其他方法可以替代cy.wait() 有很多方法可以进行不可靠的测试。您可以尝试这些方法,并检查哪些最适合您的用例: 一,。简单有效,只要在出现故障时自动重试执行即可。可以全局使用cypress.json或应用于描述块或它块 针对it块: // `it`
有很多方法可以进行不可靠的测试。您可以尝试这些方法,并检查哪些最适合您的用例: 一,。简单有效,只要在出现故障时自动重试执行即可。可以全局使用
cypress.json
或应用于描述
块或它
块
针对it块:
// `it` test block with custom configuration
it('allows user to login', {
retries: {
runMode: 2,
openMode: 2
}
}, () => {
// ...
})
// Customizing retry attempts for a suite of tests
describe('User bank accounts', {
retries: {
runMode: 2,
openMode: 2,
}
}, () => {
// The per-suite configuration is applied to each test
// If a test fails, it will be retried
it('allows a user to view their transactions', () => {
// ...
}
it('allows a user to edit their transactions', () => {
// ...
}
})
对于描述块:
// `it` test block with custom configuration
it('allows user to login', {
retries: {
runMode: 2,
openMode: 2
}
}, () => {
// ...
})
// Customizing retry attempts for a suite of tests
describe('User bank accounts', {
retries: {
runMode: 2,
openMode: 2,
}
}, () => {
// The per-suite configuration is applied to each test
// If a test fails, it will be retried
it('allows a user to view their transactions', () => {
// ...
}
it('allows a user to edit their transactions', () => {
// ...
}
})
2.在写入名称字段之前,添加.visible
和单击()
it("Can fill out form and submit", () => {
cy.get("#modal-button").click()
cy.get("#modal-form").should("exist")
cy.get("#form-name").should('be.visible') //making sure the name field is visible
cy.get("#form-name").click() //clicking on the name field
cy.get("#form-name").type("Mike Johnson")
cy.get("#form-age").type("33")
cy.get("#form-email").type("mj09@yahoo.com")
cy.get("#submit-button").click()
})
3.使用超时而不是等待
cy.get("#form-name", {timeout: 10000}).type("Mike Johnson")
4.用于等待XHR请求完成执行。在这里,我假设在单击导致问题的模式按钮之后,有一些未完成的XHR请求。如果不是这样,您可以调试您的测试以发现问题并相应地应用拦截
it("Can fill out form and submit", () => {
cy.intercept('http://example.com/settings').as('getSettings') //Intercept the url that is triggered after clicking the modal button
cy.get("#modal-button").click()
cy.wait('@getSettings') //Wait till that request has finished execution
cy.get("#modal-form").should("exist")
cy.get("#form-name").type("Mike Johnson")
cy.get("#form-age").type("33")
cy.get("#form-email").type("mj09@yahoo.com")
cy.get("#submit-button").click()
})
您应该在type()
命令中添加一个超时(而不是get()
命令中添加一个超时),并在其后添加一个should()
,以确认输入了值
type()
和should()
都将重试,直到成功
it(“可以填写表格并提交”,()=>{
cy.get(“#模式按钮”)。单击()
cy.get(“#模态形式”)//。应该(“存在”)-不需要,因为cy.get()已经这样做了
cy.get(“#form name”).type(“Mike Johnson”,{timeout:10000})//等待可操作性
.should('have.value','Mike Johnson');
cy.get(“#form age”).类型(“33”)
cy.get(“#表单电子邮件”)。键入(“mj09@yahoo.com")
cy.get(“#提交按钮”)。单击()
})