可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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---->
.
回答1:
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/
回答2:
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.
回答3:
It 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);
回答4:
I think it is not the right answer which you are looking for. You have not mentioned that db name.
I am using http://frisbyjs.com/ to get documents from couchdb (document database) in my protractor project and it fulfill my purpose very well.
回答5:
I posted the same question on Protractor git issue page, but they were not able to reply my question; so, I found an alternative for this. I broke my test case into 2 test cases. In 1st, I just clicked the button and in 2nd I was able to fetch json and perform validation on it.
回答6:
What about something like this?
http://eitanp461.blogspot.com.ar/2014/01/advanced-protractor-features.html
This post proposes to reuse an already existing AngularJS module to insert data into the DB using addMockModule function.
This could be modified to inject a new AngularJS moudule as a mock with all the features you want, like checking stuff, inseeting data, etc.
As this is an angular module/service it should use the AngularJS http module thus Protractor will know how to handle it.
Disadvantages? Well this probably need an AngularJS context to run, don't know if it will work in non angular apps (or sections of it, like a non agular login).
Thoughts?