For my API testing, my code behaving in different ways.I am using jasmine framework with node js.My GET request will give 2 responses
- Success json, with 200 status code. Response time is 400-500 ms
- Failure json, with 200 status code (valid failure), Response time is more than 10000 ms
In the 1st case the code for my test cases is below
describe('Verification of BS_004_addressCheck',()=>{
it('Verify success response for BS_004_addressCheck',function(done){
var path=require('path');
let endpoint=require(path.resolve('./config/endpoint_BS_004.json'));
// let pincodes=require(path.resolve('./config/pincodes.json'));
//let request=require(path.resolve('./config/postPin.json'));
//console.log(request.id,request.Name);
const fetch=require('node-fetch');
let baseUrl=endpoint.url;
let apikey=endpoint.apikey;
let country=endpoint.country;
let postcode=endpoint.postcode;
let picklistcount=endpoint.picklistcount;
let uniqueAddressReference=endpoint.uniqueAddressReference;
let fullUrlWithQueryParameters= baseUrl + "?apikey=" + apikey + "&country=" + country + "&postcode=" + postcode + "&picklistcount=" + picklistcount + "&uniqueAddressReference=" + uniqueAddressReference
//let pinCode=pincodes.Vlcy;
console.log(fullUrlWithQueryParameters);
console.log("test");
getR(fullUrlWithQueryParameters)
.then(jsonRes=>{
console.log(jsonRes);
expect(jsonRes).not.toBeUndefined();
expect(jsonRes.Header.ActivityStatusEnum).toBe('SUCCESS');
})
.then(done)
})
})
The code in my second case is same as the above. But in my first case, I get the spec as passes and in my second case I get the below failure
1) Verification of BS_004_addressCheck Verify success response for BS_004_addressCheck
Message:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
Stack:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
at <Jasmine>
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
1 spec, 1 failure
Finished in 5.012 seconds
Randomized with seed 21049 (jasmine --random=true --seed=21049)
My success response will take nearly 400 ms but my failure response will take nearly 10000 ms. So I guessed and added jasmine timeout as below (see above it block)
describe('Verification of BS_004_addressCheck',()=>{
beforeAll(function(done) {
jasmine.DEFAULT_TIMEOUT_INTERVAL= 10000;
});
//jasmine.DEFAULT_TIMEOUT_INTERVAL= 10000;
it('Verify success response for BS_004_addressCheck',function(done){
var path=require('path');
let endpoint=require(path.resolve('./config/endpoint_BS_004.json'));
// let pincodes=require(path.resolve('./config/pincodes.json'));
//let request=require(path.resolve('./config/postPin.json'));
//console.log(request.id,request.Name);
const fetch=require('node-fetch');
let baseUrl=endpoint.url;
let apikey=endpoint.apikey;
let country=endpoint.country;
let postcode=endpoint.postcode;
let picklistcount=endpoint.picklistcount;
let uniqueAddressReference=endpoint.uniqueAddressReference;
let fullUrlWithQueryParameters= baseUrl + "?apikey=" + apikey + "&country=" + country + "&postcode=" + postcode + "&picklistcount=" + picklistcount + "&uniqueAddressReference=" + uniqueAddressReference
//let pinCode=pincodes.Vlcy;
console.log(fullUrlWithQueryParameters);
console.log("test");
getR(fullUrlWithQueryParameters)
.then(jsonRes=>{
console.log(jsonRes);
expect(jsonRes).not.toBeUndefined();
expect(jsonRes.Header.ActivityStatusEnum).toBe('SUCCESS');
})
.then(done)
})
})
But now I got 2 errors..
Failures:
1) Verification of BS_004_addressCheck Verify success response for BS_004_addressCheck
Message:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
Stack:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
at <Jasmine>
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
Suite error: Verification of BS_004_addressCheck
Message:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
Stack:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
at <Jasmine>
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
1 spec, 2 failures
Finished in 15.015 seconds
Randomized with seed 21999 (jasmine --random=true --seed=21999).
I am not sure how to fix this. Please some one help me.The function getR which I call is below
let getR = (url) => {
console.log('test');
//console.log(url);
//console.log(id);
return fetch(url, {method: 'GET', agent: new HttpsProxyAgent('http://10.10.104.4:50683')})
.then(resRaw =>{
console.log(resRaw);
return resRaw.text();
})
.then(resJson=>{
console.log(resJson);
let res=JSON.parse(resJson);
//return res;
console.log(res.Header.ActivityStatusEnum);
return res;
})
//.then(validateResponse);
};
global.get=get;
global.getR=getR;
I think the issue is because of long response time. But don't know how to fix this. Please advise.