我正在努力在我的赛普拉斯测试中实现软断言。我需要将所有断言转换为软断言。我遇到的问题是我无法在 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')
})
})
Run Code Online (Sandbox Code Playgroud) 我在 Cypress 测试中使用了 softAssertions。我能够使用softAssert()方法来验证元素中的文本,但我试图弄清楚如何使用softAssert()方法来断言元素可见。在 cypress 中,我使用.should('be.visible')很简单,但我似乎无法使其与softAssert()方法一起使用。我试图断言的元素是输入字段、表格和按钮。我在下面举了一个简单的例子。
我就是这样做的:
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('softContains', (actual, expected, message) => {
jsonAssertion.softContains(actual, expected, message)
if (jsonAssertion.jsonDiffArray.length) {
jsonAssertion.jsonDiffArray.forEach(diff => {
const log = Cypress.log({
name: 'Soft assertion error',
displayName: 'softContains',
message: diff.error.message
})
})
} …Run Code Online (Sandbox Code Playgroud)