Search code examples
javascriptnode.jsseleniumwebstorm

setTimeout function is quicker than it should be


I have the following function in selenium test and setTimeout function is always done about 25% faster than it should be. In this case want to wait 20 seconds and function is done after 15 seconds.

test.describe('basic login test',function(){
    this.timeout(timeout);
    // variables
    test.before(...);

    test.it.only('Test', function(done){
        testLoginPage.load().then(...)
        .then(...).then(...)
        .then(...).then(...)
        .then(function(){
            var first = "1st: " + new Date().getTime();
            console.log(first);    
            setTimeout(function(){
                driver.getTitle().then(function(title){
                    assert.equal(title, 'Tittle', 'Error.');
                });
                console.log(first);
                console.log("2nd: " + new Date().getTime());
                done();
            }, 20000);
       }).then(...)
    });
    test.after();
});

Output:

1st: 1457706590459
1st: 1457706590459
2nd: 1457706605462

Solution

  • Impossible, except for the following cases.

    • Your code or library you are using, has a overridden customized setTimeout function and it is calling actual setTimeout function with lesser milliseconds.

    • You are using a JavaScript runtime (very old), with a VERY VERY CRITICAL issue.

    • Some other function is printing it in between (doesn't look like that from your code though)