LambdaTest Selenium Automation Grid is a cloud-based scalable Selenium testing platform which enables you to run your automation scripts on 2000+ different browsers and operating systems. You can now run your JavaScript Selenium automated test cases on a scalable Selenium infrastructure that is running real browsers and real operating systems.
This post will help you in getting started with configuring and running your JavaScript based automation test scripts on LambdaTest Selenium cloud platform using Nightwatch framework. This topic will help you to:
Before getting started with Automated Scripts using Selenium with Nightwatch framework on LambdaTest Automation, you need to:
Install Nightwatch automation framework through npm
1 |
npm install nightwatch |
Next step is to install Selenium dependencies for Node.js using npm. Here’s the command to run:
1 |
npm i selenium-webdriver |
Follow our documentation on Lambda Tunnel to know it all. OS specific instructions to download and setup tunnel binary can be found at the following links.
Download the binary file of:
Run Nightwatch automation tests on LambdaTest Selenium grid, it is as easy as changing a few lines of code. To start with, you would have to invoke Selenium remote webdriver instead of local browser webdriver. In addition, since we are using remote webdriver, we have to define which browser environment we want to run the test. We do that by passing browser environment details to LambdaTest Selenium grid via desired capabilities class. You can use LambdaTest Capabilities Generator to select & pass those browser environment specifications.
Let’s checkout sample Nightwatch framework code running LambdaTest Selenium grid. This is a simple Nightwatch automation script that test a sample to-do list app. The code marks two list items as done, add a list item and then finally give the total number of pending items as output.
You can also find this code at our GitHub repository for Nightwatch automation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
//googleTest.js var https = require('https'); module.exports = { '@tags': ['test'], 'Google': function(client) { client .url('https://www.google.com/ncr') .waitForElementVisible('body', 10000) .setValue('input[type=text]', 'LambdaTest\n') .pause(1000) .assert.title('LambdaTest - Google Search') .end(); }, afterEach: function(client, done) { setTimeout(function() { done(); }, 1000); } }; |
Below is the nightwatch.conf.js file where we will be declaring the desired capabilities.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//nightwatch.conf.js module.exports = (function(settings) { console.log(settings["test_settings"]["default"]["username"]) if (process.env.LT_USERNAME) { settings["test_settings"]["default"]["username"] = process.env.LT_USERNAME; } if (process.env.LT_ACCESS_KEY) { settings["test_settings"]["default"]["access_key"] = process.env.LT_ACCESS_KEY; } if (process.env.SELENIUM_HOST) { settings.selenium.host = process.env.SELENIUM_HOST; } if (process.env.SELENIUM_PORT) { settings.selenium.host = process.env.SELENIUM_PORT; } return settings; })(require('./nightwatch.json')); |
Now, Let’s create nightwatch.json.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
//nightwatch.json { "src_folders" : ["tests"], "output_folder" : "reports", "page_objects_path" : "", "globals_path" : "", "selenium" : { "start_process" : false, "server_path" : "", "log_path" : "", "host" : "hub.lambdatest.com", "port" : 80, "cli_args" : { "webdriver.chrome.driver" : "", "webdriver.ie.driver" : "", "webdriver.firefox.profile" : "" } }, "test_workers" : {"enabled" : true, "workers" : "auto"}, "test_settings" : { "default" : { "launch_url" : "https://lambdatest.com", "selenium_port" : 80, "selenium_host" : "hub.lambdatest.com", "silent": false, "screenshots" : { "enabled" : true, "path" : "" }, "username" : "", "access_key" : "", "skip_testcases_on_fail": false, "desiredCapabilities": { "build":"Nightwatch-Selenium-Sample", "visual":true, "video":true, "console":true, "network":true } }, "chrome": { "desiredCapabilities": { "platform": "Windows 8", "browserName": "chrome", "version": "71.0" } }, "safari" : { "desiredCapabilities": { "platform": "macos 10.13", "browserName": "safari", "version": "11.0" } }, "firefox" : { "desiredCapabilities": { "platform": "win10", "browserName": "firefox", "version": "60" } }, "edge" : { "desiredCapabilities": { "platform": "Windows 10", "browserName": "MicrosoftEdge", "version": "17.0" } } } } |
Now, finally add package.json file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//package.json { "name": "JS-Nightwatch.js", "version": "0.0.0", "description": "NightWatch examples for LambdaTest", "main": "", "scripts": { "test": "./node_modules/.bin/nightwatch -e chrome,firefox,edge,safari tests" }, "devDependencies": { "nightwatch": "^1.0.18" } } |
The Selenium Webdriver test would open a URL, mark the first two items in the list as done, add an item in the list, and return the total number of pending item. Your results would be displayed on the test console (or command-line interface if you are using terminal/cmd) and on LambdaTest dashboard. LambdaTest Dashboard will help you view all your text logs, screenshots and video recording for your entire Selenium tests.
You would need to run the below command in your terminal/cmd.
1 2 3 4 5 6 7 8 9 10 |
// Running First Test $ npm install ** Linux/Mac $ ./node_modules/.bin/nightwatch -e chrome,edge,firefox tests ** Windows $ node_modules\.bin\nightwatch -e chrome,edge,firefox tests |