user3055964
user3055964

Reputation: 151

Jasmine get current test result

I am using Jasmine - 3.3.1, with combination of ProtractorJS.

My requirement is to store the result for each spec (or describe / test) and update results in Testrail system using the afterEach() method. I want to store the results into the variable "testResult".

Tried with various methods - custom_reports.js etc, but could not get what I needed.

Code snippet:

var testResult;

describe('1st scenario', function () { 
    it('1st Test', function () {
        expect(true).toBe(true);
        testResult=5;
    }); 
 });

describe('2nd scenario', function () { 
    it('2nd Test', function () {
        expect(true).toBe(true);
        testResult=1;
    }); 
 });


afterEach(function () {
    helper.updateResults("Section Name", testcaseID, testResult);
});

Upvotes: 1

Views: 2163

Answers (1)

DublinDev
DublinDev

Reputation: 2348

I achieved something similar by creating my own custom reporter. My reporter uploads spec results (it blocks) to a dynamoDB table after each spec finishes and uploads suite results (describe blocks) after all tests have finished. All uploads occur asynchronously but in the onComplete all of the async upload actions are awaited.

Obviously I am using the async / await approach as opposed to the SELENIUM_PROMISE_MANAGER you appear to. I would recommend making that change over.

DBReporter.js

function dbReporter() {

    this.jasmineStarted = function (options) {};    
    this.specStarted = function (result) {};    
    this.specDone = async function (result) {

        if (result.status == 'pending') {
        }
        else if (result.status == 'passed') {
        }
        else if (result.status == 'failed') {
            //Put your testrail interaction code here
        }

        testResultsUploadQueue.push(result);
    };

    this.suiteStarted = function (result) {};    
    this.suiteDone = function (result) {}    
    this.jasmineDone = async function (result) {}
}

module.exports = dbReporter;

conf.js

onPrepare: async () => {
    //require the dpReporter file
    let dbReporter = require('../src/functions/db-reporter');

    //Declare a global variable that will contain all the asynchronous upload actions (promises)
    global.testResultsUploadQueue = [];

    //initialize the dbreporer
    await jasmine.getEnv().addReporter(new dbReporter());
}),
onComplete: async() => {
    //Wait for all uploads to resolve before completing
    let testRulesUploadValue = await Promise.all(testResultsUploadQueue);
    console.log(`    ${testRulesUploadValue.length} result files uploaded to dynamoDB`);
}

No changes required in your spec files

The Constraints

  1. I had a lot of issues with handling asynchronous actions with the reporter, this was why I chose to use the queue approach. I could not figure out how to get around this but this approach does work.
  2. Your TestRail actions must return a promise

It is important to understand the execution order of the hooks in order to understand the solution.

--- beforeLaunch           
    --- onPrepare          
      --- jasmineStarted   (set in jasmine reporter)
        --- beforeAll
         --- suiteStarted  (set in jasmine reporter)
          --- specStarted  (set in jasmine reporter)
           --- beforeEach  
           +++ afterEach   
          +++ specDone     (set in jasmine reporter)
         +++ suiteDone     (set in jasmine reporter)
        +++ afterAll
      +++ jasmineDone      (set in jasmine reporter)
    +++ onComplete         
+++ afterLaunch

Upvotes: 2

Related Questions