How to make HTTP GET + POST request in Protractor

I ran into the problem of sending an HTTP request request to Protractor. In fact, I need to check the data in the database after performing some actions in the user interface.

It will be very useful if I can do it with jQuery, but I can’t find a way to use jQuery inside Protractor.

Help is needed!

In fact, we tried to use the NODEJS library, as shown below, but we encounter 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 found that the problem is that Protractor does not wait for the HTTP request to complete and just jumps. We get "2 ---->" first in the console, and then "1 ---->".

+5
source share
6 answers

You cannot access jQuery (the side of the interface) from the protractor, because the Protractor test suite is just a NodeJS script (its side). That way you can use the NodeJS HTTP API (or another lib request).

Check this example: http://squirrel.pl/blog/2014/01/15/direct-server-http-calls-in-protractor/

+3
source

I also use the http module (for different purposes, to reinitialize the database).

To make the protractor wait for the request to finish, 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 do not like to transfer all data processing to the response.on ('end') callback, then make the callback a separate function.

In addition, I must say that Protractor is not intended to directly control the database. This is for end-to-end testing. You better create a complex script that writes some data to one of the pages, go to another page and expect the data to be updated.

+2
source

This worked for me:

 var request = require('request'); var options = { method: 'POST', url: 'http://testurl.com/endpoint/test/', headers: {'id': 'ABCD', 'sessionid': 'dummyId', 'Accept': 'application/json', 'Accept-Language': 'en-us' }, body: '{ "pay_load": [] }' }; function callback(error, response, body) { if (!error && response.statusCode == 200) { var info = JSON.parse(body); console.log(body); console.log(info); } } request(options, callback); 
+2
source

I think this is the wrong answer you are looking for. You did not mention this name db.

I use http://frisbyjs.com/ to get documents from couchdb (database document) in my protractor project, and it performs my task very well.

0
source

I posted the same question on the Protractor git page, but they could not answer my question; so i found an alternative for this. I broke my test case into 2 tests. In the 1st I just pressed a button, and in the second I was able to get json and perform a check on it.

0
source

How about this?

http://eitanp461.blogspot.com.ar/2014/01/advanced-protractor-features.html

This post suggests reusing the existing AngularJS module to insert data into the database using the addMockModule function.

This can be changed to introduce the new AngularJS trendy chart as a layout with all the features you want, for example, checking material, data for data entry, etc. Since this is an angular module / service, it must use the AngularJS HTTP module, so Protractor will know how to handle it.

Disadvantages? To do this, you probably need the AngularJS context to run, I don’t know if it will work in non-angular applications (or its sections, for example, as an unauthorized login).

Thoughts?

-1
source

All Articles