Mocha with Selenium: Tutorial to Run Your First Test on TestMu AI
In this topic, you will learn how to configure and run your JavaScript automation testing scripts on TestMu AI Selenium cloud platform using JavaScript framework Mocha.
Objective
By the end of this topic, you will be able to:
- Set up an environment for testing your hosted web pages using Mocha framework with Selenium.
- Understand and configure the core capabilities required for your Selenium test suite.
- Run test cases in parallel using Mocha with Selenium to reduce build times.
- Test your locally hosted pages on TestMu AI platform.
- Explore advanced features of TestMu AI.
Note: All the code samples in this documentation can be found in the TestMu AI's Repository on GitHub. You can either download or clone the repository to quickly run your tests.
Prerequisites For Running Mocha Test Automation Scripts Using Selenium
Before getting started with Selenium automation testing on TestMu AI, you need to:
- Download and install NodeJS from official NodeJS documentation. You should be having NodeJS v6 or newer.
- Make sure you are using the latest version of JavaScript.
- Install npm from the official npm website.
- Download Selenium JavaScript bindings from the official website. Latest versions of Selenium Client and WebDriver are ideal for running your JavaScript automation testing script on TestMu AI’s Selenium Grid.
Installing Selenium Dependencies and tutorial repo
Step 1: Clone the TestMu AI’s mocha-selenium-sample repository and navigate to the code directory as shown below:
git clone https://github.com/LambdaTest/mocha-selenium-sample
cd mocha-selenium-sample
Step 2: Install the required project dependencies using the command below:
npm i
npm install selenium-webdriver
Setting up Your Authentication
Make sure you have your TestMu AI credentials with you to run test automation scripts on TestMu AI Selenium Grid. You can obtain these credentials from the TestMu AI Automation Dashboard or through TestMu AI Profile.
Step 3: Set TestMu AI Username and Access Key in environment variables.
- For Linux/macOS:
export LT_USERNAME= "undefined"
export LT_ACCESS_KEY= "undefined"
- For Windows:
set LT_USERNAME= "undefined"
set LT_ACCESS_KEY= "undefined"
Run Your First Test
Sample Test with MochaJS
//single_test.js
var assert = require("assert"),
webdriver = require("selenium-webdriver"),
conf_file = process.argv[3] || "conf/single.conf.js";
var caps = require("../" + conf_file).capabilities;
var buildDriver = function(caps) {
return new webdriver.Builder()
.usingServer(
"http://" +
LT_USERNAME +
":" +
LT_ACCESS_KEY +
"@hub.lambdatest.com/wd/hub"
)
.withCapabilities(caps)
.build();
};
describe("Google's Search Functionality for " + caps.browserName, function() {
var driver;
this.timeout(0);
beforeEach(function(done) {
caps.name = this.currentTest.title;
driver = buildDriver(caps);
done();
});
it("can find search results", function(done) {
driver.get("https://www.lambdatest.com").then(function() {
driver.getTitle().then(function(title) {
setTimeout(function() {
console.log(title);
assert(
title.match(
"Cross Browser Testing Tools | Test Your Website on Different Browsers | LambdaTest"
) != null
);
done();
}, 10000);
});
});
});
afterEach(function(done) {
if (this.currentTest.isPassed()) {
driver.executeScript("lambda-status=passed");
} else {
driver.executeScript("lambda-status=failed");
}
driver.quit().then(function() {
done();
});
});
});
Configuration of Your Test Capabilities
Step 4: In conf/single.conf.js file, you need to update your capabilities. In this code, we are passing browser, browser version, and operating system information, along with TestMu AI Selenium grid capabilities via capabilities object. The capabilities object in the above code are defined as:
exports.capabilities = {
'build': 'Mocha-Selenium-Sample', //Build name
'name': 'Your Test Name', // Test name
'platform':'Windows 10', // OS name
'browserName': 'chrome', // Browser name
'version': 'latest', // Browser version
'visual': false, // To take step by step screenshot
'network':false, // To capture network Logs
'console':false, // To capture console logs.
'tunnel': false // If you want to run the localhost than change it to true
};
You can generate capabilities for your test requirements with the help of our inbuilt Capabilities Generator Tool.
Executing the Test
Step 5: The tests can be executed in the terminal using the following command.
npm run single
Your test results would be displayed on the test console (or command-line interface if you are using terminal/cmd) and on TestMu AI automation dashboard. TestMu AI Automation Dashboard will help you view all your text logs, screenshots and video recording for your entire automation tests.
Running Your Parallel Tests Using Mocha Framework
Setting up the Parallel Environment
Here is the parallel_test.js file which would help you to run a single test on various browsers at the same time.
//parallel_test.js
var assert = require("assert"),
webdriver = require("selenium-webdriver"),
conf_file = process.argv[3] || "conf/single.conf.js";
var capabilities = require("../" + conf_file).capabilities;
var buildDriver = function(caps) {
return new webdriver.Builder()
.usingServer(
"http://" +
LT_USERNAME +
":" +
LT_ACCESS_KEY +
"@hub.lambdatest.com/wd/hub"
)
.withCapabilities(caps)
.build();
};
capabilities.forEach(function(caps) {
describe("Google's Search Functionality for " + caps.browserName, function() {
var driver;
this.timeout(0);
beforeEach(function(done) {
caps.name = this.currentTest.title;
driver = buildDriver(caps);
done();
});
it("can find search results" + caps.browserName, function(done) {
driver.get("https://www.lambdatest.com").then(function() {
driver.getTitle().then(function(title) {
setTimeout(function() {
console.log(title);
assert(
title.match(
"Cross Browser Testing Tools | Test Your Website on Different Browsers | LambdaTest"
) != null
);
done();
}, 10000);
});
});
});
afterEach(function(done) {
if (this.currentTest.isPassed()) {
driver.executeScript("lambda-status=passed");
} else {
driver.executeScript("lambda-status=failed");
}
driver.quit().then(function() {
done();
});
});
});
});
Executing Parallel Tests using Mocha
To run parallel tests using Mocha, we would have to execute the below command in the terminal:
npm run parallel
Your test results would be displayed on the test console (or command-line interface if you are using terminal/cmd) and on TestMu AI automation dashboard.
Testing Locally Hosted or Privately Hosted Projects
You can test your locally hosted or privately hosted projects with TestMu AI Selenium grid cloud using TestMu AI Tunnel app. All you would have to do is set up an SSH tunnel using TestMu AI Tunnel app and pass toggle tunnel = True via desired capabilities. TestMu AI Tunnel establishes a secure SSH protocol based tunnel that allows you in testing your locally hosted or privately hosted pages, even before they are made live.
Refer our TestMu AI Tunnel documentation for more information.
Here’s how you can establish TestMu AI Tunnel.
Download the binary file of:
Open command prompt and navigate to the binary folder.
Run the following command:
LT -user {user’s login email} -key {user’s access key}
So if your user name is [email protected] and key is 123456, the command would be:
LT -user [email protected] -key 123456
Once you are able to connect TestMu AI Tunnel successfully, you would just have to pass on tunnel capabilities in the code shown below :
Tunnel Capability
const capabilities = {
tunnel: true,
}
