断言后带Chromedriver的Nightwatch不会退出

时间:2018-08-21 13:34:27

标签: javascript selenium-chromedriver nightwatch.js selenium-server

我遇到一个问题,即所有声明都通过后Nightwatch无法退出。尽管chrome驱动程序已关闭,但执行似乎停滞不前,并且永远不会退出。

版本如下:

chromedriver (headless): 2.41.0
selenium-server: 3.14.0
nightwatch: 1.0.9

firefox驱动程序正常工作。

Runner.js的内容如下:

// 1. start the dev server using production config
process.env.NODE_ENV = 'testing';

const webpack = require('webpack');
const DevServer = require('webpack-dev-server');

const webpackConfig = require('../../build/webpack.prod.conf');
const devConfigPromise = require('../../build/webpack.dev.conf');

let server;

devConfigPromise.then((devConfig) => {
  const devServerOptions = devConfig.devServer;
  const compiler = webpack(webpackConfig);
  server = new DevServer(compiler, devServerOptions);
  const port = devServerOptions.port;
  const host = devServerOptions.host;
  return server.listen(port, host);
})
  .then(() => {
  // 2. run the nightwatch test suite against it
  // to run in additional browsers:
  //    1. add an entry in test/e2e/nightwatch.conf.js under "test_settings"
  //    2. add it to the --env flag below
  // or override the environment flag, for example: `npm run e2e -- --env chrome,firefox`
  // For more information on Nightwatch's config file, see
  // http://nightwatchjs.org/guide#settings-file
    let opts = process.argv.slice(2);
    if (opts.indexOf('--config') === -1) {
      opts = opts.concat(['--config', 'test/e2e/nightwatch.conf.js']);
    }
    if (opts.indexOf('--env') === -1) {
      opts = opts.concat(['--env', 'chrome']);
    }

    const spawn = require('cross-spawn');
    const runner = spawn('./node_modules/.bin/nightwatch', opts, { stdio: 'inherit' });

    runner.on('exit', (code) => {
      server.close();
      process.exit(code);
    });

    runner.on('error', (err) => {
      console.log(`About to exit with code: ${err}`);
      server.close();
      throw err;
    });
  });

nightwatch.conf.js的内容如下:

require('babel-register');
const config = require('../../config');

// http://nightwatchjs.org/gettingstarted#settings-file
module.exports = {
  src_folders: ['test/e2e/specs'],
  output_folder: 'test/e2e/reports',
  custom_assertions_path: ['test/e2e/custom-assertions'],

  selenium: {
    start_process: true,
    server_path: require('selenium-server').path,
    host: '127.0.0.1',
    port: 4444,
    cli_args: {
      'webdrive.gecko.driver': require('geckodriver').path,
      'webdriver.chrome.driver': require('chromedriver').path,
    },
  },
  test_settings: {
    default: {
      selenium_port: 5555,
      selenium_host: 'localhost',
      silent: true,
      globals: {
        devServerURL: `http://localhost:${process.env.PORT || config.dev.port}`,
        serverUrl: '[server url]',
      },
    },

    chrome: {
      desiredCapabilities: {
        browserName: 'chrome',
        javascriptEnabled: true,
        acceptSslCerts: true,
        chromeOptions: {
          args: [
            '--headless', '--no-sandbox',
          ],
        },
      },
    },

    firefox: {
      desiredCapabilities: {
        browserName: 'firefox',
        javascriptEnabled: true,
        acceptSslCerts: true,
        'moz:firefoxOptions': {
          args: [
            '-headless',
          ],
        },
      },
    },
  },
};

任何帮助都将不胜感激,因为到目前为止我还没有找到解决方案。

1 个答案:

答案 0 :(得分:1)

首先,请确保在每次测试的最后一步都调用browser.end()。

第二,Nightwatch 1.0+仍处于测试阶段。我会尝试0.9.21版,这是大多数人仍在使用的版本。如果0.9.21可以运行,请针对1.09版提交错误。