Intern Async Test

980 views Asked by At

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;
});
2

There are 2 answers

0
mziemer On BEST ANSWER

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)

// 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 protected]"
            }),
            headers: {
                'Content-Type': "application/json"
            }
        });
    }
};

return Company;
});
0
C Snover On

deferred.callback is intended to be used to wrap another callback that is executed at another time. It doesn’t resolve the underlying Promise, it returns a new function that, when invoked, resolves the Promise if the passed in callback function doesn’t throw an error. For example:

'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();
  }));
}

You should be using deferred.resolve, which resolves the promise to the value passed as the first argument. See the async tests documentation for more detailed information on each of these functions.