如何使用茉莉花自定义报告程序发布失败的规格列表以发布到松弛状态? [英] How to make a list of failed specs using jasmine custom reporter to post to slack?

查看:51
本文介绍了如何使用茉莉花自定义报告程序发布失败的规格列表以发布到松弛状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用自定义的茉莉花记者,并在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函数中访问它,这就是我在调用slack 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

This is what i have it based on : 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',因此请安装它(

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/zh-CN/pl/help/articles/215770388-创建并重新生成API令牌):

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);

别忘了邀请闲暇的应用程序加入频道. 然后准备您的结果界面".根据您的需求和可能性.例如:

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.

这篇关于如何使用茉莉花自定义报告程序发布失败的规格列表以发布到松弛状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