Cooperisduhace
Cooperisduhace

Reputation: 135

nightwatch.js pausing at the end of test suite

I've been using nightwatch.js for functional test automation. The problem is that the test is pausing when the test suite is finished. It doesn't end the process. The code looks like this :

var afterSuite = function(browser) {
    dbFixture.deleteCollectionItemById(companyId, 'cilents');
    dbFixture.deleteCollectionItemById(customerId, 'users');
    dbFixture.deleteCollectionItemById(assetId, 'assets');
    dbFixture.deleteFile(imageId);
    browser.end();
};

var loginTest = function(browser) {
    dbFixture.createCompany(function(company) {
        dbFixture.createCustomer(company._id, function(customer, assetid, imageid) {
            companyId = company._id;
            customerId = customer._id;
            assetId = assetid;
            imageId = imageid;
            goTo.goTo(url.localhost_home + url.login, browser);
            login.loginAsAny(customer.email, browser);
            newCustomerLoginAssert.assertNewCustomerLogin(browser);
        });
    });
};

module.exports = {
    after: afterSuite,
    'As a Customer, I should be able to login to the system once my registration has been approved': loginTest
};

I also tried adding done(); in afterSuite but still no success. Thanks in advance!

Upvotes: 5

Views: 2531

Answers (4)

Sethicus
Sethicus

Reputation: 1

Small tweak over Martin Oppitz's answer; setTimeout is necessary to give webdriver time to terminate. Otherwise, running it again will give a race condition.

// test/reporter.js
const reporter = new HtmlReporter({
  reportsDirectory: 'test/reports',
});

module.exports = {
  write: function(results, options, done) {

    const hasFailure =
      (typeof results.failed !== 'undefined' && results.failed !== 0) ||
      (typeof results.errors !== 'undefined' && results.errors !== 0);

    const _forceExit = () => {
      done();
    // setTimeout is neccessary to give webdriver time to terminate.
      setTimeout(() => {
        if (hasFailure) {
          return process.exit(1);
        }
        return process.exit(0);
      }, 1000);
    };

    reporter.fn(results, _forceExit);
  },
};

This can be run via nightwatch --reporter test/reporter.js.

Upvotes: 0

Martin Oppitz
Martin Oppitz

Reputation: 11

in the latest global.js script, is a tiny bug ... the property name is errors.

module.exports = {
  /**
   * After all the tests are run, evaluate if there were errors and exit appropriately.
   *
   * If there were failures or errors, exit 1, else exit 0.
   *
   * @param results
   */
  reporter: function(results) {
    if (
      (typeof results.failed === 'undefined' || results.failed === 0) &&
      (typeof results.errors === 'undefined' || results.errors === 0)
    ) {
      process.exit(0);
    } else {
      process.exit(1);
    }
  }
};

Upvotes: 1

Peter Bollen
Peter Bollen

Reputation: 59

What was the root cause of this issue?

Using Josh's approach solves the issue, but then I'm not getting a junit report anymore.

Upvotes: 1

Josh Stuart
Josh Stuart

Reputation: 1528

An approach is to register a global reporter function that is run once all tests have finished and exits the process accordingly ie. if tests have failed or errored, exit 1, otherwise exit 0.

eg. http://nightwatchjs.org/guide#external-globals

In your nightwatch.json config add:

{
  "globals_path": "./config/global.js"
}

Then in ./config/global.js

module.exports = {
    /**
     * After all the tests are run, evaluate if there were errors and exit appropriately.
     *
     * If there were failures or errors, exit 1, else exit 0.
     *
     * @param results
     */
    reporter: function(results) {
        if ((typeof(results.failed) === 'undefined' || results.failed === 0) &&
        (typeof(results.error) === 'undefined' || results.error === 0)) {
            process.exit(0);
        } else {
            process.exit(1);
        }
    }
};

Upvotes: 7

Related Questions