A guide to running Selenium Webdriver tests with Nightwatch on BrowserStack.
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:
Before you can start running your Selenium tests with Nightwatch, install Nightwatch using npm
npm install nightwatch
To run your first Nightwatch test on BrowserStack, follow the steps below:
git clone https://github.com/browserstack/nightwatch-browserstack.git
cd nightwatch-browserstack
npm install
nightwatch-browserstack/conf/single.conf.js
file as shown below:nightwatch_config = {
...
test_settings: {
default: {
desiredCapabilities: {
'browserstack.user': 'YOUR_USERNAME',
'browserstack.key': 'YOUR_ACCESS_KEY',
...
}
}
}
};
...
Alternatively, you can set the environment variables in your system as shown below:
export BROWSERSTACK_USERNAME="YOUR_USERNAME"
export BROWSERSTACK_ACCESS_KEY="YOUR_ACCESS_KEY"
Note: Make sure the environment variable is set permanently
./node_modules/.bin/nightwatch -c conf/single.conf.js
You can visit BrowserStack Automate Dashboard and see your test there once it has successfully completed.
The sample test that you just ran can be found in nightwatch-browserstack/tests/single/single_test.js
. The 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();
}
};
In the sample repository, you can find conf/single.conf.js
file which is responsible for configuring your test to run on BrowserStack. The useful sections of the file are shown below which enable the tests to run on BrowserStack:
nightwatch_config = {
src_folders : [ "tests/single" ], // Specifies which tests would run
selenium : {
"start_process" : false,
"host" : "hub-cloud.browserstack.com", // Specifies the BrowserStack Hub URL
"port" : 443
},
test_settings: {
default: {
// The following are the BrowserStack specific capabilities for all your tests that will run using this conf file.
desiredCapabilities: {
'build': 'nightwatch-browserstack',
'browserstack.user': 'YOUR_USERNAME',
'browserstack.key': 'YOUR_ACCESS_KEY',
'browserstack.debug': true,
'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;
desiredCapabilities
section of single.conf.js
file. This will help you to migrate your existing tests.
BrowserStack does not know whether your test’s assertions have passed or failed because only the framework knows whether the assertions have passed.
You have to mark your tests as passed or failed based on the test assertions using the REST API code snippet as shown below. It is recommended that you use this snippet under globals: { afterEach() }
method:
var request = require("request");
request({uri: "https://YOUR_USERNAME:YOUR_ACCESS_KEY@api.browserstack.com/automate/sessions/<session-id>.json", method:"PUT", form:{"status":"<passed/failed>","reason":""}})
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 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 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
}
Sample Visual Logs from Automate Dashboard:
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.
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. Complete details to enable all the debugging options can be found here.
Once you have successfully run your first test on BrowserStack, you might want to do one of the following:
Contact our Support team for immediate help while we work on improving our docs.
Contact our Support team for immediate help while we work on improving our docs.