Skip to content

Parsing spec tags into metadata

Ramon Klein edited this page Aug 22, 2018 · 8 revisions

If you decorate your describes or its texts in the style of https://www.npmjs.com/package/karma-jasmine-spec-tags e.g. it('file upload timeout check #slow', function() { expect(0).toEqual(1); }); _"#slow" is a tag in this example _ And if you want to process them in the following report of protractor-beautiful-reporter, you need to implement two steps:

  • Parse the tags into meaningful metadata
  • Show the results a in HTML page by using tags as extra "pivot" values

I will describe step 1) here and give a hint how to achieve step 2)

Step 1 include this code snippet in your protractor.conf and adapt it to your needs:

jasmine.getEnv().addReporter(
      new HtmlReporter({
        baseDirectory: e2eOutputDir
        , preserveDirectory: false
        // , cssOverrideFile: "assets/reporter-style.css",
        ,gatherBrowserLogs: true,
        jasmine2MetaDataBuilder: function (spec, descriptions, results, capabilities) {
          let metaData=originalJasmine2MetaDataBuilder(spec, descriptions, results, capabilities);
          metaData.tags=[];
          if(descriptions && descriptions.length >0) {
            for(let i=0;i<descriptions.length;i++) {
              //the prefix # can be changed to your desired tag pattern ...tags are limited to alphanumeric characters and underscore
              const tagRgx=/#([a-zA-Z0-9_]+)/g;
              var match;
              while(match=tagRgx.exec(descriptions[i])){
                if(match.length>1) {
                  metaData.tags.push(match[1]);
                }
              }
              //filter out tags from the standard view
              descriptions[i]=descriptions[i].replace(/#[a-zA-Z0-9]+/g,'');
            }
          }
          //call the original method after my own mods
          return metaData;
        }
      }).getJasmine2Reporter()
    );

You have to change # in the tagRgx if you want to use another tag prefix (see karma-jasmine-spec-tags for inspiration)

The above code snippet achieves that your combined.json and all single result .json files in your e2e output folder will contain an extra property tags:[] which contains all tags from you describes and its. e.g. a single JSON result file (I) it might look like this:

{
  "description": "should display welcome message|workspace-project App",
  "passed": false,
  "pending": false,
  "os": "Windows NT",
  "instanceId": 8248,
  "browser": {
    "name": "chrome",
    "version": "70.0.3519.0"
  },
  "message": [
    "Expected 'Welcome to protractor-test-app!' to equal 'Welcome to protractor-test-app 1!'."
  ],
  "trace": [],
  "tags": [
    "slow","scenario_1"
  ],
  "browserLogs": [],
  "screenShotFile": "00d60080-005b-00cc-0086-00ff000800c8.png",
  "timestamp": 1534961772039,
  "duration": 1831
}

For Step 2:

  • You might have noticed that the results of all e2e tests are stoed in a file called "combined.json"
  • Your can write an extra page with e.g. angularjs as an additional to normal html report which uses this data and these tags to present a custom pivot table view