如何让nightwatch test_workers与browserstack-local一起工作

时间:2018-06-18 14:32:46

标签: nightwatch.js browserstack

我正在努力让Nightwatch的内置并行test_workers与browserstack-local一起工作进行本地网址测试。

当没有本地浏览器运行测试时,Nightwatch test_workers似乎工作正常。在没有启用test_workers的情况下运行本地测试也是如此。

我已经尝试了https://github.com/browserstack/nightwatch-browserstack中的示例,但这些示例都没有将browserstack-local与Nightwatch test_workers结合使用。

使用test_workers在本地运行时,我得到以下输出。

Connecting local
Connected. Now testing...
Process terminated with code 0.

还有其他人遇到类似的问题吗?

编辑:我已经解决了这个问题并在下面发布了答案

我已经转储了下面的相关配置文件。

我的local.conf.js

nightwatch_config = {
    globals_path: 'globals.js',
    output_folder: false,
    src_folders: ['tests'],
    selenium: {
        'start_process': false,
        'host': 'hub-cloud.browserstack.com',
        'port': 80
    },
    test_workers: {
        "enabled": true,
        "workers":2
    },
    test_settings: {
        default: {
            desiredCapabilities: {
                'browserstack.user': process.env.BROWSERSTACK_USER,
                'browserstack.key': process.env.BROWSERSTACK_ACCESS_KEY,
                'browserstack.local': true,
                'browserstack.debug': false,
            }
        }
    }
};

// Code to copy seleniumhost/port into test settings
for (let i in nightwatch_config.test_settings) {
    if (nightwatch_config.test_settings.hasOwnProperty(i)) {
        let config = nightwatch_config.test_settings[i];
        config['selenium_host'] = nightwatch_config.selenium.host;
        config['selenium_port'] = nightwatch_config.selenium.port;
    }
}

module.exports = nightwatch_config;

local.runner.js

    #!/usr/bin/env node

var Nightwatch = require('nightwatch');
var browserstack = require('browserstack-local');
var bs_local;

process.env.BROWSERSTACK_ID = new Date().getTime();

try {
    process.mainModule.filename = "./node_modules/.bin/nightwatch";

    // Code to start browserstack local before start of test
    console.log("Connecting local");
    Nightwatch.bs_local = bs_local = new browserstack.Local();
    bs_local.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY }, function (error) {
        if (error) throw error;

        console.log('Connected. Now testing...');
        Nightwatch.cli(function (argv) {
            Nightwatch.CliRunner(argv)
                .setup(null, function () {
                    // Code to stop browserstack local after end of parallel test
                    bs_local.stop(function () { });
                })
                .runTests(function () {
                    // Code to stop browserstack local after end of single test
                    bs_local.stop(function () { });
                });
        });
    });
} catch (ex) {
    console.log('There was an error while starting the test runner:\n\n');
    process.stderr.write(ex.stack + '\n');
    process.exit(2);
}

和我的package.json脚本

node ./local.runner.js -c ./local.conf.js

2 个答案:

答案 0 :(得分:1)

这里的问题是由于在local.runner.js中错误地定义了模块文件名

process.mainModule.filename = "./node_modules/.bin/nightwatch";

应直接指向其目录中的Nightwatch文件。

process.mainModule.filename = "./node_modules/nightwatch/bin/nightwatch";

这些文件的不同以及此解决方案工作的确切原因超出了我的范围。

答案来自"套件"跑步者https://github.com/browserstack/nightwatch-browserstack

答案 1 :(得分:0)

您似乎没有指定浏览器。修改配置文件以使用以下配置文件内联:

var browserstack = require('browserstack-local');

nightwatch_config = {
  src_folders : [ "local" ],

  selenium : {
    "start_process" : false,
    "host" : "hub-cloud.browserstack.com",
    "port" : 80
  },
  test_workers: {
          "enabled": true,
          "workers":2
      },

  common_capabilities: {
    'browserstack.user': process.env.BROWSERSTACK_USERNAME || 'BROWSERSTACK_USERNAME',
    'browserstack.key': process.env.BROWSERSTACK_ACCESS_KEY || 'BROWSERSTACK_ACCESS_KEY',
    'browserstack.debug': true,
    'browserstack.local': true
  },

  test_settings: {
    default: {},
    chrome: {
      desiredCapabilities: {
        browser: "chrome"
      }
    },
    firefox: {
      desiredCapabilities: {
        browser: "firefox"
      }
    },
    safari: {
      desiredCapabilities: {
        browser: "safari"
      }
    }
  }
};

// Code to support common capabilites
for(var i in nightwatch_config.test_settings){
  var config = nightwatch_config.test_settings[i];
  config['selenium_host'] = nightwatch_config.selenium.host;
  config['selenium_port'] = nightwatch_config.selenium.port;
  config['desiredCapabilities'] = config['desiredCapabilities'] || {};
  for(var j in nightwatch_config.common_capabilities){
    config['desiredCapabilities'][j] = config['desiredCapabilities'][j] || nightwatch_config.common_capabilities[j];
  }
}

module.exports = nightwatch_config;