My Protractor tests need some data setup which I would like to implement by making a series of POSTs and PUTs to the running server.
So, the question is: How do you
For making bare http calls from protractor you need to use the HTTP module from node js... here is the simple solution that we had used in the situation
So how to make the HTTP call to rest end point,
use this documentation https://nodejs.org/api/http.html#http_http_get_options_callback
and this is the code snippet
you need to have
var http=require('http');
it('MAKEHTTPCALL', function() {
var gotResponse=false;
var myResponse={};
//this function to wait the rest to respond back
function waitForBackend(){
browser.wait(function(){
//console.log(myResponse);
console.log(gotResponse);
return gotResponse;
}, 5000);
}
var options = {
hostname: 'yourhostname.com',
port: 8081,
path: '/yourendpoint/path/XXXX',
method: 'GET',
headers: {
'token':'XXXXXXXX'
}
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
gotResponse = true;
myResponse=JSON.parse(chunk);
/*
TO DO
Add the script validations here…..
*/
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.on('connect', function(res, socket, head) {
console.log('got connected!');
});
req.end();
waitForBackend();
});