Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait for the response of http request from Protractor side

I used code from the answer https://stackoverflow.com/a/25149395/3330910.

I do next:

it('HTTP request', function () {
    var BackRequest = require('../helper/backRequest');
    var request = new BackRequest();

    page.visitPage();

    request.setBaseUrl('http://localhost:8081');

    // Step #1
    request.get('/api/v1/one')
        .then(function(result){
        expect(result.statusCode).toBe(100); // An error #1
        expect(result.bodyString).toContain('Some text');
    });

    // Step #2
    expect(1).toBe(2); // an error #2
});

And I get the errors in order:

  • Error #2
  • Error #1

How can I force the protractor to wait for Step #1 and then do the step #2.

For now only I can do is to chain then() functions:

request.get('/api/v1/one')
    .then(function(result){
        expect(result.statusCode).toBe(100); // An error #1
        expect(result.bodyString).toContain('Some text')
    .then(function(result){
        expect(1).toBe(2);
        }); 

Update

So, it ends up with the next approach:

describe('Scenarios', function () {

    beforeEach(function () {
        page.visitPage();
    });

    var chain = function () {
        var defer = protractor.promise.defer();
        defer.fulfill(true);
        return defer.promise;
    };

    it('HTTP request', function () {
        var BackRequest = require('../helper/backRequest');
        var request = new BackRequest();
        request.setBaseUrl('http://localhost:8081');

        chain()
            .then(function () {
                // Save data
            })

            .then(function () {
                request.get('/api/v1/one')
                    .then(function (result) {
                        expect(result.statusCode).toBe(200);
                        expect(result.bodyString).toContain('text');
                    });
            })

            .then(function () {
                // Change and Save again
            })

            .then(function () {
                request.get('/api/v1/one')
                    .then(function (result) {
                        expect(result.statusCode).toBe(200);
                        expect(result.bodyString).toContain('new text');
                        expect(result.bodyString).not.toContain('text');
                    });
            });
    });

});

Thanks Leo Gallucci for help.

like image 422
SO User Avatar asked Nov 05 '14 15:11

SO User


1 Answers

Step #2 gets resolved immediately because there is nothing to wait for, there are no webdriver promises there you're simply comparing absolute numbers with expect(1).toBe(2);

You can stick with chaining then() as you did or the way I prefer is separate it() blocks:

it('HTTP request', function () {
    // Step #1 code ...
});

it('keeps testing other things in this step #2', function () {
    expect(1).toBe(2);
});

BTW I'm glad you found useful my other answer!

like image 163
Leo Gallucci Avatar answered Nov 02 '22 14:11

Leo Gallucci