How to Test Your JavaScript with Selenium WebDriver and Mocha

This article was originally published on TestProject.
In case you are looking to write a functional test in JavaScript, the following tutorial provides UI automation engineers with the perfect structural reference material for JavaScript testing with Selenium WebDriver 3, Mocha and NodeJS.
These days, JavaScript is a ubiquitous web language which seems to overcome its ‘notorious’ past and has become a more solid platform not only for client-side, but for server domains too. Mocha.js, or simply Mocha, is a feature-rich JavaScript test framework running on Node.js, which provides the platform and the API for building standalone applications server-side using Google’s V8 JavaScript engine at its base.
*Note: to get started with this JavaScript tutorial, you’ll need to be familiar with the basics of NodeJS and the JavaScript programming language.
Tutorial Overview:
1. Mocha Test Construction

Introduction
Installation
Installing Chai Assertion Module
Test suite and Test Case Structure
Constructing Tests with Mocha
Running Mocha’s Test Suite and Test Cases
Managing Syncing of Async Testing Code

2. Using Javascript Selenium 3 API Integrated with MochaJS

Selenium Introduction
Selenium Installation
WebDriver Construction
Integrating MochaJS with Selenium WebDriver 3

Versions used:

Node version used: 6.10.1 (LTS)
Mocha: 2.5.3
WebDriverJS: 3.3.0

1. Constructing Tests with Mocha
Introduction to Mocha
As mentioned, Mocha is a JavaScript test framework that runs tests on Node. Mocha comes in the form of a Node package via npm, allowing you to use any library for assertions as a replacement to Node’s standard ‘assert’ function, such as ChaiJS. In addition, Mocha has several similar components to Jasmine, another popular test automation framework which we’ve mentioned in our research for Front End and Unit Test Automation Trends.
Mocha provides an API, which specifies a way to structure the testing code into test suites and test case modules for execution, and later on to produce a test report. Mocha provides two modes for running: either by command line (CLI) or programmatically (Mocha API).
Install Mocha
If Mocha is to be used in CLI, then it should be installed globally as Node.js.
npm install -g mocha

Install Chai Assertion Module
npm install –save chai

The –save option is used to install the module in the project’s scope and not globally.
Test Suite and Test Case Structure
In Mocha, a test suite is defined by the ‘describe’ keyword which accepts a callback function. A test suite can contain child / inner test suites, which can contain their own child test suites, etc. A test case is denoted by the ‘it’ function, which accepts a callback function and contains the testing code.
Mocha supports test suite setup and test case setup functions. A test suite setup is denoted by before while a test case setup applies beforeEach. beforeEach is actually a common setup for every case in the suite, and will be executed before each case.
As with the setup, Mocha supports test suite and test case teardown functions. A test suite teardown is denoted by after, while a test case teardown is implemented with afterEach, functions that are executed after a test suite and after each test case respectively.
Create a file that will ‘host’ the test suite, e.g. test_suite.js, and write the following to it;
describe(“Inner Suite 1", function(){

before(function(){

// do something before test suite execution
// no matter if there are failed cases

});

after(function(){

// do something after test suite execution is finished
// no matter if there are failed cases

});

beforeEach(function(){

// do something before test case execution
// no matter if there are failed cases

});

afterEach(function(){

// do something after test case execution is finished
// no matter if there are failed cases

});

it("Test-1", function(){

// test Code
// assertions

});

it("Test-2", function(){

// test Code
// assertions

});

it("Test-3", function(){

// test Code
// assertions

});

});

Running Mocha Test Suite and Test Cases
Mocha supports execution of tests in three ways: Whole Test Suite file, tests filtered by “grep” patterns and tests grep filtering looking in a directory tree (recursive option)
Run whole Test Suite file:
mocha /path/to/test_suite.js

Run a specific suite or test from a specific suite file.
If a suite is selected then all the child suites and/or tests will be executed.
mocha -g “Test-2” /path/to/test_suite.js

Run a specific suite or test file by searching recursively in a directory tree.
mocha –recursive -g “Test-2” /directory/

For extensive CLI options:
mocha –-help

Managing Syncing of Async Testing Code
In case async functions are used with Mocha and not handled properly, you may find yourself struggling. If asyncing code (e.g. http requests, files, selenium, etc.) is to be used in a test case, follow these guidelines to overcome unexpected results:
Continue reading %How to Test Your JavaScript with Selenium WebDriver and Mocha%

Link: https://www.sitepoint.com/how-to-test-your-javascript-with-selenium-webdriver-and-mocha/