I am facing problem to send HTTP get request in Protractor. Actually, I need to check data in DB after perform some action in UI.
It will be very helpful if I will be able to do it using JQuery, but I am not able to find a way how to use JQuery inside Protractor.
Need Help !!
Actually, we did try to use the Node.js lib as shown below, but facing problems in it.
var http = require('http');
var json_data;
http.get('SiteUrl', function(response) {
var bodyString = '';
response.setEncoding('utf8');
response.on("data", function(chunk) {
bodyString += chunk;
});
response.on('end', function() {
json_data = bodyString;
console.log("1---->" + json_data);
});
}).on('error', function(e) {
console.log("There is an error in GET request");
});
console.log("2---->" + json_data);
After Debugging, we have found that the problem is Protractor is not waiting for HTTP request to be complete and just pass on. We are getting 2---->
first in console and then 1---->
.
I also use http module (for different purpose, for reinitialization of the database).
To make protractor wait for ending the request, use promises
var http = require('http');
var json_data;
http.get('SiteUrl', function(response) {
var bodyString = '';
response.setEncoding('utf8');
response.on("data", function(chunk) {
bodyString += chunk;
});
response.on('end', function() {
json_data = bodyString;
console.log("1---->"+json_data);
// All the processing and Angular code should be here
console.log("2---->"+json_data);
});
}).on('error', function(e) {
console.log("There is an error in GET request");
});
If you don't like putting all the data processing into response.on('end') callback, then make callback a separate function.
Additionally I have to say that Protractor is not intended to be used to check the database directly. It is for end-to-end testing. You should better build a complex scenario which writes some data on one of the page, go to another page and expects that data is updated.
You cannot access to jQuery (it's frontend side) from protractor, because a Protractor test suite is just a NodeJS script (it's backend side). So you can use the NodeJS HTTP API (or another request lib).
Check this example : http://squirrel.pl/blog/2014/01/15/direct-server-http-calls-in-protractor/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With