如何使用茉莉花自定义记者发布失败的规格列表以发布到松弛?

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

I am trying to work on a custom jasmine reporter and get a list of all the failed specs in the specDone function:

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

failedExpectations 将存储失败规范的完整列表,我需要在量角器配置文件的 afterLaunch 函数中访问它.但是由于每次运行新规范时都会加载配置文件,因此它基本上会被覆盖,并且范围限定使得我无法在 afterLaunch 函数中访问它,这就是我调用松弛 api.有没有办法做到这一点?

where failedExpectations will store an entire list of the failed specs and i need to access this in the afterLaunch function in the protractor config file. But due to the fact that the config file loads everytime a new spec runs it basically gets overwritten and scoping is such that I cannot access it in the afterLaunch function, that is where I am making the call to the slack api. Is there a way to achieve this?

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

推荐答案

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

I think the best way is to post the results asynchronously after each spec (*or every "it" and "describe") using @slack/web-api. This way you don't have to worry about overwriting. Basically you "collect" all the results during the test run and send it before the next suite starts. Keep in mind all of this should be done as a class.

首先你准备好你的'@slack/web-api',所以安装它(https://www.npmjs.com/package/@slack/web-api).

First you prepare your you '@slack/web-api', so install it (https://www.npmjs.com/package/@slack/web-api).

npm i -D '@slack/web-api'

然后将其导入您的记者:

Then import it in your reporter:

import { WebClient } from '@slack/web-api';

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

And initialize it with your token. (https://slack.com/intl/en-pl/help/articles/215770388-Create-and-regenerate-API-tokens):

this.channel = yourSlackChannel;
this.slackApp = new WebClient(yourAuthToken);

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

Don't forget to invite your slack app to the channel. Then prepare your result "interface" according to your needs and possibilities. For example:

this.results = {
      title: '',
      status: '',
      color: '',
      successTests: [],
      fails: [],
    };

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

Then prepare a method / function for posting your results:

 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"时记得清除";结果:

When you got all of this ready it's time to "collect" your results. So on every 'suitStart' remember to "clear" the results:

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

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

Then collect success and failed tests:

 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我强烈推荐这个框架,你可以将它与 Jasmine 一起使用.

NOTE: It is just a draft based on reporter of mine. I just wanted to show you the flow. I was looking at Jasmine custom reporter but this was based on WDIO custom reporter based on 'spec reporter'. They are all very similar but you probably have to adjust it. The main point is to collect the results during the test and send them after each part of test run. *You can look up this explanation: https://webdriver.io/docs/customreporter.html I highly recommend this framework, you can use it with Jasmine on top.

相关文章