如何使用茉莉花自定义报告器发布失败的规格列表以发布到 slack?

pj0*_*013 6 javascript jasmine protractor slack-api e2e-testing

我正在尝试使用自定义 jasmine 报告器并获取 specDone 函数中所有失败规范的列表:

specDone: function(result) {
    if(result.status == 'failed') {          
        failedExpectations.push(result.fullName);
        console.log(failedExpectations);         
    }       
}
Run Code Online (Sandbox Code Playgroud)

在哪里failedExpectations将存储失败规范的完整列表,我需要afterLaunch在量角器配置文件的函数中访问它。但是由于配置文件在每次运行新规范时都会加载,它基本上会被覆盖,并且范围使得我无法在afterLaunch函数中访问它,这就是我调用 slack api 的地方。有没有办法实现这一目标?

这是我基于的内容:http : //jasmine.github.io/2.1/custom_reporter.html

Viv*_*ppo 1

我认为最好的方法是使用 @slack/web-api 在每个规范(*或每个“it”和“describe”)之后异步发布结果。这样您就不必担心覆盖。基本上,您在测试运行期间“收集”所有结果,并在下一个套件开始之前发送它。 请记住,所有这些都应该在课堂上完成。

首先,准备好“@slack/web-api”,然后安装它(https://www.npmjs.com/package/@slack/web-api)。

npm i -D '@slack/web-api'
Run Code Online (Sandbox Code Playgroud)

然后将其导入到您的报告器中:

import { WebClient } from '@slack/web-api';
Run Code Online (Sandbox Code Playgroud)

并使用您的令牌对其进行初始化。(https://slack.com/intl/en-pl/help/articles/215770388-Create-and-regenerate-API-tokens):

this.channel = yourSlackChannel;
this.slackApp = new WebClient(yourAuthToken);
Run Code Online (Sandbox Code Playgroud)

不要忘记邀请您的 Slack 应用程序加入频道。然后根据您的需要和可能性准备您的结果“界面”。例如:

this.results = {
      title: '',
      status: '',
      color: '',
      successTests: [],
      fails: [],
    };
Run Code Online (Sandbox Code Playgroud)

然后准备一个方法/函数来发布你的结果:

 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);
}
Run Code Online (Sandbox Code Playgroud)

当您准备好所有这些后,就可以“收集”您的结果了。因此,在每次“suitStart”时,请记住“清除”结果:

  suiteStarted(result) {
    this.results.title = result.fullName;
    this.results.status = '';
    this.results.color = '';
    this.results.successTests = [];
    this.results.fails = [];
  }
Run Code Online (Sandbox Code Playgroud)

然后收集成功和失败的测试:

 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;
    }
Run Code Online (Sandbox Code Playgroud)

最后发送给他们:

suiteDone() {
      this.postResultOnSlack(this.results);
    }
Run Code Online (Sandbox Code Playgroud)

注:这只是我的记者的草稿。我只是想向你展示流程。我正在查看 Jasmine 自定义报告器,但这是基于基于“规范报告器”的 WDIO 自定义报告器。它们都非常相似,但您可能必须对其进行调整。要点是在测试期间收集结果并在测试运行的每个部分之后发送它们。*你可以查一下这个解释:https://webdriver.io/docs/customreporter.html 我强烈推荐这个框架,你可以在上面与 Jasmine 一起使用。