我已经成功地实现了这个表达式的答案,并且可以像这样softAssert(softAssert,'message')。我不会转载下面的代码,它包含在链接中。
但是,我注意到在使用mochawesome report生成器时,它的报告内容使用中的正文
'test:after:run', (result) => {
result.body
}通常在Cypress中,这是由window.it给出的,其中.it表示规范文件中的每个it(“测试标题”)块。
但是当使用softAssert时,特别是在本部分中,其中window.it是从commands.js覆盖的:
// monkey-patch `it` callback so we insert `cy.then()` as a last command
// to each test case where we'll assert if there are any soft assertion errors
function itCallback ( func ) {
func();
cy.then(() => {
if ( errors.length ) {
const _ = Cypress._;
let msg = '';
if ( Cypress.browser.isHeaded ) {
msg = 'Failed soft assertions... check log above ↑';
} else {
_.each( errors, error => {
msg += '\n' + error;
});
msg = msg.replace(/^/gm, '\t');
}
throw new Error(msg);
}
});
}
const origIt = window.it;
window.it = (title, func) => {
origIt(title, func && (() => itCallback(func)));
};
window.it.only = (title, func) => {
origIt.only(title, func && (() => itCallback(func)));
};
window.it.skip = (title, func) => {
origIt.skip(title, func);
};问题是我的报告现在将测试主体显示为:
return itCallback(func)我假设这是因为这个东西:origIt(title, func && (() => itCallback(func)));
如何在保留softAssert功能的同时修复此问题并返回实际的it()块体。
在过去的几天里,我一直在尝试解决这个问题,但没有成功。如果https://stackoverflow.com/users/927631/dwelle、https://stackoverflow.com/users/5878476/jennifer-shehane或https://stackoverflow.com/users/4168257/gleb-bahmutov是在线的,我需要一个真正的专业人士来联系我,我被难住了。
发布于 2021-04-22 19:19:02
我猜他们叫它猴子补丁是有原因的。
看起来您正在尝试在it()之后运行错误收集代码,为什么不使用afterEach()呢
afterEach(() => {
if ( errors.length ) {
const separator = '\n\t';
const msg = Cypress.browser.isHeaded ?
'Failed soft assertions... check log above ↑' :
seperator + errors.join(separator);
throw msg;
}
})这里给出了一种做软断言的更好的方法How can i use soft assertion in Cypress。
它利用了soft-assert包(17000下载pw),并提供了几个简单的自定义命令来实现
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())https://stackoverflow.com/questions/67209904
复制相似问题