Skip to main content
Get Automate insights in Microsoft Teams with our new integration! Get Started!.

Test on Internal Networks

Local Testing is a BrowserStack feature that allows you to run automated tests on internal development environments (localhost) or behind a corporate firewall.

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.

This guide will help you:

  1. Run your local test
  2. Understand Nightwatch project structure
  3. Understand the Nightwatch configuration file
  4. Understand the command to run local test

Prerequisites

Run your local test

  1. Clone the nightwatch-browserstack sample project, available on GitHub, using the following command:

    git clone https://github.com/browserstack/nightwatch-browserstack.git
    

    Note: You can use the repository to understand how test cases can be structured to integrate BrowserStack with your existing test suites.

  2. Open the cloned project in the IDE of your choice and install the required dependencies by running the following command in your command line:

     npm install
    
  3. Set your BrowserStack credentials in the project by running the following command at the root directory of your project:
     export BROWSERSTACK_USER=YOUR_USERNAME &&
     export BROWSERSTACK_KEY=YOUR_ACCESSKEY
    

    Note: It is recommended that the BrowserStack credentials are added to the environment variables to use them every time you run this project.

  4. Run a local test using the following command:
     npm run local
    
  5. View your tests on BrowserStack by visiting your Automate Dashboard.
Note: This npm command generates the nightwatch.conf.js file in the sample Nightwatch.js project folder, preloaded with some capabilities, such as browser name and browser version. If you are editing your existing test scripts and already have a configuration file, ensure that the browserstack.local capability is added to the configuration file and set to true. For example:
desiredCapabilities: {
        'browserstack.local': true
      }

Understand Nightwatch project structure

Nightwatch.js project consists of the following files and folders structure:

  • tests/local directory: It contains the test script file to test a locally-hosted website.
  • scripts directory: It contains the runner file required to set up and manage a local connection using methods, such as bs_local.start(), bs_local.stop(),and bs_local.isRunning(), which are provided by BrowserStack.
  • package.json file: It contains the installed dependencies of the project. It specifies the command to run the tests.
  • nightwatch.conf.js: It contains the configuration of environments and capabilities to use while running the test.

Understand the Nightwatch configuration file

In the sample repository, after you run the npm run local command, a configuration file named nightwatch.conf.js is created at the root directory of the project. It is preloaded with default configuration for connecting tests to BrowserStack, and sample capabilities, such as browser name and its version, for running tests on multiple browsers.

As seen in the code snippet given below, the browserName has been set to firefox, within desiredCapabilities, to run a test on Firefox. As there is no desktop machine specified, the local test runs on Windows 10 by default.

nightwatch-browserstack/nightwatch.conf.js

// default setting of the test
test_settings: {
    default: {
      disable_error_log: false,
      launch_url: 'https://nightwatchjs.org',

      screenshots: {
        enabled: false,
        path: 'screens',
        on_failure: true
      },

      desiredCapabilities: {
        browserName : 'firefox'
      },

      webdriver: {
        start_process: true,
        server_path: (Services.geckodriver ? Services.geckodriver.path : '')
      }
    },

// other capabilities

// Configuration for when using the browserstack.com cloud service               |

browserstack: {
      selenium: {
        // setting the host name
        host: 'hub-cloud.browserstack.com',
        port: 443
      },
      // More info on configuring capabilities can be found on:
      // https://www.browserstack.com/automate/capabilities?tag=selenium-4
      desiredCapabilities: {
        'bstack:options' : {
          // sets BrowserStack's credentials via environment variables
          userName: '${BROWSERSTACK_USER}',
          accessKey: '${BROWSERSTACK_KEY}',
        }
      },

      //...

      // capabilities to run local test on BrowserStack
      'browserstack.local': {
        extends: 'browserstack',
        desiredCapabilities: {
          'browserstack.local': true
      }
    },

Understand the command to run local test

The test file and capabilities to consider for running local test is specified in the package.json file, as shown below:

nightwatch-browserstack/package.json
    "scripts": {
      ...
    "local": "node scripts/local.runner.js --test ./tests/local/local_test.js  --env browserstack.local ",
    ...
  },

This statement instructs the npm run local command to run the local_test.js file by using capabilities specified within browserstack.local in the nightwatch.conf.js file.

Next steps

After you have successfully run your local test on BrowserStack, you might want to do the following:

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