Search by

Selenium with Nightwatch

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

Nightwatch repo on Github

Introduction

BrowserStack gives you instant access to our Selenium Grid of 2000+ 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. Mark tests as pass / fail
  3. Debug your app

Prerequisites

Before you can start running your Selenium tests with Nightwatch, Install Nightwatch using npm

  1. Install Nightwatch using npm
# Install using npm

npm install nightwatch

Run your first test

To understand how to integrate with BrowserStack, we will look at two things:

  1. A sample test case written in Nightwatch with NodeJS
  2. Integration of this sample test case with BrowserStack

Sample test case

The sample Nightwatch test case below searches for the string “BrowserStack” on Google, and checks if the title of the resulting page is “BrowserStack - Google Search”

module.exports = {
  'Google\'s Search Functionality' : function (browser) {
    browser
      .url('https://www.google.com/ncr')
      .waitForElementVisible('body', 1000)
      .setValue('input[type=text]', 'BrowserStack')
      .click('input[name=btnK]')
      .pause(1000)
      .assert.title('BrowserStack - Google Search')
      .end();
  }
};

Now that we have created the test case, we are ready to integrate it into BrowserStack.

Integrating with BrowserStack

Note: Running your Selenium tests on BrowserStack requires a BrowserStack Username and Access Key.

To obtain your username and access keys, sign up for a Free Trial or purchase a plan.

To integrate the sample Nightwatch test case into BrowserStack, we have to update the *.conf.js config files with the BrowserStack Hub URL and credentials required to connect to the BrowserStack Selenium grid. The example below shows the single.conf.js config file, used for single test runs:

nightwatch_config = {
  selenium : {
    "start_process" : false,
    "host" : "hub-cloud.browserstack.com",
    "port" : 80,
    "proxy": "http://PROXY_USERNAME:PROXY_PASSWORD@proxy-host:proxy-port"
  },

  test_settings: {
    default: {
      desiredCapabilities: {
        'browserstack.user': 'YOUR_USERNAME',
        'browserstack.key': 'YOUR_ACCESS_KEY',
        'browser': 'chrome',
      }
    }
  }
};

// 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;

We are now ready to run the test on BrowserStack, using the following command:

./node_modules/.bin/nightwatch -c conf/single.conf.js

Mark tests as pass / fail

BrowserStack provides a comprehensive REST API to access and update information about your tests. Shown below is a sample code snippet which allows you to mark your tests as pass or fail based on the assertions in your Nightwatch test cases.

var request = require("request");
request({uri: "https://YOUR_USERNAME:YOUR_ACCESS_KEY@api.browserstack.com/automate/sessions/<session-id>.json", method:"PUT", form:{"status":"completed","reason":""}})

A full reference of our REST API can be found here.

Debug your app

BrowserStack provides a range of debugging tools to help you quickly identify and fix bugs you discover through your automated tests.

  • 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.

desiredCapabilities: {
  'browserstack.debug': true
}
  • 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.

desiredCapabilities: {
  'browserstack.video' : 'false'
}

In addition to these logs BrowserStack also provides Raw logs, Network logs, Console logs, Selenium logs, Appium logs and Interactive session. Complete details to enable all the debugging options can be found here.