I am trying to get an async intern test working using a separate module to do the request call. I am having an issue returning true once the test is done because I always get a timeout error, even though the request is successful, and the test runs to completion. After the test runs it just sits on the last page and times out. login_test.js is the test file, companyCreate is the request calling file that exists in an external module. I'm not quite sure what is happening to my test return call if I pass it into 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 protected]"
}),
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;
});
Working solution is below. I had no need for this.async. comapnyCreate.js returns the response and promise from the request. login_test.js runs the test after the promise is fulfilled. (still needs some error handling logic)