Skip to main content
Introducing the Automate SDK! Get your entire test suite running on BrowserStack in minutes! Learn More.

Selenium with Nightwatch

A guide to run Selenium Webdriver tests with Nightwatch on BrowserStack.

Important: Sample test scripts are available in the nightwatch-browserstack repository.

The sample test script in this section is compatible with W3C-based client bindings. Check out our JSON wire protocol-based scripts in the selenium-3 branch of the repository.

Introduction

BrowserStack gives you instant access to our Selenium Grid of 3000+ real devices and desktop browsers. Running your Selenium tests with Nightwatch on BrowserStack is simple. This guide will help you:

  1. Run your first test
  2. Understand the details of your test
  3. Run test on mobile devices
  4. Debug your app

Prerequisites

Run your first test

To run your first Nightwatch test on BrowserStack, perform the following steps:

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

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

Step 2: Install the required dependencies by running the following command in your command line:

    npm install

Step 3: Set your BrowserStack credentials in the project by running the following command at the root directory of your project:

# Set these values in your ~/.zprofile (zsh) or ~/.profile (bash)
  export BROWSERSTACK_USERNAME="YOUR_USERNAME"
  export BROWSERSTACK_ACCESS_KEY="YOUR_ACCESS_KEY"
# setx.exe does not set the environment variable in the current command prompt, but it will be available in subsequent command prompts
  setx BROWSERSTACK_USERNAME="YOUR_USERNAME"
  setx BROWSERSTACK_ACCESS_KEY="YOUR_ACCESS_KEY"

# Verify whether the variables have been set
echo BROWSERSTACK_USERNAME
echo BROWSERSTACK_ACCESS_KEY

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

Step 4: Run a parallel test using the following command:

    npm run parallel

Step 5: View your tests on BrowserStack by visiting your Automate Dashboard.

Note: When you run the test, the nightwatch.conf.js file is generated automatically preloaded with some capabilities. Check out the Understand the Nightwatch configuration file section to learn about the nightwatch.conf.js file.

Understand the details of your test

When you run the npm run parallel command, the test written in the single_test.js file, located in the nightwatch-browserstack/tests/single/ directory, is executed on several browsers, namely Chrome, Firefox, and Safari.

When the test is triggered, it:

  • Opens the bstackdemo.com website.
  • Adds a product to the cart.
  • Verifies whether the product is added to the cart.
nightwatch-browserstack/tests/single/single_test.js
module.exports = {
  "Bstack Demo": async function (browser) {
    browser.url('https://www.bstackdemo.com/');

    // Get the product text on the page
    const productText = await browser.useXpath().getText('//*[@id="1"]/p', function(result) {
      return Promise.resolve(result.value);
    });

    //Check of button present for add to cart
    browser.useXpath().expect.element('//*[@id="1"]/div[4]').to.be.present;

    //click on add to cart
    browser.useXpath().click('//*[@id="1"]/div[4]');

    // Check if cart os opened 
    browser.useXpath().expect.element('//*[@class="float-cart__content"]').to.be.present;

    // Check if product in cart is equal to the product we selected
    browser.useXpath().expect.element('//*[@id="__next"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]').text.to.equal(productText);
  }
};

Understand the Nightwatch configuration file

After you run your first test, a configuration file named nightwatch.conf.js is created at the root directory of the project. This file is preloaded with default configuration required for connecting your tests to BrowserStack, and sample capabilities, such as browser name and its version, for running tests on several browsers.

Note: You can use our capability builder and select from a wide range of custom capabilities that BrowserStack supports.

The configuration file includes preloaded capabilities for every browser:

  • browserstack.chrome: Capability set to run BrowserStack test on Chrome.
  • browserstack.firefox: Capability set to run BrowserStack test on Firefox.
  • browserstack.safari: Capability set to run BrowserStack test on Safari.
  • browserstack.edge: Capability set to run BrowserStack test on Edge.

As there are no desktop machines or mobile devices defined in the capabilities, the parallel test runs on default desktop machines. Tests defined to run on the Chrome, Firefox, and Edge browsers choose a Windows OS machine, whereas tests defined to run on the Safari browser choose a macOS machine.

nightwatch-browserstack/nightwatch.conf.js

browserstack: {
      selenium: {
        // setting the host name
        host: 'hub.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_USERNAME}',
          accessKey: '${BROWSERSTACK_ACCESS_KEY}',
        }
      },

      disable_error_log: true,
      webdriver: {
        timeout_options: {
          timeout: 30000,
          retry_attempts: 3
        },
        keep_alive: true,
        start_process: false
      }
    },

      // other capabilities

    // capabilities to run test on Chrome
    'browserstack.chrome': {
      extends: 'browserstack',
      desiredCapabilities: {
        browserName: 'chrome',
          // add other capabilities you want
        'goog:chromeOptions': {
          w3c: true
        }
      }
    },

    // capabilities to run test on Firefox
    'browserstack.firefox': {
      extends: 'browserstack',
      desiredCapabilities: {
        browserName: 'firefox'
        // add other capabilities you want
      }
    },

    // capabilities to run test on Internet Explorer
    'browserstack.edge': {
      extends: 'browserstack',
      desiredCapabilities: {
        browserName: 'edge'
        // add other capabilities you want

      }
    },

    // capabilities to run test on Safari
    'browserstack.safari': {
      extends: 'browserstack',
      desiredCapabilities: {
        browserName: 'safari',
        // add other capabilities you want

      }
    },
