KMC
KMC

Reputation: 1742

Get request async call in protractor

I have a test case where my code make request to server and check if the filename with specified date exists. Since GET request is async call, how can I make sure that I have the filename from the server before I check if it is a specified date?

Here's excerpt of my code :

var re = new RInterface();
it('data show exists', function() {   
    target.each(function(ele){
        browser.actions().mouseMove(ele).perform();     

        re.get(function(result){                
            expect(result).toEqual(true);   
        });
    });
});

RInterface.js

 var Service = function() {
 var serv = this;
 var uname = atob(settings.username);
 var pwd = atob(settings.password);
 var url = 'https://' + uname + ':' + pwd + '@' + settings.Url + '/' + settings.format + '/' + settings.period;
 var completeURL = url;     
 var today = DateString();

 serv.get = function(callback) {
 var dataStrAry = [];
 var count = 0;

 request(completeURL, function (error, response, body) {
            if (!error && response.statusCode == 200) {
                var serverData = JSON.parse(body);                  

                var split = serverData[serverData.length-1].Name.split(" "); 
                var target = split[split.length-1].split(".")[0]; 

                // Check if the file with current date is available 
                // If it is, then assume data is saved on the server
                if(target == today) {
                    console.log("equal");
                    callback(true);                     
                }
                else {
                    console.log("not equal");
                    callback(false);
                }

            }
            else {
                console.log("errror call");
                callback(false);
                return;

            }
        });

    };

So, re.get is where I make GET request to the server and I passed callback function to be called at the end of get request. The problem, I think is protractor complete executing the test before my code gets data from the server. How do I force protractor to wait so that I can check the returned data? My current workaround is put the get request inside beforeEach and seems that protractor forces test to wait for it finish executing.

Upvotes: 2

Views: 530

Answers (2)

Martin Blaustein
Martin Blaustein

Reputation: 1163

You can make it easier by using browser.wait, just wrap the request function (which returns a promise) like

browser.wait(request...).then(result => { 
// here continue execution with the result of the request
 }

This will make the browser wait for your promise.

Upvotes: 2

Florent B.
Florent B.

Reputation: 42518

You need to handle the result of your request with a Promise if you want the control flow to wait for it:

var re = new RInterface();
it('data show exists', function() {   
    target.each(function(ele){
        browser.actions().mouseMove(ele).perform();     

        expect(re.get()).toEqual(true);
    });
});
var Service = function() {
    ...
    this.get = function() {
        var defer = protractor.promise.defer();
        ...
        request(completeURL, function (error, response, body) {
            if (!error && response.statusCode == 200) {
                var result = ...
                defer.fulfill(result);
            } else {
                defer.reject(error);
            }
        });

        return defer.promise;
    };
};

Upvotes: 4

Related Questions