我认为最好的方法是使用 @slack/web-api 在每个规范(*或每个“it”和“describe”)之后异步发布结果。这样您就不必担心覆盖。基本上,您在测试运行期间“收集”所有结果,并在下一个套件开始之前发送它。请记住,所有这些都应该在课堂上完成。
首先,准备好“@slack/web-api”,然后安装它(https://www.npmjs.com/package/@slack/web-api https://www.npmjs.com/package/@slack/web-api).
npm i -D '@slack/web-api'
然后将其导入到您的报告器中:
import { WebClient } from '@slack/web-api';
并使用您的令牌对其进行初始化。 (https://slack.com/intl/en-pl/help/articles/215770388-Create-and-regenerate-API-tokens https://slack.com/intl/en-pl/help/articles/215770388-Create-and-regenerate-API-tokens):
this.channel = yourSlackChannel;
this.slackApp = new WebClient(yourAuthToken);
不要忘记邀请您的 Slack 应用程序加入频道。
然后根据您的需要和可能性准备您的结果“界面”。例如:
this.results = {
title: '',
status: '',
color: '',
successTests: [],
fails: [],
};
然后准备一个方法/函数来发布你的结果:
postResultOnSlack = (res) => {
try {
this.slackApp.chat.postMessage({
text: `Suit name: ${res.title}`,
icon_emoji: ':clipboard:',
attachments: [
{
color: res.color,
fields: [
{
title: 'Successful tests:',
value: ` ${res.successTests}`,
short: false
},
{
title: 'Failed tests:',
value: ` ${res.fails}`,
short: false
},
]
}
],
channel: this.channel
});
console.log('Message posted!');
} catch (error) {
console.log(error);
}
当您准备好所有这些后,就可以“收集”您的结果了。
因此,在每次“suitStart”时,请记住“清除”结果:
suiteStarted(result) {
this.results.title = result.fullName;
this.results.status = '';
this.results.color = '';
this.results.successTests = [];
this.results.fails = [];
}
然后收集成功和失败的测试:
onSpecDone(result) {
this.results.status = result.status
// here you can push result messages or whole stack or do both:
this.results.successTests.push(`${test.passedExpectations}`);
for(var i = 0; i < result.failedExpectations.length; i++) {
this.results.fails.push(test.failedExpectations[i].message);
}
// I'm not sure what is the type of status but I guess it's like this:
result.status==1 ? this.results.color = #DC143C : this.results.color = #048a04;
}
最后发送给他们:
suiteDone() {
this.postResultOnSlack(this.results);
}
注:这只是我的记者的草稿。我只是想向你展示流程。我正在查看 Jasmine 自定义报告器,但这是基于“规范报告器”的 WDIO 自定义报告器。它们都非常相似,但您可能必须对其进行调整。要点是在测试期间收集结果并在测试运行的每个部分之后发送它们。
*你可以查一下这个解释:https://webdriver.io/docs/customreporter.html https://webdriver.io/docs/customreporter.html我强烈推荐这个框架,你可以将它与 Jasmine 一起使用。