// ...
Note: By referring to the browserstack section of this auto-generated nightwatch.conf.js configuration file, you can integrate your existing tests with BrowserStack. To learn more about integrating existing tests with BrowserStack, check out the Migrate your test suites guide.

Understand the command to run parallel test

The test file and capabilities required for running a parallel test is specified in the package.json file, as shown in the following example:

nightwatch-browserstack/package.json
    "scripts": {
      ...
      "parallel": "nightwatch --test ./tests/single/single_test.js --env browserstack.chrome,browserstack.edge,browserstack.safari,browserstack.firefox",
    ...
  },

This statement instructs the npm run parallel command to run the single_test.js file by using capabilities specified within browserstack.chrome, browserstack.edge, browserstack.safari, and browserstack.firefox sections of the nightwatch.conf.js file for running parallel tests on Chrome, Internet Explorer, Safari, and Firefox respectively.

Run test on mobile devices

The auto-generation nightwatch.conf.js configuration file is preloaded with capabilities for running tests on desktop machines only. To run tests on mobile devices, complete the following steps:

  1. Update the configuration file
  2. Update the command to run test

Update the configuration file

In the nightwatch.conf.js file, add the following code snippet where other environments such as browserstack.chrome or browserstack.edge are defined.

You can update values of the browserName, device, and os_version capabilities to run your test on any other Android or iOS devices.

nightwatch-browserstack/nightwatch.conf.js
  //other environments

  'browserstack.chrome_android': {
    extends: 'browserstack',
    desiredCapabilities: {
      browserName: 'chrome',
      'bstack:options': {
        deviceName: 'Samsung Galaxy S22',
        osVersion: '12.0'
      },
    'goog:chromeOptions': {
      w3c: true
      }
    }
  },

  'browserstack.safari_ios': {
    extends: 'browserstack',
    desiredCapabilities: {
      browserName: 'safari',
      'bstack:options': {
        deviceName: 'iPhone 12',
        osVersion: '14'
      }
    }
  },

  'browserstack.samsung_android': {
    extends: 'browserstack',
    desiredCapabilities: {
      browserName: 'samsung',
      'bstack:options': {
        deviceName: 'Samsung Galaxy S22',
        osVersion: '12.0'
      }
    }
  },

Update the command to run test

In the package.json file, update the single command by setting the environment to either browserstack.chrome_android, browserstack.safari_ios, or browserstack.samsung_android for running tests on Android, iOS, or Samsung devices respectively, as shown in the following example:

nightwatch-browserstack/package.json
  "scripts": {
      ...
      "parallel": "nightwatch --test ./tests/single/single_test.js  --env browserstack.chrome_android,browserstack.safari_ios,browserstack.samsung_android",

    ...
  },

Debug your app

BrowserStack provides a range of debugging tools to help you quickly identify and fix bugs you discover through your automated tests. Learn more about how to debug tests on BrowserStack using the Automate Dashboard.

Text logs

Text Logs are a comprehensive record of your test. They are used to identify all the steps executed in the test and troubleshoot errors for the failed step. Text Logs are accessible from the Automate dashboard or via our REST API.

Visual logs

Visual Logs automatically capture the screenshots generated at every Selenium command run through your Nightwatch tests. Visual logs help with debugging the exact step and the page where failure occurred. They also help identify any layout or design related issues with your web pages on different browsers.

Visual Logs are disabled by default. In order to enable Visual Logs you will need to set browserstack.debug capability to true in the nightwatch.conf.js file.

desiredCapabilities: {
  'bstack:options': {
    debug: true
  }
}

Sample Visual Logs from Automate Dashboard: BrowserStack Automate Visual Logs)

Video recording

Every test run on the BrowserStack Selenium grid is recorded exactly as it is executed on our remote machine. This feature is particularly helpful whenever a browser test fails. You can access videos from Automate Dashboard for each session. You can also download the videos from the Dashboard or retrieve a link to download the video using our REST API.

Note: Video recording slightly increases test execution time. To disable video recording, set video to false within bstack:options.
desiredCapabilities: {
  'bstack:options': {
    video: false
  }
}

In addition to these logs BrowserStack also provides Raw logs, Network logs, Console logs, Selenium logs, Appium logs and Interactive session. You can find the complete details to enable all the debugging options.

Next steps

After you have successfully run your first test on BrowserStack, you might want to do one of 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