Test on Internal Networks

BrowserStack enables you to run your Nightwatch automated tests on your internal development environments, on localhost, and from behind a corporate firewall. This feature is called “Local Testing”.

Local Testing establishes a secure connection between your machine and the BrowserStack cloud. Once you set up Local Testing, all URLs work out of the box, including HTTPS URLs and those behind a proxy or firewall.

Note: Code samples in this guide can be found in the nightwatch-browserstack sample repo on GitHub

Configuring your Nightwatch tests for Local Testing takes just three steps:

  1. Install the BrowserStack Local Testing bindings:

    npm install browserstack-local
    
  2. Next, you need to update your Nightwatch config file, nightwatch-browserstack/conf/local.conf.json (in sample repo), and set the browserstack.local capability to true:

    nightwatch_config = {
      selenium : {
        "start_process" : false,
        "host" : "hub-cloud.browserstack.com",
        "port" : 443,
        "proxy": "http://PROXY_USERNAME:PROXY_PASSWORD@proxy-host:proxy-port"  // If you are behind a proxy
      },
    
      test_settings: {
        default: {
          desiredCapabilities: {
            'browserstack.user': 'YOUR_USERNAME',
            'browserstack.key': 'YOUR_ACCESS_KEY',
            'browserstack.local': true,
            'browser': 'chrome',
            'name': 'Bstack-[Nightwatch] Local Test'
          }
        }
      }
    };
    
    // Code to copy seleniumhost/port into test settings
    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;
    }
    module.exports = nightwatch_config;
    
  3. Run a sample test case to check whether the Local test connection has been setup properly. You can use the following sample test case, which can be found in the repo under nightwatch-browserstack/tests/local/local_test.js:

    module.exports = {
      'BrowserStack Local Testing' : function (browser) {
        browser
          .url('http://bs-local.com:45691/check')
          .waitForElementVisible('body', 1000)
          .assert.containsText('body', 'Up and running')
          .end();
      }
    };
    

    You will also need the following custom script, nightwatch-browserstack/scripts/local.runner.js which starts and stops the Local test connection:

    #!/usr/bin/env node
    
    var Nightwatch = require('nightwatch');
    var browserstack = require('browserstack-local');
    var bs_local;
    
    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': 'YOUR_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);
    }
    
  4. You can now run your Nightwatch test using BrowserStack Local with the following command:

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

We're sorry to hear that. Please share your feedback so we can do better







Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better







Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback!

Talk to an Expert
Talk to an Expert