Павел Тявин Павел Тявин - 1 month ago 19
CoffeeScript Question

how to run a selenium test in mocha?

I have a mocha test like this:

selenium = require 'selenium-webdriver'

driver = new selenium.Builder().forBrowser('firefox').build()

after (done)->
driver.quit().then done

describe 'simple test', ->
before (done) ->
driver.get('http://127.0.0.1:8016').then done

it 'should pass this simple test', (done) ->
done()


But when I run it, I've got errors:

>mocha --compilers coffee:coffee-script/register

simple test
1) "before all" hook

2) "after all" hook

0 passing (4s)
2 failing

1) simple test "before all" hook:
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
at [object Object].<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:170:19)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)


2) "after all" hook:
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
at [object Object].<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:170:19)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)


Versions:


  • Mocha 2.2.1

  • Coffee-script 1.9.1

  • selenium-webdriver 2.45.1



How can I write a simple test that can pass?

UPD: I've passed it, but making a new driver for every test:

selenium = require 'selenium-webdriver'

describe 'simple test', ->
@timeout 10000
beforeEach ->
@driver = new selenium.Builder().forBrowser('firefox').build()
afterEach (done)->
@driver.quit().then -> done()

it 'should pass this simple test', (done) ->
@driver.get('http://127.0.0.1:8016').then ->
console.log('done')
done()

Answer

The code that works has a timeout of 10s. Your original code uses the 2s default timeout. You should increase the timeout in the original code.

You should be able to simplify your code by returning promises instead of using done.

after ()->
  driver.quit();

For the benefit of those who don't do CoffeeScript, the above translates to:

after(function() {
  return driver.quit();
});

Since driver.quit() returns a promise, Mocha will use this promise to figure when the after hook is done.