【问题标题】:How to get nightwatch test_workers work with browserstack-local如何让 nightwatch test_workers 与 browserstack-local 一起工作
【发布时间】:2018-06-18 14:32:45
【问题描述】:

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

在没有本地浏览器堆栈的情况下运行测试时,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

【问题讨论】:

    标签: nightwatch.js browserstack


    【解决方案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中的“套件”跑步者

    【讨论】:

      【解决方案2】:

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

      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;
      

      【讨论】:

      • 我试图在不使用 nightwatch test_workers 扩展到不同浏览器的情况下进行并行测试。当我们为多个浏览器环境做好准备时,我会牢记这一点。
      猜你喜欢
      • 2023-02-02
      • 2017-06-25
      • 2020-12-13
      • 2012-02-02
      • 2011-02-22
      • 2013-03-24
      • 2016-07-22
      • 2011-08-17
      • 2021-12-18
      相关资源
      最近更新 更多