量角器:HTTP响应测试 [英] Protractor: HTTP Response Testing
问题描述
我正在使用 Protracotr 进行e2e测试.
I'm using Protracotr for e2e testing.
我想在量角器中测试来自HTTP的响应. 基本上:
I want to test response from HTTP in protractor. Basically:
- 我已经运行了一些NodeJS服务器.
- 我想向该服务器发送请求
- 接收一些JSON数据
- 解析这些数据
- 检查它们是否正确
我正在使用"http" NODEJS Lib进行http调用GET + POST.
I'm using "http" NODEJS Lib to make http calls GET+POST.
var http = require('http');
describe("Some test", function() {
function httpGet(siteUrl) {
http.get(siteUrl, function(response) {
response.setEncoding('utf8');
response.on("data", function(chunk) {
bodyString += chunk;
});
response.on('end', function() {
defer.fulfill({
bodyString: bodyString
});
});
}).on('error', function(e) {
defer.reject("Got http.get error: " + e.message);
});
return defer.promise;
}
it('Test case', function(){
httpGet("http://localhost:3333/path/1/10").then(function(result) {
var json_data = JSON.parse(result.bodyString);
for (var i = 0; i < json_data.length; ++i) {
console.log("label: " + json_data[i].label);
expect(json_data[i].label).toEqual('abc');
}
});
});
});
我可以在console.log中看到响应消息很好地解析,但是我无法测试任何东西,我的测试结果是:
I can see response message nice parsed in console.log, but I'm not able to test anything, my test results are:
Finished in 0.019 seconds
1 test, 0 assertions, 0 failures
label: Text1
label: Text2
[launcher] 0 instance(s) of WebDriver still running
[launcher] chrome #1 passed
Process finished with exit code 0
测试完成后,控制台日志将被记录下来,并且没有断言.
请提供帮助,如何在Protractor中测试来自服务器的响应(JSON格式)?
Any help please, how to test those responses (in JSON format) from server in Protractor?
推荐答案
对于异步测试,您需要将完成的回调传递给函数.然后,成功调用done()
或失败调用done.fail()
.参见 Jasmine的异步支持文档.
For async tests, you'll need to pass the done callback to your function. Then call done()
on success or done.fail()
on failure. See Jasmine's Asynchronous support documentation.
it('Test case', function(done){
httpGet("http://localhost:3333/path/1/10").then((result) => {
var json_data = JSON.parse(result.bodyString);
for (var i = 0; i < json_data.length; ++i) {
console.log("label: " + json_data[i].label);
}
done();
}).catch(err => {
done.fail();
});
});
这篇关于量角器:HTTP响应测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!