ssharma
ssharma

Reputation: 941

How to wait for Rest API to respond in Protractor

I am using following function to call API in my Protractor test, and sometimes API takes time to respond.

var request = require( "superagent" );
var PostUrl = browser.baseUrl + 'rest/1.0/dev/users';
var CreateTenantUrl = browser.baseUrl + 'rest/1.0/tenant';

exports.CreateTenant = function(body){  
 var data = '{' + body + '}';       
 request.post(CreateTenantUrl).set('Content-Type', 'application/json').set('serviceSharedSecret', 'sharedsecret').send(data).end(function(err,res){
        if(err){
            console.log("CreateTenant post error=  ", err )
        } else{
            console.log("CreateTenant post response = ", res.status)
        }
        expect(res.status).toEqual(200)
     });     
};

exports.CreateUsers = function(body){
  var data = '{' +body + '}';
   request.post( PostUrl ).set('Content-Type', 'application/json').send(data).end(function(err,res){
 if(err){
        console.log("CreateUsers post error= ", err )
    } else{
        console.log("CreateUsers post response = ", res.status)
    }
expect(res.status).toEqual(202)
 });            
};

Call these functions in test script:

Common.CreateTenant('"tid": "1","long_name": "test tenant"');
Common.CreateUsers('"userName": "test1", "tenantKey": "1", "password": "Test1", "userID": "1"');

is there any way to put wait for each API call to complete and then execute the next one?

Upvotes: 0

Views: 1484

Answers (2)

findlayc
findlayc

Reputation: 146

In order to wait for the request to complete you need to wrap it in a promise. See How to make superagent return a promise for more information

Upvotes: 0

Nicholas Ackerman
Nicholas Ackerman

Reputation: 401

If you need your second API call to only fire after the first one completes, send the second API call in the callback method of the first API call.

'Waiting' for an API call to complete is tpyically bad practice. An example of what not to do is the following: send one API call wait 10 seconds, check if first call completed, if it has, send second api call, otherwise wait another 10 seconds and repeat the process.

Its almost always a better approach is to use callbacks where you are alerted when the API call completes.

For your example, you should do the following:

var request = require( "superagent" );
var PostUrl = browser.baseUrl + 'rest/1.0/dev/users';
var CreateTenantUrl = browser.baseUrl + 'rest/1.0/tenant';

exports.CreateTenant = function(body){  
 var data = '{' + body + '}';       
 request.post(CreateTenantUrl).set('Content-Type', 'application/json').set('serviceSharedSecret', 'sharedsecret').send(data).end(function(err,res){
        if(err){
            console.log("CreateTenant post error=  ", err )
        } else{
            console.log("CreateTenant post response = ", res.status)

            //Create user once tenant has been successfully created
            Commons.CreateUsers('"userName": "test1", "tenantKey": "1", "password": "Test1", "userID": "1"');


        }
        expect(res.status).toEqual(200)
     });     
};

exports.CreateUsers = function(body){
  var data = '{' +body + '}';
   request.post( PostUrl ).set('Content-Type', 'application/json').send(data).end(function(err,res){
 if(err){
        console.log("CreateUsers post error= ", err )
    } else{
        console.log("CreateUsers post response = ", res.status)
    }
expect(res.status).toEqual(202)
 });            
};

Upvotes: 1

Related Questions