我正在尝试使用单独的模块进行异步实习测试以执行请求调用。测试完成后,我有一个问题返回true,因为即使请求成功,我总是会收到超时错误,并且测试运行完成。测试运行后,它只是位于最后一页并超时。 login_test.js是测试文件,companyCreate是存在于外部模块中的请求调用文件。如果我将它传递给deferred.callback(),我不太确定我的测试返回调用发生了什么。
// login_test.js
define([
'intern!object',
'pages/loginpage',
'runtime/testConfig',
'intern/dojo/node!nconf',
'helpers/companyCreate',
'locators/loginpage',
'locators/companyselectionpage'
], function(registerSuite, LoginPage, conf, nconf, Company) {
var tests = {
name: 'Login test',
'Test': function() {
/* make a call to create a company
* param1: test function to run after we get response with login details
* param2: intern object so we can make it async
*/
Company.createCompany(function(response, testObj) {
testObj.timeout = 120000; //The default timeout is 30 seconds. Not enough
var region = nconf.get("region"); //Getting command line region value
var regionData = conf.get(region); //Fetching config data based on region
var loginId = regionData.LOGIN;
var password = regionData.PASSWORD;
var loginPage = new LoginPage(testObj.remote, regionData.DEFAULT_TIMEOUT);
var companySelectionPage = loginPage
.load(regionData.BASE_URL)
.loginIn(loginId, password);
var homePage = companySelectionPage
.doesCurrentURLContain('/companysel')
.isTitlePresent()
.selectCompany(CompanySelectionLocators.data);
return homePage
.doesCurrentURLContain('/homepage')
.getAccumulatedState();
}, this);
}
};
registerSuite(tests);
});
>
// companyCreate.js
define(function(require) {
var request = require('intern/dojo/request');
var Company = {
createCompany: function(callbackArg, testObj) {
// tell intern this is async
var deferred = testObj.async(120000);
// make post
request.post('https://internal.com/createcompany', {
query: {
version: ".0.1",
special: "true"
},
data: JSON.stringify({
userName: "Test",
password: "pass",
userEmail: "email@hi.com"
}),
headers: {
'Content-Type': "application/json"
}
}).then(function(response) {
// success, tell intern async is done, return test function to run and pass it the response
console.log(response);
return deferred.callback(callbackArg(response, testObj));
}, function(err) {
console.log(err);
}, function(evt) {
//console.log(evt);
});
}
};
return Company;
});
答案 0 :(得分:3)
deferred.callback
旨在用于包装另一个时间执行的另一个回调。它不解析底层的Promise,它返回一个新函数,当调用时,如果传入的回调函数没有抛出错误,则解析Promise。例如:
'Test': function () {
var dfd = this.async();
// this use of `dfd.callback`…
fs.readFile('foo.txt', dfd.callback(function (error, data) {
if (error) {
throw error;
}
assert.strictEqual(data, 'hello, world');
}));
// …is equivalent to this without it:
fs.readFile('foo.txt', function (error, data) {
if (error) {
dfd.reject(error);
return;
}
try {
assert.strictEqual(data, 'hello, world');
}
catch (error) {
dfd.reject(error);
return;
}
dfd.resolve();
}));
}
您应该使用deferred.resolve
,它解析了作为第一个参数传递的值的承诺。有关这些功能的更多详细信息,请参阅async tests documentation。
答案 1 :(得分:0)
工作解决方案如下。我不需要this.async。 comapnyCreate.js返回请求中的响应和承诺。 login_test.js在履行承诺后运行测试。 (仍然需要一些错误处理逻辑)
// login_test.js
define([
'intern!object',
'pages/loginpage',
'runtime/testConfig',
'intern/dojo/node!nconf',
'helpers/companyCreate',
'locators/loginpage',
'locators/companyselectionpage'
], function(registerSuite, LoginPage, conf, nconf, Company) {
var tests = {
name: 'Login test',
'Test': function() {
this.timeout = 60000;
var remote = this.remote;
return Company.createCompany().then(function(response) {
var region = nconf.get("region"); //Getting command line region value
var regionData = conf.get(region); //Fetching config data based on region
var loginId = regionData.LOGIN;
var password = regionData.PASSWORD;
var loginPage = new LoginPage(remote, regionData.DEFAULT_TIMEOUT);
var companySelectionPage = loginPage
.load(regionData.BASE_URL)
.loginIn(loginId, password);
var homePage = companySelectionPage
.doesCurrentURLContain('/companysel')
.isTitlePresent()
.selectCompany(CompanySelectionLocators.data);
return homePage
.doesCurrentURLContain('/homepage')
.getAccumulatedState();
});
}
};
registerSuite(tests);
});
// companyCreate.js
define(function(require) {
var request = require('intern/dojo/request');
var Company = {
createCompany: function() {
// make post
return request.post('https://internal.com/createcompany', {
query: {
version: ".0.1",
special: "true"
},
data: JSON.stringify({
userName: "Test",
password: "pass",
userEmail: "email@hi.com"
}),
headers: {
'Content-Type': "application/json"
}
});
}
};
return Company;
});