How to Test Selenium Node.JS with Mocha
By Mohit Joshi, Community Contributor - February 15, 2023
Node.JS is one of the most popularly used testing framework. It is easy to set up and comes with a wide array of features. The ease of reporting and integrating with a CI process and tool makes it a popular choice among testers.
Mocha is an extremely rich JavaScript test framework that runs on Node.js. Mocha tests run synchronously, thereby ensuring flexibility, accuracy, and mapping uncaught expectations with correct test cases.
There are several tools that allow testing. However, Selenium is the industry’s top choice to offer automation features. It keeps the development and testing going hand-in-hand, without slowing down the pace of the software development cycle. Also, tests being automated helps to test several aspects of an application like stability, security, and more without intervening in the process.
This guide explains how to test Selenium NodeJS with Mocha.
Getting started with Node.JS Unit Testing
Node.JS Unit testing is the process of testing small code pieces or components in isolation inside the NodeJS application. It helps in finding bugs early and improves the quality of the code. This section explains how to set up Selenium Node.JS unit testing.
Prerequisites to set up Selenium Node.JS
- You must have installed the latest version of NodeJs on your machine and must have set up all the necessary path variables.
- Install the Selenium Webdriver to automate your test.
- Since you will be running tests on your local machine, you will need a browser to run the test case, therefore, install the latest version of the chrome driver package. However, you can install any browser driver package depending on the choice of browser you want to run the test case.
- Pick any IDE of your choice. For the purpose of this example, let us pick VS code.
- Install the mocha assertion library. Mocha can be installed using the required commands:
npm install mocha
Install globally:
npm install --global mocha
Install as a dependency:
npm install -- save-dev mocha
Also Read: Unit testing for NodeJS using Mocha and Chai
Implementing Selenium Node.JS Testing
Step 1: Setting up the project and installing dependencies
Create a new folder for your project and run the following command in the terminal. This will create a package.json file inside your project.
npm init
Install the Selenium Webdriver from the CLI with the following command.
npm install selenium-webdriver
After this, let’s install the browser driver. For example, Chrome Webdriver.
npm install chromedriver
Then, install mocha from CLI that will install the required node modules.
npm install mocha
After performing the above installations, let us edit the scripts section in the package.json file.
"scripts": { "test": "mocha --timeout 60000" }
Step 2: Setting up a Node.JS test file using Selenium
Create a new file in the project folder, let’s name it as sample.js and paste the following script inside the file.
var webdriver = require('selenium-webdriver'); function googleSearch(){ var driver = new webdriver.Builder().forBrowser('chrome').build(); driver.get('https://bstackdemo.com').then(function() { driver.findElement(webdriver.By.linkText('Sign In')).click().then(function() { driver.getTitle().then(function(title) { setTimeout(function() { console.log(title); driver.quit(); }, 5000); }); }); }); } googleSearch();
Step 3: Executing test file
If the above test passes, our test will open the Google Chrome browser, navigate to the Browserstack’s demo website, and will click on the ‘Sign in’ menu. After clicking on the Sign in, the test will fetch the title of the Sign in page and will display it in the console.
To execute the test, follow the command.
node sample.js
Best Practices for Node.JS Testing Using Selenium
Node.JS is one of the most used languages to build powerful applications. However, if you start considering some major practices while writing our script, you can further enhance its usability to a greater extent.
Here are some practices if you incorporate could enhance your testing experience.
- Keep tests short: When you keep your Node.JS tests short and precise to the point, you get more concrete results. If one test fails it doesn’t impact the outcome of other tests.
- Use BDD technique: BDD stands for behavior-driven development. BDD supports its integration with Node.JS and is a famous technique that is often used to increase the participation of the contributors to the project. In BDD, the test cases are written in plain English, known as the Gherkin language, thus allowing more people to share ideas while writing tests.
Also Read: How to achieve Advanced BDD Test Automation
- Always keep the latest dependencies: Anything is more stable if it works in synchronization with its surrounding elements. Therefore, if all your dependencies are updated, it keeps the application and its elements stable. For a better testing experience, it is always recommended to keep the track of latest dependencies.
- Selenium Grid: Selenium Grid is used to test the application in multiple operating systems and browsers simultaneously. It measures the exact amount of stability of your application taking all the real-user environments into consideration.
- Test Reports: The idea behind implementing test reports is to keep the track of your test status such as when and how it fails. Setting up reports reduces the time of taking measures for test failures.
- Use Real Device Cloud: It is always better to perform testing on real devices rather than using emulators or simulators. BrowserStack offers a real device cloud, that allows you to test your websites and apps on 3000+ devices, browsers and OS combinations. With BrowserStack you can perform automation tests using not just Selenium but also other popular desktop and mobile testing frameworks like Cypress, Playwright, Puppeteer, Appium, Espresso, and XCUITest.
You can easily debug the websites/apps and view network logs, device logs, app crash logs etc. It offers integration with popular tools like Jira, Slack, GitHub etc. Take a look at all our integrations.