Skip to main content
Learn all about testing on BrowserStack with interactive courses. Sign up for Test University!

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 JSON wire protocol-based client bindings. Check out our W3C-based scripts in the selenium-4 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.ie: Capability set to run BrowserStack test on Internet Explorer.

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 Internet Explorer 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_USER}',
          accessKey: '${BROWSERSTACK_KEY}',
        }
      },

      // other capabilities

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

    // 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.ie': {
      extends: 'browserstack',
      desiredCapabilities: {
        browserName: 'internet explorer',
        browserVersion: '11.0'
        // 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.ie,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.ie, 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.ie 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

  // environment to run tests on Android devices
  'browserstack.android': {
      extends: 'browserstack',
      desiredCapabilities: {
        os_version : '10.0',
        device : 'Samsung Galaxy S20',
        real_mobile : 'true',
        browserName: 'chrome',
        chromeOptions : {
          w3c: false
        }
      }
    },

    // environment to run tests on iOS devices
    'browserstack.ios': {
      extends: 'browserstack',
      desiredCapabilities: {
        os_version : '14',
        device : 'iPhone 12',
        real_mobile : 'true',
        browserName: 'safari',
        chromeOptions : {
          w3c: false
        }
      }
    },

    // other environments
    'browserstack.firefox': {
      //...

Update the command to run test

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

nightwatch-browserstack/package.json
  "scripts": {
      ...
      "parallel": "nightwatch --test ./tests/single/single_test.js  --env browserstack.android, browserstack.ios",

    ...
  },

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: {
  'browserstack.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 increases test execution time slightly. You can disable this feature by setting the browserstack.video capability to 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