Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js + mocha + webdriverjs: Failed tests kill suite

I am testing a web application using Mocha and WebDriverJS, more or less as decribed here. When the tests pass, everything is fine. However, if one test fails, the rest of the tests in the suite will timeout and the runner will exit at the end of the suite, without closing the Webdriver instance. Example test case:

var assert = require('assert'),
    client = require("webdriverjs").remote({
        logLevel: 'silent'
    });

describe('Self-test', function() {

    before(function(done) {
        client
            .init()
            .url('http://www.wikipedia.org/', function() {
                done();
            });
    });

    after(function(done) {
        client.end(function() {
            done();
        });
    });

    // tests

    it('should fail properly', function(done) {
        client.getTitle(function(result) {
            assert(false, 'This should fail');
            done();
        });
    });

    it('should pass afterwards', function(done) {
        client.getTitle(function(result) {
            assert(true, 'This should still pass');
            done();
        });
    });

});

output:

~> mocha  test/self-test.js

    Self-test
  1) should fail properly
  2) should pass afterwards
  3) "after all" hook

✖ 3 of 2 tests failed:

1) Self-test should fail properly:
   AssertionError: This should fail
    at null.<anonymous> (./test/self-test.js:24:17)
    at QueueItem (./node_modules/webdriverjs/lib/webdriverjs.js:242:15)
    at null.<anonymous> (./node_modules/webdriverjs/lib/commands/getTitle.js:12:6)
    at QueueItem (./node_modules/webdriverjs/lib/webdriverjs.js:242:15)
    at IncomingMessage.WebdriverJs.proxyResponse (./node_modules/webdriverjs/lib/webdriverjs.js:782:6)
    at IncomingMessage.EventEmitter.emit (events.js:115:20)
    at IncomingMessage._emitEnd (http.js:366:10)
    at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
    at Socket.socketOnData [as ondata] (http.js:1366:20)
    at TCP.onread (net.js:402:27)

2) Self-test should pass afterwards:
   Error: timeout of 10000ms exceeded
    at Object.<anonymous> (./node_modules/mocha/lib/runnable.js:158:14)
    at Timer.list.ontimeout (timers.js:101:19)

3) Self-test "after all" hook:
   Error: timeout of 10000ms exceeded
    at Object.<anonymous> (./node_modules/mocha/lib/runnable.js:158:14)
    at Timer.list.ontimeout (timers.js:101:19)

As far as I can tell, this is because the WebDriverJS queue gets stalled when a test fails. Is there any way to fix this? It's sub-optimal even for local command-line testing, and it makes running tests automatically and/or in the background difficult-to-impossible.

Update: I think I can fix the queue failure by instantiating a new client for every test, but this would make things much slower (as the WebDriver instance would need to start up from scratch each time) and would leave WebDriver processes hanging around unkilled on test failure. Ideally, I'd like something like the structure offered by Soda, where a failure somewhere in the queue skips to the end of the queue and then throws the error for the test framework to catch.

like image 786
nrabinowitz Avatar asked Nov 12 '12 22:11

nrabinowitz


1 Answers

You should setup and tear down with beforeEach() and afterEach() instead of before() and after() if each test is dependent upon the state of WebDriverJS queue.

like image 168
srquinn Avatar answered Nov 09 '22 09:11

srquinn