nightwatch.js在测试套件的末尾暂停

时间:2015-10-29 09:01:12

标签: javascript node.js selenium nightwatch.js

我一直在使用 nightwatch.js 进行功能测试自动化。问题是测试套件完成后测试暂停。它并没有结束这个过程。代码如下所示:

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

我还尝试在 done(); 中添加 afterSuite ,但仍然没有成功。提前谢谢!

4 个答案:

答案 0 :(得分:6)

一种方法是注册一个全局reporter函数,该函数在所有测试完成后运行并相应地退出该过程,即。如果测试失败或出错,exit 1,否则为exit 0

例如。 http://nightwatchjs.org/guide#external-globals

nightwatch.json配置中添加:

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

然后在./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);
        }
    }
};

答案 1 :(得分:1)

这个问题的根本原因是什么?

使用Josh的方法解决了这个问题,但后来我再也没有收到junit报告。

答案 2 :(得分:0)

在最新的global.js脚本中,

是一个小错误...属性名称为error s

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

答案 3 :(得分:0)

对Martin Oppitz的回答进行小调整; setTimeout是必要的,以便给网络驱动程序时间终止。否则,再次运行它将给出竞争条件。

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

这可以通过nightwatch --reporter test/reporter.js运行。