【问题标题】:Cypress - how to assert multiple errors at the same time赛普拉斯 - 如何同时断言多个错误
【发布时间】:2020-06-26 11:16:23
【问题描述】:
我有一个表格,它有 -say- 5 个需要填写的输入。我想测试验证是否正确启动。
因此,在尝试发送空表单时,我应该能够找到 5 条错误消息。
我想拥有类似的东西:
cy.get('form')
.should('contain 5 times', 'You should fill this up, mate')
有没有可能,或者我不得不在无用的重复行上浪费无尽的虚拟树?
【问题讨论】:
标签:
validation
automated-tests
cypress
【解决方案1】:
这种方式是行不通的。如果您希望它干净地包裹在外部助手上并用于您的目的。
最短的方法是:
cy.get('.error-text').each((item) => {
expect(item).to.contain('You should fill this up, mate')
})
【解决方案2】:
我最终使用了
cy.get('form')
.should('contain', 'You should fill this up, mate')
cy.get('.invalid-input-error-msg')
.should('have.length', 5)
虽然不完美,但还过得去
【解决方案3】:
以下代码满足您的要求。如果您有以下要求。
在不填写表单的情况下单击注册按钮,您应该会看到每个字段的错误消息...您需要更正的只是下面代码中的定位器。
cy.get('#sign-up-button').click(); // Click on sign-up button for example without filling form
cy.get('form').within(() => { // Basically you are asserting inside form for each individual fields
cy.get('#username').should('have.text', 'you should fill username input')
cy.get('#password').should('have.text', 'you should fill password input')
cy.get('#email').should('have.text', 'you should fill email input')
cy.get('#address').should('have.text', 'you should fill address input')
cy.get('#gender').should('have.text', 'you should fill gender input')
})