Matt bodey
Matt bodey

Reputation: 33

How to get nightwatch test_workers work with browserstack-local

I am trying to get Nightwatch's inbuilt parallel test_workers working with browserstack-local for local url testing.

When running tests without browserstack local, Nightwatch test_workers seem to work just fine. The same is true for running local tests without test_workers enabled.

I have tried the examples found here https://github.com/browserstack/nightwatch-browserstack but none of these uses browserstack-local in combination with Nightwatch test_workers.

When running locally with test_workers I get the following output.

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

Has anyone else encountered similar issues?

EDIT: I have since resolved this issue and have posted an answer below

I have dumped the relevant configuration files below.

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

and my package.json script

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

Upvotes: 1

Views: 1006

Answers (2)

Matt bodey
Matt bodey

Reputation: 33

The issue here was due to the module filename being incorrectly defined in the local.runner.js

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

should be pointed directly at the Nightwatch file in its directory.

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

The difference in these files and the exact reason for this solution working are beyond me.

The answer was derived from the "suite" runner in https://github.com/browserstack/nightwatch-browserstack

Upvotes: 2

BountyHunter
BountyHunter

Reputation: 1411

It seems you are not specifying browsers. Modify your configuration file to be inline with the following configuration file:

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;

Upvotes: 0

Related Questions