如何使用jsonAssertion.softAssert来断言元素是可见的



我在Cypress测试中使用softAssertions。我可以使用softAssert((方法来验证元素中的文本,但我正在努力弄清楚如何使用softAssert(sshould('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
})

})
}
});
Cypress.Commands.add('softAssertAll', () => jsonAssertion.softAssertAll())

这是我的自定义命令,这是实际的测试

describe('Load Validation Test', function(){
const jsonAssertion = require("soft-assert")

it('Load Validation Test', function(){ 
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')
})
})

对于任何开箱即用不支持的条件,请使用常规.then()回调来应用jsonAssertion.softAssert()

const jsonAssertion = require("soft-assert")
it('Load Validation Test', function() { 
cy.get('input[placeholder="Activity Name"]')
.then($input => {
const isVisible = $input.is(':visible')
jsonAssertion.softAssert(isVisible, true, 'should be visible')
})
})

这是可行的,但您需要一个包装器来提取actualexpected

Cypress.Commands.add('softVisible', {prevSubject:true}, 
(subject, expectedCondition, message) => {
// translate expectedCondition to true/false
const expected = expectedCondition === 'be.visible'

// get actual from subject
const actual = subject.is(":visible")
cy.softAssert(actual, expected, message)
})
// usage
cy.get('input[placeholder="Activity Name"]').softVisible('be.visible')
cy.get('input[placeholder="Activity Name"]').softVisible('not.be.visible')

如果您想控制测试流,请小心使用softVisible

cy.get('input[placeholder="Activity Name"]')
.softVisible('be.visible')
.type('my activity')

这不会在运行.type()之前等待可见性。

正如我在soft-assert npm库中看到的,它们支持以下所有方法,并且我看不到检查visibility的方法。

1.  deepAssert(actual, expected, msg, ignoreKeys)
2.  softAssert(actual, expected, msg, ignoreKeys)
3.  deepContains(actual, expected, msg, ignoreKeys)
4.  softContains(actual, expected, msg, ignoreKeys)
5.  deepAssertKey(actual, expected, key, msg, ignoreKeys)
6.  softAssertKey(actual, expected, key, msg, ignoreKeys)
7.  deepContainstKey(actual, expected, key, msg, ignoreKeys)
8.  softContainsKey(actual, expected, key, msg, ignoreKeys)
9.  deeptTrue(value, msg)
10. softTrue(value, msg)
11. deepAssertKeyAbsence(actual, key, msg)
12. softAssertKeyAbsence(actual, key, msg)
13. softAssertAll()

或者,如果你只想测试元素是否可见,并在不失败的情况下执行一些操作,你可以:

describe('Load Validation Test', function () {
it('Load Validation Test', function () {
if (Cypress.$('input[placeholder="Activity Name"]').is(':visible')) {
//Element is visible, do something
} else {
//Element is not visible, do something
}
})
})

除了您的两个方法softAssert和softAssertAll之外,您只需要再添加一个,以验证是否存在一个元素。就像这样。

Cypress.Commands.add('isTheElementPresent', (ele) => {
cy.get('body').then($body => {
if ($body.find(ele).length) return true
else return false
});
})

然后,在你的测试中,你可以用这种方式组合这些方法:

cy.isTheElementPresent('.site-description').then(isTrue => {
cy.softAssert(isTrue, true, "expected this element to be present!");
})
// Here we can put extra assertions or any other additional code...
// Finally:
cy.softAssertAll();

最新更新