This tutorial will help you to automate your Nightwatch tests on LambdaTest online Selenium cloud grid.
- Install npm.
sudo apt install npm
- Install NodeJS.
sudo apt install nodejs
Step 1. Clone the Nightwatch Selenium Repository.
git clone https://github.com/LambdaTest/nightwatch-selenium-sample.git
Step 2. Inside mightwatch-selenium-sample, export the Lambda-test Credentials. You can get these from your automation dashboard.
For Linux/macOS:
export LT_USERNAME="YOUR_USERNAME"
export LT_ACCESS_KEY="YOUR ACCESS KEY"
For Windows:
set LT_USERNAME="YOUR_USERNAME"
set LT_ACCESS_KEY="YOUR ACCESS KEY"
Step 3. Inside nightwatch-selenium-sample folder install necessary packages.
cd nightwatch-selenium-sample
npm i
Step 4. To run your First Test.
npm run single
You can check your test results on the Automation Dashboard.
- Will use the same test script over different configration to demonstarte parallel testing. Parallel testing with Mocha will help you to run multiple test cases simultaneously.
npm run parallel
- Importing necessary packages, and cofiguring Lambdatest credentials.
var https = require("https");
var lambdaRestClient = require("@lambdatest/node-rest-client");
var lambdaCredentials = {
username: process.env.LT_USERNAME,
accessKey: process.env.LT_ACCESS_KEY
};
var lambdaAutomationClient = lambdaRestClient.AutomationClient(
lambdaCredentials
);
- Next, we write our test to open Todo app, and add a task in the list.
module.exports = {
"@tags": ["test"],
TodoTest: function(client) {
client
.url("https://lambdatest.github.io/sample-todo-app/")
.waitForElementPresent("body", 1000)
.setValue("input[id=sampletodotext]", "Complete LambdaTest tutorial.")
.click("input[id=addbutton]")
.pause(1000)
.end();
},
after: function(browser) {
console.log("Closing down...");
},
afterEach: function(client, done) {
if (
process.env.LT_USERNAME &&
process.env.LT_ACCESS_KEY &&
client.capabilities &&
client.capabilities["webdriver.remote.sessionid"]
) {
lambdaAutomationClient.updateSessionById(
client.capabilities["webdriver.remote.sessionid"],
{ status_ind: client.currentTest.results.failed ? "failed" : "passed" },
function(error, session) {
console.log(error)
if (!error) {
client.pause(1000)
done();
}
}
);
} else {
console.log("Test Run Successfully!");
client.pause(1000)
done();
}
}
};
Since, now we have a first script ready. Let us specify the selenium capabilities to run the script on LambdaTest cloud-based Selenium Grid. LambdaTest provides a capability generator to the capabilities in all the major languages. All you need to do is to select the OS, Resolution, Browser, Version and the code will be generated. You can just copy it and paste it in your code. In the above example, we have used the following Selenium capabilities which are mentioned in "nightwatch.json" file inside the repo.
"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"
}
}
Note: Don't forget to change your location as Javascript in the Selenium capability generator.
To perform an automation test on a file or application hosted on your local environment or behind firewall, follow the given steps:
- Set tunnel value to
True
in test capabilities So for example, if I have to run the above script for a locally hosted web-application then my capabilities class would be :
"tunnel" : true;
OS specific instructions to download and setup tunnel binary can be found at the following links.
- Some Safari & IE browsers, doesn't support automatic resolution of the URL string "localhost". Therefore if you test on URLs like "http://localhost/" or "http://localhost:8080" etc, you would get an error in these browsers. A possible solution is to use "localhost.lambdatest.com" or replace the string "localhost" with machine IP address. For example if you wanted to test "http://localhost/dashboard" or, and your machine IP is 192.168.2.6 you can instead test on "http://192.168.2.6/dashboard" or "http://localhost.lambdatest.com/dashboard".
LambdaTest is a cloud based selenium grid infrastructure that can help you run automated cross browser compatibility tests on 2000+ different browser and operating system environments. LambdaTest supports all programming languages and frameworks that are supported with Selenium, and have easy integrations with all popular CI/CD platforms. It's a perfect solution to bring your selenium automation testing to cloud based infrastructure that not only helps you increase your test coverage over multiple desktop and mobile browsers, but also allows you to cut down your test execution time by running tests on parallel.