admin管理员组文章数量:1405393
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 execute "bare" HTTP calls from Protractor tests?
One way that I found is using Node Http module, but it's a bit unwieldy. I wonder how such problems are typically solved - does Protractor expose anything? Is using Http (and other Node modules when you need them) the way to go? Is there some other way?
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 execute "bare" HTTP calls from Protractor tests?
One way that I found is using Node Http module, but it's a bit unwieldy. I wonder how such problems are typically solved - does Protractor expose anything? Is using Http (and other Node modules when you need them) the way to go? Is there some other way?
Share Improve this question asked Jan 10, 2014 at 22:54 Konrad GarusKonrad Garus 54.1k44 gold badges162 silver badges233 bronze badges3 Answers
Reset to default 2If you have a service in your Angular app that you can call to create your testing objects there is a trick that I described here:
Accessing Angular inside Protractor Test
I gave a presentation about Protractor a few weeks ago. Here is an example of the technique:
https://github./andresdominguez/protractor-meetup/blob/master/test/e2e/member3-spec.js#L25 https://github./andresdominguez/protractor-meetup/blob/master/test/e2e/api-helper.js
You can also take a look at this post: http://eitanp461.blogspot./2014/01/advanced-protractor-features.html
You can inject a module with protractor and then call it.
An alternate way that doesn't depend on Angular is manually creating an XMLHttpRequest
inside of browser.executeAsyncScript
. This is especially helpful if you need to make a call as part of the test setup, prior to Angular loading or prior to navigating to a page at all.
See this example in the Protractor docs:
Example #3: Injecting a XMLHttpRequest and waiting for the result. In this example, the inject script is specified with a function literal. When using this format, the function is converted to a string for injection, so it should not reference any symbols not defined in the scope of the page under test.
driver.executeAsyncScript(function() {
var callback = arguments[arguments.length - 1];
var xhr = new XMLHttpRequest();
xhr.open("GET", "/resource/data.json", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
callback(xhr.responseText);
}
}
xhr.send('');
}).then(function(str) {
console.log(JSON.parse(str)['food']);
});
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
- protractor test script get the data from rest end point
- protractor test script get the data from web page
- protract test script validate this data against the data on web page
So how to make the HTTP call to rest end point,
use this documentation https://nodejs/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.',
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();
});
本文标签: javascriptBare HTTP calls from Protractor testsStack Overflow
版权声明:本文标题:javascript - Bare HTTP calls from Protractor tests - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744264033a2597859.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论