问题描述
`我已经从 npm (npm i soft-assert) 配置了软断言,现在我的 package.josn 有软断言":^0.2.3"
`I have configured soft assert from npm (npm i soft-assert) and now my package.josn has "soft-assert": "^0.2.3"
我想使用软断言的功能'softAssert(actual, expected, msg, ignoreKeys)'
i want to use function of Soft assert'softAssert(actual, expected, msg, ignoreKeys)'
但是不知道具体的使用步骤是什么
But don't know, what is the exact steps to use it
示例:当我在代码中使用软断言函数时,出现以下错误.
Example:When i use soft assertion function in my code, getting following error.
如果我这样使用
- cy.softAssert(10, 12, "expected actual mismatch and will execute next line") : 不支持或者如果我使用不同的方式像
- softAssert(10, 12, "expected actual mismatch and will execute next line") : SoftAssert 未定义
谁能告诉我如何通过一些小例子在 cypress 代码中使用这个softAssert"函数.`
Can any one tell me how to use this 'softAssert' function in cypress code with some small example.`
现在我面临的问题
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")"语句.
I need my code after soft assertion as cy.log("text")
to be executed in the same 'it' block but the current test failing the whole 'it' block, without executing 'cy.log("text")' statement.
推荐答案
软断言概念非常酷,您可以将它以最少的实现添加到 Cypress
The soft assertion concept is pretty cool, and you can add it with minimal implementation to 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
})
对我来说,在日志中看到每个软断言失败会更好,因此可以添加自定义命令来包装软断言功能
To me, it would be nicer to see each soft assertion failure in the log, so it's possible to add custom commands to wrap the soft-assert functions
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();
})
这篇关于我如何在 Cypress 中使用软断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!