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

Selenium with Nightwatch

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

Note: Code samples used in this guide are available in the nightwatch-browserstack sample repository on GitHub.

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 your first test script
  3. Understand the Nightwatch configuration file
  4. Understand the command to run single test
  5. Run test on mobile devices
  6. Debug your app
Protip: For detailed understanding about integrating Nightwatch and BrowserStack, visit Test University.

Prerequisites

Run your first test

Protip: Selenium 4 is now supported on BrowserStack. To use the Selenium 4 client bindings, modify your existing test scripts as follows:
  • Edit or add capabilities in the W3C format using our W3C capability generator.
  • Add the seleniumVersion capability in your test script and set the value to 4.0.0.

To run your first Nightwatch test on BrowserStack, follow the steps below:

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

    git clone https://github.com/browserstack/nightwatch-browserstack.git
    
  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 single test using the following command:
     npm run single
    
  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 your first test script

When you run the npm run single command, the single_test.js file within the nightwatch-browserstack/tests/single/ directory is executed. This test script searches for the string BrowserStack on google.com, and verifies if the title of the resulting page is BrowserStack - Google Search:

nightwatch-browserstack/tests/single/single_test.js
module.exports = {
  "Google's Search Functionality": function (browser) {
    browser
      .url("https://www.google.com")
      .setValue("input[name=q]", ["BrowserStack", browser.Keys.ENTER]) // this submits on desktop browsers
      .pause(1000)
      .title((result) => {
        if (!/BrowserStack/i.test(result.value)) {
          browser.submitForm("input[name=q]"); // this helps in mobile browsers
        }
      }).expect.title().to.match(/BrowserStack/i);
    browser.end();
  },
};

Understand the Nightwatch configuration file

In the sample repository, after you run your first test, 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 several browsers.

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

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

nightwatch-browserstack/nightwatch.conf.js

// default setting of the single 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}',
        }
      },
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 single test

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

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

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

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 below:

nightwatch-browserstack/package.json
  "scripts": {
      ...
    // command to run single test on Android device
   // "single": "nightwatch --test ./tests/single/single_test.js --env browserstack.android",

    // to run single test on iOS device
    // "single": "nightwatch --test ./tests/single/single_test.js --env 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