我正在进行ETE测试,需要执行Http请求,然后一旦有了“令牌”,我便有了另一个函数“构建”将用于测试的URL。
“令牌”部分出现问题,因为它没有等待响应。以下是功能和我的ETE测试。
//从ERP服务器获取令牌。从传递的凭据中返回令牌字符串。 fetchToken:
function(username, password) {
var request = require('request');
var credentials = 'Basic ' + (new Buffer(username + ':' + password).toString('base64'));
var options = {
method: 'POST',
url: browser.params.ERPTokenUrl,
rejectUnauthorized: false,
headers: {'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': credentials,
'Accept': 'application/json'
},
};
var xmlDoc = '';
function callback(error, response, body) {
console.log('Step 1: ' + body);
if (!error && response.statusCode === 200) {
console.log('Step 1.1: ' + body);
xmlDoc = JSON.parse(body);
} else {
console.log(error);
console.log(response.statusCode);
}
}
console.log('Step 2: ' + xmlDoc);
request(options, callback);
// This is the token
var token = xmlDoc.AccessToken;
console.log('Step 3: ' + token);
return xmlDoc;
},
// Build the embedded URL prior testing.
buildEmbeddedUrl: function(viewId, token, company) {
return (browser.params.urls.embeddedView + viewId +
'?auth=TOKEN&token=' + token + '&company=' + company);
}
it('should open an embedded view', function() {
// GET Token
var token = embeddedPO.fetchToken(browser.params.login.user, browser.params.login.password);
var url = embeddedPO.buildEmbeddedUrl(browser.params.discoveryViews.Embedded.jobEstimatedActualCost,
token, browser.params.Companies.EpicorEducation);
browser.get(url);
});
我的结果显示:
Test name:[ETE Test: Embeded - should open an embedded view]
Step 2:
Step 3: undefined
(node:3228) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
Step 1: {"AccessToken":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIxNTM1Mzg0ODU5IiwiaWF0IjoiMTUzNTM4MTI1OSIsImlzcyI6ImVwaWNvciIsImF1ZCI6ImVwaWNvciIsInVzZXJuYW1lIjoiTWFuYWdlciJ9.dNm4xG0p30CPJXk88KsEMtYNjliv5LtG2UUvyorVFE8","ExpiresIn":3600,"RefreshToken":"","TokenType":"Bearer"}
Step 1.1: {"AccessToken":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIxNTM1Mzg0ODU5IiwiaWF0IjoiMTUzNTM4MTI1OSIsImlzcyI6ImVwaWNvciIsImF1ZCI6ImVwaWNvciIsInVzZXJuYW1lIjoiTWFuYWdlciJ9.dNm4xG0p30CPJXk88KsEMtYNjliv5LtG2UUvyorVFE8","ExpiresIn":3600,"RefreshToken":"","TokenType":"Bearer"}
.
1 spec, 0 failures
如何添加等待响应,以便在令牌函数上返回该值?