测试失败时,量角器/ Jasmine发送REST调用

时间:2017-01-30 16:39:18

标签: rest http tfs jasmine protractor

我正在使用Protractor和Jasmine来测试我的混合移动应用程序,它运行正常。当测试失败时,我想在我的Team Foundation Server(TFS)上创建一个事件。因此,我必须向Api发送一个REST-Call,它在我的Angular App中也能正常工作。但是当我进入我的测试环境时,它不起作用。

我的代码:

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

所以问题是,我唯一的控制台输出是(在console.log(mappedResult)之后):E / launcher - BUG:启动器退出并剩下1个任务

所以我不知道,为什么这不起作用。

任何帮助表示赞赏。

修改

量角器:5.0.0 Appium Desktop Client:1.4.16.1 Chromedriver:2.27 Windows 10 64位 茉莉花:2.4.1

1 个答案:

答案 0 :(得分:0)

我终于解决了我的问题。这个问题是由于无视茉莉花的承诺造成的。我必须在.controllFlow().wait()

中添加protractor.promise

以下代码可以正常使用:

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

});

注意

当测试套件完成并且此时未解决最后一个承诺时,不会创建最后一个事件。我会尝试通过在上一个测试中添加browser.sleep(5000);来解决这个问题,以便create(incident)函数有更多时间完成。

感谢StackOverflow这个帮助我的答案。