Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make HTTP GET+POST request in Protractor

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---->.

like image 849
Jatin Seth Avatar asked May 19 '15 07:05

Jatin Seth


2 Answers

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.

like image 105
Igor Shubovych Avatar answered Sep 28 '22 06:09

Igor Shubovych


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/

like image 29
Nicolas Pennec Avatar answered Sep 28 '22 07:09

Nicolas Pennec