【发布时间】:2017-05-18 23:27:51
【问题描述】:
我正在使用 Protracotr 进行 e2e 测试。
我想在量角器中测试来自 HTTP 的响应。 基本上:
- 我正在运行一些 NodeJS 服务器。
- 我想向这个服务器发送请求
- 接收一些 JSON 数据
- 解析这些数据
- 检查它们是否正确
我正在使用“http”NODEJS Lib 进行 http 调用 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 中看到很好解析的响应消息,但我无法测试任何东西,我的测试结果是:
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 格式)?
【问题讨论】:
标签: json node.js protractor httprequest httpresponse