如何在摩卡中进行硒测试?

时间:2015-03-21 13:51:06

标签: selenium-webdriver coffeescript mocha

我有一个像这样的摩卡测试:

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()

但是当我运行它时,我有错误:

>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)

版本:

  • 摩卡2.2.1
  • 咖啡脚本1.9.1
  • selenium-webdriver 2.45.1

如何编写可以通过的简单测试?

UPD:我已经通过了,但为每次测试都制作了新的驱动程序:

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()

2 个答案:

答案 0 :(得分:3)

有效的代码超时为10秒。您的原始代码使用2s默认超时。您应该增加原始代码中的超时。

您应该可以通过返回promises来简化代码,而不是使用done

after ()->
  driver.quit();

为了那些不做CoffeeScript的人的利益,上述内容转换为:

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

由于driver.quit()返回一个promise,Mocha将使用此promise来计算after挂钩何时完成。

答案 1 :(得分:0)

尝试使用/ testing区域:

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

则...

test.describe(...)
    it(...)

似乎Mocha需要这个包装器。