Mocha in Selenium in node

Just like how we have JUnit and TestNG testing frameworks in Java world, we have a mocha (https://mochajs.org/) framework in node.js You can use below command to install mocha package.
 
npm install mocha

//Below example shows how to use testing framework in node.js
 

var assert = require(‘assert’);

var webdriver = require(‘selenium-webdriver’),
By = webdriver.By,
until = webdriver.until;

var test = require(‘selenium-webdriver/testing’);

var chrome = require(“selenium-webdriver/chrome”);

//Describe is equivalent to the test class in JUnit
test.describe(‘Verify the title of the Page’, function() {
this.timeout(30*1000);
var driver;

//before is equivalent to the @before annotation in JUnit
test.before(function() {
var options = new chrome.Options();
options.addArguments(“start-maximized”);
options.addArguments(“test-type”);
driver = new webdriver.Builder().withCapabilities(options.toCapabilities()).build();

});

//”it” is equivalent to the @test annotation in JUnit
test.it(‘Title should be equal to Selenium Test Page | Free Software Tutorials’, function() {
driver.get(‘https://www.softpost.org/selenium-test-page/’);
driver.wait(until.titleIs(‘Selenium Test Page | Free Software Tutorials’), 4000);
driver.getTitle().then(function(title) {
console.log(“title is ” + title);
assert(title==’Selenium Test Page | Free Software Tutorials’);
});

});

//after is equivalent to the @after annotation in JUnit
test.after(function() {
driver.quit();
});

});

To run the tests, we can use below syntax.
 
mocha s1.js

We can put all test files in “test” directory and then run all tests in test directory using below command.
 
mocha

You may get below error if you try to run the js file containing mocha tests using node command.
 
node mochatests.js
c:selenium-node
ode_modulesselenium-webdriver	estingindex.js:226
exports.describe.skip = global.describe.skip; ^
TypeError: Cannot read property ‘skip’ of undefined
*/

Complex problems, Simple Solutions