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

How to get nightwatch test_workers work with browserstack-local

我正在尝试让 Nightwatch 的内置并行 test_workers 与 browserstack-local 一起工作以进行本地 url 测试。

当 运行 在没有 browserstack local 的情况下进行测试时,Nightwatch test_workers 似乎工作正常。对于未启用 test_workers 的 运行 本地测试也是如此。

我尝试了此处找到的示例 https://github.com/browserstack/nightwatch-browserstack,但其中 none 将 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

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

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;

这里的问题是由于在 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

中的 "suite" 跑步者