admin管理员组

文章数量:1355697

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(panyId, 'cilents');
    dbFixture.deleteCollectionItemById(customerId, 'users');
    dbFixture.deleteCollectionItemById(assetId, 'assets');
    dbFixture.deleteFile(imageId);
    browser.end();
};

var loginTest = function(browser) {
    dbFixture.createCompany(function(pany) {
        dbFixture.createCustomer(pany._id, function(customer, assetid, imageid) {
            panyId = pany._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!

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(panyId, 'cilents');
    dbFixture.deleteCollectionItemById(customerId, 'users');
    dbFixture.deleteCollectionItemById(assetId, 'assets');
    dbFixture.deleteFile(imageId);
    browser.end();
};

var loginTest = function(browser) {
    dbFixture.createCompany(function(pany) {
        dbFixture.createCustomer(pany._id, function(customer, assetid, imageid) {
            panyId = pany._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!

Share Improve this question asked Oct 29, 2015 at 9:01 CooperisduhaceCooperisduhace 1352 silver badges17 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 7

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

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.

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

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.

本文标签: javascriptnightwatchjs pausing at the end of test suiteStack Overflow