我如何在赛普拉斯中使用软断言
How can i use soft assertion in Cypress
`我已经从 npm (npm i soft-assert) 配置了软断言,现在我的 package.josn 有 "soft-assert": "^0.2.3"
我想使用Soft assert的功能
'softAssert(actual, expected, msg, ignoreKeys)'
但是不知道,具体使用步骤是什么
示例:
当我在我的代码中使用软断言函数时,出现以下错误。
如果我这样使用
- 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' 函数。`
现在我面临的问题
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();
})
`我已经从 npm (npm i soft-assert) 配置了软断言,现在我的 package.josn 有 "soft-assert": "^0.2.3"
我想使用Soft assert的功能 'softAssert(actual, expected, msg, ignoreKeys)'
但是不知道,具体使用步骤是什么
示例: 当我在我的代码中使用软断言函数时,出现以下错误。
如果我这样使用
- 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' 函数。`
现在我面临的问题
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();
})