Using Mocha and Selenium

Install Mocha, Chai, Selenium and Chromdriver

Init local Repository

D:\xampp\htdocs\pvdata\testSelenium\test> npm init
..
package name: (test) testselenium
version: (1.0.0)
description: test Selenium and Mocha
entry point: (googlePage.js) test_s5.js
test command:
git repository:
keywords:
author: Helmut
license: (ISC)
About to write to D:\xampp\htdocs\pvdata\testSelenium\test\package.json:

{
  "name": "testselenium",
  "version": "1.0.0",
  "description": "test Selenium and Mocha",
  "main": "test_s5.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Helmut",
  "license": "ISC"
}

Is this ok? (yes)
..

Install local Nodejs Modules

D:\xampp\htdocs\pvdata\testSelenium\test > npm install chromedriver@2.34.1 --save

> chromedriver@2.34.1 install D:\xampp\htdocs\pvdata\testSelenium\test\node_modules\chromedriver
> node install.js

Downloading https://chromedriver.storage.googleapis.com/2.34/chromedriver_win32.zip
Saving to D:\Users\helmut\AppData\Local\Temp\chromedriver\chromedriver_win32.zip
Received 781K...
Received 1568K...
Received 2352K...
Received 3136K...
Received 3222K total.
Extracting zip contents
Copying to target path D:\xampp\htdocs\pvdata\testSelenium\test\node_modules\chromedriver\lib\chromedriver
Done. ChromeDriver binary available at D:\xampp\htdocs\pvdata\testSelenium\test\node_modules\chromedriver\lib\chromedriver\chromedriver.exe
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN testselenium@1.0.0 No repository field.

+ chromedriver@2.34.1
added 96 packages in 14.552s

D:\xampp\htdocs\pvdata\testSelenium\test> npm install selenium-webdriver@3.6.0 --save
npm WARN testselenium@1.0.0 No repository field.

+ selenium-webdriver@3.6.0
added 14 packages in 4.256s

D:\xampp\htdocs\pvdata\testSelenium\test> npm install chai --save
npm WARN testselenium@1.0.0 No repository field.

+ chai@4.1.2
added 7 packages in 2.129s

Verify Local Module Status

D:\xampp\htdocs\pvdata\testSelenium\test> npm  list --depth=0
testselenium@1.0.0 D:\xampp\htdocs\pvdata\testSelenium\test
+-- chai@4.1.2
+-- chromedriver@2.34.1
`-- selenium-webdriver@3.6.0

Install Mocha as a Global Nodejs Module

D:\xampp\htdocs\pvdata\testSelenium\test> npm -g install mocha@4.1
D:\Users\helmut\AppData\Roaming\npm\mocha -> D:\Users\helmut\AppData\Roaming\npm\node_modules\mocha\bin\mocha
D:\Users\helmut\AppData\Roaming\npm\_mocha -> D:\Users\helmut\AppData\Roaming\npm\node_modules\mocha\bin\_mocha
+ mocha@4.1.0
updated 1 package in 4.61s

Verify Global Package Status

D:\xampp\htdocs\pvdata\testSelenium\test> npm  list --depth=0  -g
D:\Users\helmut\AppData\Roaming\npm
+-- eslint@4.16.0
`-- mocha@4.1.0

Testscripts

Script: test_s4.js

require('chromedriver');
var assert = require('chai').assert;
var selenium = require('selenium-webdriver');
var test = require('selenium-webdriver/testing');
var driver;
var GooglePage = require('./googlePage.js');

const timeOut = 15000;
 
test.describe('Testing Google Search Page', function() {
  test.beforeEach(function() {
  	dumpMessage("-> beforeEach Hook");
    this.timeout(timeOut);
    driver = new selenium.Builder()
    	.forBrowser('chrome')
    	.build();
  	var googlePage = new GooglePage(driver);
    googlePage.view();

  });
  
  test.it( 'Working TestCase' , function(){  
    this.timeout(timeOut);
  	driver.getTitle().then(function(title){
  		dumpMessage("Found Page Title: " + title);
  		assert.equal("Google",title );
	});
	dumpMessage("--> Start Processing" ); 
	driver.findElement(selenium.By.name('q')).sendKeys('webdriver');           // Fill in the  Search String for Google Search 
	driver.findElement(selenium.By.name('btnK')).click();					   // Start the Google Search 	
    dumpMessage("--> Leaving Processing" ); 
  });
  
    test.it( 'Failing TestCase' , function(){  
    this.timeout(timeOut);
  	driver.getTitle().then(function(title){
  		dumpMessage("Found Page Title: " + title);
  		assert.equal("xGoogle",title );
	});
	dumpMessage("--> Start Processing" ); 
	driver.findElement(selenium.By.name('q')).sendKeys('webdriver');           // Fill in the  Serach String to the 
	driver.findElement(selenium.By.name('btnK')).click();					   	// Start the Google Search 	
    dumpMessage("--> Leaving Processing" ); 
  });
            
  test.afterEach(function() {
  	  var promise;
  	  var waitTime = 2000;       // Keep the browser window 2 seconds open before calling driver.quit()
  	  this.timeout(10000);       // Increase Timeout ot avoid Error: Timeout of 2000ms exceeded. 
								 // If returning a Promise, ensure it resolves.        
    	promise = new Promise(function(resolve, reject){
        	setTimeout(function(){
           		dumpMessage("-> afterEach Hook Promise called - WaitTime: " +  waitTime );
           		driver.quit();
            	resolve();
        	}, waitTime);
 		});
    	// mocha will wait for the promise to be resolved before exiting
    	return promise;      		
  	dumpMessage("-> afterEach Hook");
  });

});
															 		// Helper Functions 							
function dumpMessage(mesg) {                                        // console.log at top level will create a undefined message
	console.log(getTime() + mesg);                                  // Provide a wrapper function which returns some data 
	return "---------------------------------------------------";	
}

function getTime() {
	var ts = new Date();
    return ts.toLocaleTimeString()+ ":" + ts.getMilliseconds() + "  ";
}

Script: googlePage.js

var webdriver = require('selenium-webdriver');
 
GooglePage = function GooglePage(driver) {
  this.driver = driver;
  this.url = "http://www.google.com/ncr";
};
 
GooglePage.prototype.view = function() {
  this.driver.get(this.url);
  return webdriver.promise.fulfilled(true);
};
 
module.exports = GooglePage;                     // export the module so in an be imported with the require syntax

Running the Mocha/Selenium Testcase

Testcase Overview

  • Testcase consists out of 2 tests !
  • Both tests starts and closes the Chromedriver
  • Testcase 1 should work !
  • Testcase 2 should fail !

Testcase Details

Command: mocha test_s5.js
Image mocha_img4.jpg NOT Found

A more complete Testcase

NetBeans Output

Press F6 in Netbeans to Start Test Scripts
Image mocha_img10.jpg NOT Found

Reference

Leave a Reply

Your email address will not be published. Required fields are marked *