Hello Everyone! This is my First blog post on Functional Testing For Node.js Using Mocha and Zombie. I was Doing Functional Testing using mocha with zombie. it was too fast for functional testing because Zombie.js is a lightweight framework for testing client-side JavaScript code in a simulated environment. No browser required.

For functional testing using zombie we need the NPM (node package manager) and run this command:

sudo npm install -g zombie

From this command your zombie would be install and you can run test using zombie.

Let’s try search jellyfishtechnology in google and assert the title of http://www.jellyfishtechnologies.com/ and see what happens:

var Browser = require("zombie");
var assert = require('assert');
var browser = new Browser();

browser.visit('https://www.google.co.in', {debug: true}, function () {
    browser.fill("q", 'jellyfishtechnologies').pressButton("Google Search",
function () {
        browser.clickLink(".r a", function () {
            assert.equal(browser.text("title"), "JellyFish Technologies | Groovy, Grails, Node.js, AngularJS, Grails India, Node.js India, AngularJS  IndiaJellyFish Technologies | Groovy, Grails, Node.js, AngularJS, Grails India, Node.js India, AngularJS India");
            browser.viewInBrowser();
            onsole.log("**done**");

        });

    });

});

In this Example require a modules of zombie and assert, zombie make’s virtual browser and I tried explain in some steps that is:

  • var browser = new Browser() this is the syntax of virtual browser.
  • In my example I used visit browser.visit(“https://www.google.co.in”,callback) for visiting any site, in which the visit function taking two arguments as you showing in above example first one is url of the site but I used https://www.google.co.in and second one is callback function.
  • In this step browser.fill(“q”,’jellyfishtechnologies’) I picked textbox field name of google q and along with which you want to enter in text-box of google but I used jellyfishtechnologies.
  • zombie provide pressButton(“name of button or input[type=submit]”,callback) ,used in my example and google will search jellyfishtechnologies this keyword.
  • In this step, whenever press in google button pressButton(“name of button or input[type=submit]”,callback) so lots of links would be open related to jellyfishtechnologies then I want click on jellyfishtechnoligies link so I used clickLink(“name of link”,callback).
  • the final step is assert the title if the title would be match then showing on console “**Done**” if you want to do assert that document load or not, use browser.assert.success().

Run this code

node FILE-NAME.js

If you want to see in web view,I have to explained already in example, zombie provide viewInBrowser() you can use it and lots of inbuild function provided by the Zombie like visit,wait,back etc, for more detail you can follow this link http://zombie.labnotes.org/ API.

If you want to run multiple test case at a time, we will use “Mocha”. Mocha is a feature-rich JavaScript test framework running on node.js and the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.

Run this command for install mocha:

sudo npm install -g mocha

Lets try zombie with mocha and see what happen:


var Browser = require("zombie");
var assert = require('assert');
var browser = new Browser();

describe("google page", function () {
    it("assert the title of jellyfishtechnology from google search", function (done) {
        browser.visit('https://www.google.co.in', {debug: true}, function () {
            browser.fill("q", 'jellyfishtechnologies').pressButton("Google Search", 
function () {
                browser.clickLink(".r a", function () {
                    assert.equal(browser.text("title"), "JellyFish Technologies | Groovy, Grails, Node.js, AngularJS, Grails India, Node.js India, AngularJS IndiaJellyFish Technologies | Groovy, Grails, Node.js, AngularJS, Grails India, Node.js India, AngularJS India");

                    browser.viewInBrowser();
                    console.log("**done**");
                    done();

                });

            });

        });
    });
});

there I used only two things extra that is describe(“module name”,callback) and it(“test case”,callback).if you want to run this code follow this command:

mocha -R spec file-name.js -t 50s

-R: The –reporter option allows you to specify the reporter.
spec: The “spec” reporter outputs a hierarchical view nested just as the test cases are.
-t: Specifies the test-case timeout.