Mocha: A JavaScript-Based Test Framework
Explore how to integrate Selenium WebDriver scripts with the Mocha JavaScript test framework to create structured tests and add assertions. Learn the roles of describe and it blocks in Mocha, and how to perform checks using Node.js assert module to build effective automated browser tests.
We'll cover the following...
Test syntax framework
Selenium scripts are different from Selenium tests. Selenium scripts are just pieces of code for driving the web browsers. On the other hand, Selenium tests drive the web browser as well as maintain a proper structure. In addition to this, Selenium tests provide assertions (performs checks on test scripts). To make effective use of Selenium scripts, we need to put them in the test framework first.
A test syntax framework is a framework that defines the structure of a test and it provides assertions based on that test.
Mocha test framework
Several JavaScript test frameworks are available in the market, and the most popular ones include Mocha, Karma, and Jasmine. For this course, we will use the Mocha test framework. Keep in mind, however, that any other JavaScript test framework would also work fine. Selenium techniques are applicable regardless of the syntax frameworks.
Consider the following Selenium script with Mocha framework (executable here) for user sign-in:
Here, the keywords, describe and it, are provided by selenium-webdriver/testing module, and define the structure of Mocha tests. I have used test.describe and test.it in the above code, which wait for the promise to resolve before resuming the function.
-
test.describeDescription of a collection of related test cases.
-
test.itAn individual test case.
Furthermore, I have used Node.js’ assert module ('assert.equal()') to perform checks in the above code as well.