NapkinHD NapkinHD - 4 years ago 191
reST (reStructuredText) Question

Protractor/Jasmine send REST Call when a test failed

I am using Protractor and Jasmine to test my hybrid mobile app, which works fine. I'd like to create an incident on my Team Foundation Server (TFS), when a test fails. Therefore, I have to send an REST-Call to the Api, which also works fine in my Angular App. But it does not work, when I am inside my test environment.

My Code:

var BrowsePage = require('./browse.page');
var tfsIncident = require('./tfsIncident_service');
var request = require('request');

describe('Testing the browse state', function () {

var browsePage = new BrowsePage();
var specsArray = [];

var reporterCurrentSpec = {
specDone: function (result) {
if (result.status === 'failed') {
var mappedResult = tfsIncident.create(result);
console.log(mappedResult); //This works so far, but then it crashes
var options = {
method: 'PATCH', //THis Method requiered the API
url: 'MY_COOL_API_ENDPOINT',
headers: {
'Authorization': 'Basic ' + btoa('USERNAME' + ':' + 'PASSWORD'),
'Content-Type': 'application/json-patch+json'
},
body: mappedResult
};

function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log(response);
console.log(info);
}
}
request(options, callback);
}
}
};
jasmine.getEnv().addReporter(reporterCurrentSpec);

//This test passes
it('should be able to take display the heading', function () {
expect(browsePage.headline.isPresent()).toBe(true);
});

// Test is supposed to fail
it('should be able to fail', function () {
expect(browsePage.headline).toBe(1);
});

// Test is supposed to fail as well
it('should be able to fail too', function () {
expect(browsePage.headline).toBe(2);
});
});


So the problem is, that my only console output is (after the console.log(mappedResult)): E/launcher - BUG: launcher exited with 1 tasks remaining

So I have no idea, why this does not work.

Any help appreciated.

Edit



Protractor: 5.0.0
Appium Desktop Client: 1.4.16.1
Chromedriver: 2.27
Windows 10 64 Bit
Jasmine: 2.4.1

Answer Source

I finally got my problem solved. The problem was caused by ignoring the promises by jasmine. I had to add a .controllFlow().wait() to my protractor.promise

The following code works fine:

var BrowsePage = require('./browse.page');

describe('Testing the browse state', function () {

    var browsePage = new BrowsePage();

    var reporterCurrentSpec = {
        specDone: function (result) {
            if (result.status === 'failed') {
                //Mapping of the result
                var incident = [
                    {
                        op: 'add',
                        path: '/fields/System.Title',
                        value: 'Test: ' + result.fullName + ' failed'
                    },
                    {
                        op: 'add',
                        path: '/fields/System.Description',
                        value: result.failedExpectations[0].message
                    },
                    {
                        op: 'add',
                        path: '/fields/Microsoft.VSTS.Common.Priority',
                        value: '1'
                    },
                    {
                        op: 'add',
                        path: '/fields/System.AssignedTo',
                        value: 'Name Lastname <e@mail.com>'
                    }
                ];
                protractor.promise.controlFlow().wait(create(incident)).then(function (done) { //The magic happens in this line
                    console.log("test done from specDone:" + done);
                });
            }
        }
    };
    jasmine.getEnv().addReporter(reporterCurrentSpec); //Add new Jasmine-Reporter

    function create(incident) {

        var request = require('request');
        var defer = protractor.promise.defer(); //new promise

        request({
            url: 'https://MY_COOL_ENDPOINT.COM',
            method: "PATCH",
            json: true,   // <--Very important!!!
            headers: {
                'Authorization': 'Basic ' + new Buffer('USERNAME' + ':' + 'PASSWORD').toString('base64'),
                'Content-Type': 'application/json-patch+json'
            },
            body: incident
        }, function (error, response, body) {
            console.log(error);
            console.log(response.statusCode);
            console.log(body.id); //Id of created incident on TFS
            defer.fulfill({
                statusCode: response.statusCode
            }); //resolve the promise
        });
        return defer.promise; //return promise here
    }

    it('should be able to display the heading', function () {

        expect(browsePage.headline.isPresent()).toBe(true);
    });

    it('should be able to fail', function () {

        expect(browsePage.headline.isPresent()).toBe(false);
    });

    it('should be able to fail 2', function () {

        expect(browsePage.headline.isPresent()).toBe(false);
    });

});

Attention

When the test suite is done and the last promise is not resolved at this moment, the last incident is not created. I'll try to work around by adding to the last test a browser.sleep(5000); so that the create(incident) function gets more time to finish.

Thanks to this StackOverflow answer for helping me.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download