http.request 实习生功能测试
http.request in intern functional test
我正在尝试 post 到 url 以获取用户名和密码,以便我可以登录我的网站进行功能测试。我启动 selenium 服务器和 运行 这个基本测试并请求 posting 服务。
define([
'intern!object',
'runtime/testConfig',
'intern/dojo/node!nconf',
'intern/dojo/node!http'
], function(registerSuite, conf, nconf, http) {
var tests = {
name: 'Login test',
'Test': function() {
return http.request({
host: 'posttestserver.com',
path: '/post.php',
json: true,
method: 'POST',
body: {
"userName": "sgfi98j",
"password": "sgfi98j",
"userEmail": "sgfi98j@it.com",
"sourceCode": "TEST",
"region": "US"
},
headers: {
'Content-Type': 'application/json'
}
}, function(response) {
// Continuously update stream with data
var body = '';
console.log("getting data");
response.on('data', function(d) {
body += d;
console.log(".");
});
response.on('end', function() {
console.log("done");
console.log(body);
});
response.on('error', function(e) {
console.log("ERROR :( ");
console.log(e.message);
});
});
}
};
registerSuite(tests);
});
我已经尝试了很多版本,要么没有错误,要么
Warning: FATAL ERROR
Error: [POST http://localhost:4444/wd/hub/session] connect ECONNREFUSED
Error: connect ECONNREFUSED
at exports._errnoException <util.js:746:11>
at TCPConnectWrap.afterConnect [as oncomplete] <net.js:1000:19> Use --force to continue.
我的 selenium/intern 配置中是否遗漏了什么?
ECONNREFUSED 错误表明在指定的目标上没有 Selenium 服务器 运行。
你的代码永远不会工作,因为 http.request
不是 return Promise,它 return 是 ClientRequest object. If you are performing an asynchronous operation you need to return a Promise or use this.async
. See the asynchronous test documentation 描述了这两个选项更详细。
我从来没有真正解决过使用 http 模块的错误,因为我确实有 selenium 运行 但它仍然给我错误。但我尝试使用 dojo/request 因为它 returns 承诺并且它立即起作用。
define([
"intern!object",
"runtime/testConfig",
"intern/dojo/node!nconf",
"intern/dojo/request",
"intern/chai!expect"
], function(registerSuite, conf, nconf, request, expect) {
var tests = {
name: "Login test",
"Test": function() {
request.post("http://requestb.in/qlnoyyql", {
data: JSON.stringify({
userName: "gz4nio4",
password: "sdfgsdfgsdgf4",
userEmail: "gzenio4@wi.com",
billingCode: "abc-123",
sourceCode: "TEST",
companyName: "gwzanio4",
region: "US",
partner: "NONE"
}),
headers: { 'Content-Type': 'application/json' }
}).then(function(response) {
console.log(response);
}, function(err) {
console.log(err);
}, function(evt) {
console.log(evt);
});
}
};
registerSuite(tests);
});
我正在尝试 post 到 url 以获取用户名和密码,以便我可以登录我的网站进行功能测试。我启动 selenium 服务器和 运行 这个基本测试并请求 posting 服务。
define([
'intern!object',
'runtime/testConfig',
'intern/dojo/node!nconf',
'intern/dojo/node!http'
], function(registerSuite, conf, nconf, http) {
var tests = {
name: 'Login test',
'Test': function() {
return http.request({
host: 'posttestserver.com',
path: '/post.php',
json: true,
method: 'POST',
body: {
"userName": "sgfi98j",
"password": "sgfi98j",
"userEmail": "sgfi98j@it.com",
"sourceCode": "TEST",
"region": "US"
},
headers: {
'Content-Type': 'application/json'
}
}, function(response) {
// Continuously update stream with data
var body = '';
console.log("getting data");
response.on('data', function(d) {
body += d;
console.log(".");
});
response.on('end', function() {
console.log("done");
console.log(body);
});
response.on('error', function(e) {
console.log("ERROR :( ");
console.log(e.message);
});
});
}
};
registerSuite(tests);
});
我已经尝试了很多版本,要么没有错误,要么
Warning: FATAL ERROR
Error: [POST http://localhost:4444/wd/hub/session] connect ECONNREFUSED
Error: connect ECONNREFUSED
at exports._errnoException <util.js:746:11>
at TCPConnectWrap.afterConnect [as oncomplete] <net.js:1000:19> Use --force to continue.
我的 selenium/intern 配置中是否遗漏了什么?
ECONNREFUSED 错误表明在指定的目标上没有 Selenium 服务器 运行。
你的代码永远不会工作,因为
http.request
不是 return Promise,它 return 是 ClientRequest object. If you are performing an asynchronous operation you need to return a Promise or usethis.async
. See the asynchronous test documentation 描述了这两个选项更详细。
我从来没有真正解决过使用 http 模块的错误,因为我确实有 selenium 运行 但它仍然给我错误。但我尝试使用 dojo/request 因为它 returns 承诺并且它立即起作用。
define([
"intern!object",
"runtime/testConfig",
"intern/dojo/node!nconf",
"intern/dojo/request",
"intern/chai!expect"
], function(registerSuite, conf, nconf, request, expect) {
var tests = {
name: "Login test",
"Test": function() {
request.post("http://requestb.in/qlnoyyql", {
data: JSON.stringify({
userName: "gz4nio4",
password: "sdfgsdfgsdgf4",
userEmail: "gzenio4@wi.com",
billingCode: "abc-123",
sourceCode: "TEST",
companyName: "gwzanio4",
region: "US",
partner: "NONE"
}),
headers: { 'Content-Type': 'application/json' }
}).then(function(response) {
console.log(response);
}, function(err) {
console.log(err);
}, function(evt) {
console.log(evt);
});
}
};
registerSuite(tests);
});