我在 Cypress 测试中努力实现软断言。 我需要将所有断言转换为软断言。我遇到的问题是我无法在 jsonAssertion 中找到该元素。例如 cy.get('span[class="h4"]') 是元素,我需要断言它包含一些文本。如何使用 jsonAssertion.softAssert() 完成此操作?
这是我的测试:
describe('Load Validation Test', function(){
const jsonAssertion = require("soft-assert")
it('Load Validation Test', function(){
let url = Cypress.config().baseUrl
cy.visit(url+'activityTaskManagement')
cy.get('span[class="h4"]').should('contain.text','Manage Activities')
cy.get('button[ng-click="vm.addActivityTask();"]').should('be.visible')
cy.get('button[ng-click="vm.addActivityTaskBulk();"]').should('be.visible')
cy.get('input[placeholder="Activity Name"]').should('be.visible')
cy.get('div table[class="table table-striped b-t b-light table-nowrap"]').should('be.visible')
})
})
最佳答案
有关软断言,请参阅 How can i use soft assertion in Cypress
作为自定义命令,
const jsonAssertion = require("soft-assert")
Cypress.Commands.add('softAssert', (actual, expected, message) => {
jsonAssertion.softAssert(actual, expected, message)
if (jsonAssertion.jsonDiffArray.length) {
jsonAssertion.jsonDiffArray.forEach(diff => {
const log = Cypress.log({
name: 'Soft assertion error',
displayName: 'softAssert',
message: diff.error.message
})
})
}
});
Cypress.Commands.add('softAssertAll', () => jsonAssertion.softAssertAll())
在测试中
cy.get('span[class="h4"]').then($el=> {
const actual = $el.text()
cy.softAssert(actual, 'Manage Activities')
})
关于automated-tests - 如何在我的 Cypress 测试中实现软断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71262780/