1
votes

Am new to Javascript ,node.js and mocha. as i was looking to basic example i encountered an empty function "function()". What's the ppurpose of using empty function.

var assert = require('assert'),
var test = require('selenium-webdriver/testing'),
var webdriver = require('selenium-webdriver');

test.describe('Google Search', function() {
  test.it('should work', function() {
    var driver = new webdriver.Builder().
    withCapabilities(webdriver.Capabilities.chrome()).
    build();

    driver.get('http://www.google.com');
    var searchBox = driver.findElement(webdriver.By.name('q'));
    searchBox.sendKeys('simple programmer');
    searchBox.getAttribute('value').then(function(value) {
      assert.equal(value, 'simple programmer');
    });
    driver.quit();
    done();
  });
});

when i run this sample code i got below error , can u please help me resolving this issue please .

error : Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

as i see am able to launch browser and open google.com as writted in program but i see fail still

Google Search 1) should work

0 passing (2s) 1 failing

1) Google Search should work: Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test. at null. (C:\Users\kashyap\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:170:19) at Timer.listOnTimeout (timers.js:110:15)

can you please also suggest me some links to know and try mocha+nodejs with selenium

1
You should definitely accept the provided answer. - alecxe

1 Answers

11
votes

Your function are not empty. In fact, in javascript, there are 2 ways to define a function :

function myFunction (arg1, arg2){
    //Do some stuff
}

or

var myFunction = function(arg1, arg2){
    //Do some stuff
}

In both cases, the function is stored in the variable called "my function" (with different scopes, though).

So basically, in javascript, a function is a value. You may draw a parallel with functional pointers in c, to understand the way it works.

Now, what mocha does, is getting the test process as a parameter of the function describe, and the test itself as a parameter of the function it (you can set initiliazing/cleaning processes with before/afer functions as well).

We can now see what the "done" function is. The done function (you can give it the name you want, done is just the most common name) is the function you should call when your test is over. It's given as a parameter of the function that defines the process you're in. Quick example :

it('should work', function(done){
    //do some testing
    done();
});

does the same thing as :

it('should work', function(){
    //do some testing
});

But where that parameter is useful is when you're testing asynchronous functions. For example, I want to test a query in a database, which is not instant.

it("should work", function(){
    dbDriver.get('key1', function(data){
        assert(data == goodValue);
    });
});

The previous example will not work properly, cause the db request is not instantaneous and is an asynchronous call, which mean that my test will keep running, and my assertion will happen after we left the test section. So mocha will think that everything was ok, and then will ignore my assertion afterwards.

To allow asynchronous function testing, mocha designed the done parameter, which makes the test to keep on running while the parameter function is not called, so, if I want my test to run correctly, I have to do it like this :

it("should work", function(done){
    dbDriver.get('key1', function(data){
        assert(data == goodValue);
        done();
    });
});

Now, mocha will be stuck in this test while the done function is not called. And, it will be called once the callback of the get function is called and the assertion is made.

Now, let's look at the code. First of all, your call to done is uncalled (...) for. Done is not given as a parameter of any of your callbacks, so it should be undefined, and, if that line was executed, you would simply get a syntax error.

Now, basically, what's happening in your code is that a function doesn't respond fast enough, so mocha considers that the test has timed out (default value is 2000 ms).

Basically, you should start testing adding the function one by one in your process to check which one is timing out, to then be able to reduce your problem to the timed out function only.

If you want to learn mocha, the official website of the framework is pretty good : http://mochajs.org/, and presents some good practice (like using before and after).

I hope I wasn't too long and that I could help you understanding mocha and javascript better.