Smi*_*jan 11 automation assertion cypress
我已经从 npm 配置了软断言(npm i soft-assert),现在我的 package.josn 有 "soft-assert": "^0.2.3"
我想使用软断言功能
softAssert(actual, expected, msg, ignoreKeys)
Run Code Online (Sandbox Code Playgroud)
但不知道具体的使用步骤是什么
示例:当我在代码中使用软断言函数时,出现以下错误。
如果我这样使用
谁能告诉我如何通过一些小例子在 cypress 代码中使用这个“softAssert”函数?
现在我面临的问题
it('asserts and logs and fails', () => {
Cypress.softAssert(10, 12, "expected actual mismatch...");
cy.log("text")
Cypress.softAssertAll();
})
Run Code Online (Sandbox Code Playgroud)
我需要在软断言之后cy.log("text")在同一个“it”块中执行我的代码,但当前测试使整个“it”块失败,而不执行“cy.log(“text”)”语句。
Ric*_*sen 16
软断言概念非常酷,您可以通过最少的实现将其添加到 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
})
Run Code Online (Sandbox Code Playgroud)
对我来说,最好在日志中看到每个软断言失败,因此可以添加自定义命令来包装软断言函数
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();
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7336 次 |
| 最近记录: |