CAPABILITY GENERATOR

LambdaTest DocumentationAutomation Testing ● Automation Testing With Mocha And Selenium

Automation Testing With Mocha 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 on-cloud. You will be learning about:

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

Prerequisites To Run Mocha and Selenium Testing


Important: Clone LambdaTest GitHub repository for Mocha and Selenium.

  • 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:
    • For Windows:
  • Global Dependencies
    • Make sure to install Node.js from their official website.
    • You could also install Node.js using Homebrew.

First Automation Test Case using Mocha 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 and Selenium.

Parallel testing with Mocha and 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.

Mocha & Selenium Tests for Locally Hosted Web Pages


Using Lambda Tunnel you can perform cross browser testing of your locally hosted web pages and web applications on 2000+ real browsers hosted on our cloud servers. Lambda 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.

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, 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 the 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“.

Kudos! That is all you need to run your Mocha test 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 support@lambdatest.com. Happy Testing! 🙂