0

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?

ssharma
  • 935
  • 2
  • 19
  • 43

2 Answers2

1

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)
 });            
};
  • HI @Nicholas Ackerman , I am using above function as you suggested in code but the issue is `request.post(CreateTenantUrl).set('Content-Type', 'application/json').set('serviceSharedSecret', 'sharedsecret').send(data).end(function(err,res)` takes time to respond with status 200 hence `Commons.CreateUsers('"userName": "test1", "tenantKey": "1", "password": "Test1", "userID": "1"')` call is never being executed. Please suggest how can i put wait or is there anything else I can do. – ssharma Jun 29 '16 at 18:37
0

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

Community
  • 1
  • 1
findlayc
  • 136
  • 5