`我已经从 npm (npm i soft-assert) 配置了软断言,现在我的 package.josn 有 "soft-assert": "^0.2.3"
我想使用软断言的功能
'softAssert(实际,预期,消息,忽略键)'
但是不知 Prop 体的使用步骤是什么
例子:
当我在代码中使用软断言函数时,出现以下错误。
如果我像这样使用
或者如果我使用不同的方式,如
任何人都可以通过一些小例子告诉我如何在 cypress 代码中使用这个“softAssert”函数。`
现在我面临的问题
it('asserts and logs and fails', () => {
Cypress.softAssert(10, 12, "expected actual mismatch...");
cy.log("text")
Cypress.softAssertAll();
})
软断言后我需要我的代码为 cy.log("text")
将在同一个“it”块中执行,但当前测试在整个“it”块中失败,而不执行“cy.log("text")”语句。
最佳答案
软断言概念非常酷,您可以将它以最少的实现添加到 Cypress
const jsonAssertion = require("soft-assert")
it('asserts several times and only fails at the end', () => {
jsonAssertion.softAssert(10, 12, "expected actual mismatch");
// some more assertions, not causing a failure
jsonAssertion.softAssertAll(); // Now fail the test if above fails
})
对我来说,在日志中看到每个软断言失败会更好,所以可以添加自定义命令来包装软断言功能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())
//-- all above can go into /cypress/support/index.js
//-- to save adding it to every test (runs once each test session)
it('asserts and logs but does not fail', () => {
cy.softAssert(10, 12, "expected actual mismatch...");
cy.log('text'); // this will run
})
it('asserts and logs and fails', () => {
cy.softAssert(10, 12, "expected actual mismatch...");
cy.log('text'); // this will run
cy.softAssertAll();
})
关于automation - 我如何在 Cypress 中使用软断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66132293/