I'm facing a strange problem in my automated tests written using Protractor. We need to test a bunch of API endpoints that return JSON over HTTP, as opposed to actual websites so instead of relying on Protractor, my team decided to use Chakram.
I have a page object responsible for accessing the API:
const qs = require('querystring')
const chakram = require('chakram');
function MyApi() {
const domain = // read from a configuration file
this.readImportantBusinessData = (queryParams) => {
const serviceUrl = `${domain}/services/seriousBusiness.json`;
const queryString = qs.stringify(queryParams);
const fullUrl = `${serviceUrl}?${queryString}`;
return chakram.get(fullUrl).then((response) => {
return response;
});
};
};
module.exports = new MyApi();
then, in one of my specs, I call the readImportantBusinessData
function to check if it returns the expected data.
return MyApi.readImportantBusinessData(validParameters).then((response) => {
chakramExpect(response).to.have.status(HTTP_200_OK);
chakramExpect(response).to.have.json({
"foo" : "bar"
});
});
Depending on the enviornment where I run this code, the test passes or fails with an error message that basically means that no response has been received.
Failed: Cannot read property 'statusCode' of undefined
I can confirm that the server I'm hitting is running and I can get a correct response when using a web browser.
The rquest made in my tests succeeds when I use a shared server hosted in AWS and fails when I use a local server running in VirtualBox.
Why could Chakram not recieve a response at all?