Skip to main content

Automation Testing With Mocha Test Framework And Selenium

Mocha is one of the most renowned test automation frameworks for JavaScript. It offers powerful test reporting with serial test execution for asynchronous testing, along with immense browser support. The best part is that it runs on Node.js and highlights uncaught exceptions to the appropriate test cases, making it fairly adaptable for budding automation testers.

This document will help you execute your test automation scripts using Mocha with Selenium Grid cloud. You will be learning about:

  • Prerequisites To Run Mocha Selenium Testing
  • Running your first automation test case using Mocha Selenium test framework.
  • Parallel testing with Mocha test framework and Selenium.
  • Running Mocha Selenium tests for locally hosted web application.

Prerequisites To Run Mocha Selenium Testing#

All the code samples in this documentation can be found in the Mocha LambdaTest Repository on Image GitHub. You can either download or clone the repository to quickly run your tests.

  1. LambdaTest Authentication Credentials You need to know your LambdaTest authentication credentials i.e. your LambdaTest username, access key and HubURL. You need to set them up as your environment variables.

After cloning the repository, you would need to update the *.conf.js files inside the conf/ directory with your LambdaTest Username and Access Key.

You can retrieve them from your LambdaTest automation dashboard by clicking on the key icon near the help button or by running the below commands in your cmd/terminal.

  • For Linux/Mac:
$ export LT_USERNAME= "undefined"$ export LT_ACCESS_KEY= "undefined"
  • For Windows:
$ set LT_USERNAME= "undefined"$ set LT_ACCESS_KEY= "undefined"
  1. Global Dependencies

$ brew install node

First Automation Test Case using Mocha Test Framework and Selenium#

After cloning the LambdaTest GitHub repository for Mocha and Selenium and configuring your LambdaTest authentication credentials in the conf.js file. You now need to simply run the below command for executing your first automation test script using Mocha test framework and Selenium.

npm run single

Parallel Testing With Mocha Selenium#

With LambdaTest’s online Selenium Grid, you can execute Selenium scripts in parallel, allowing your team to shorter test cycles and faster product delivery. You could run a similar test case on multiple browser + OS combinations or you could run multiple test cases over a single browser + OS combination.

The more concurrent sessions you have, the more tests you can run in parallel. If you are confused about how many parallel sessions you may need, don’t be! We have come up with a Concurrency Calculator that helps with an estimate of the number of concurrent sessions you need to go for.

Use the below command to run your Mocha and Selenium test cases in parallel.

npm run parallel

Mocha Selenium Tests For Locally Hosted Web Pages#

Using LambdaTest tunnel you can perform cross browser testing on your locally hosted web pages and web applications on 2000+ real browsers hosted on our cloud servers. LambdaTest tunnel establishes an SSH(Secure Shell) connection between your local machine and our cloud servers.

Refer our LambdaTest Tunnel documentation for more information.

Download latest LambdaTest Tunnel binary file and extract it.

To run your Mocha automation script for locally hosted web applications on LambdaTest Selenium Grid, you would need to set the Tunnel Capability flag to true.

var capabilities ={  "tunnel" : true}

You can also refer to our Selenium Desired Capabilities Generator to get the Capabilties class as per your requirements without having to code yourself.

Important Note: Some Safari & IE browsers don’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 “” or replace the string “localhost” with the machine IP address.

For example, if you wanted to test “http://localhost/dashboard” or, and your machine IP is, you can instead test on “” or ““.

Kudos! That is all you need to run your Mocha test framework scripts on LambdaTest Selenium Grid. You can view the results of your test script execution on the LambdaTest Automation Dashboard.

Got Questions? Feel free to give us a shout on our 24/7 in-app customer chat support. You can even mail to [email protected]. Happy Testing! 🙂

Last updated on