用mocha和咖啡脚本进行UI测试

时间:2017-05-31 06:26:19

标签: javascript node.js selenium-webdriver coffeescript

我正在Getting started with Selenium WebDriver for node.js

的帮助下设置测试用例环境

我已安装所有依赖项但仍无法运行上述博客中提供的示例测试用例:

selenium = require 'selenium-webdriver'
chai = require 'chai'
chai.use require 'chai-as-promised'
expect = chai.expect

before ->
    @timeout 10000
@driver = new selenium.Builder()
    .withCapabilities(selenium.Capabilities.chrome())
    .build()
@driver.getWindowHandle()

after ->
    @driver.quit()

describe 'Webdriver tutorial', ->
beforeEach ->
    @driver.get 'http://bites.goodeggs.com/posts/selenium-webdriver-nodejs-tutorial/'

it 'has the title of the post in the window\'s title', ->
expect(@driver.getTitle()).to.eventually.contain
'Getting started with Selenium Webdriver for node.js'

it 'has publication date', ->
text = @driver.findElement(css: '.post .meta time').getText()
expect(text).to.eventually.equal 'December 30th, 2014'

it 'links back to the homepage', ->
@driver.findElement(linkText: 'Bites').click()
expect(@driver.getCurrentUrl()).to.eventually.equal 'http://bites.goodeggs.com/'

运行以上测试用例:

mocha JavaScript_test.coffee  --compilers coffee:coffee-script/register

我收到以下错误:

module.js:471
    throw err;
    ^

Error: Cannot find module 'coffee-script/register'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at /usr/local/lib/node_modules/mocha/bin/_mocha:337:3
    at Array.forEach (native)
    at Object.<anonymous> (/usr/local/lib/node_modules/mocha/bin/_mocha:329:19)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我不知道它何时发生变化,但模块咖啡脚本不再存在。 正确的包是 coffeescript 所以正确的命令是:

mocha JavaScript_test.coffee  --compilers coffee:coffeescript/register

但是--compilers is deprecated所以新的正确语法是:

mocha JavaScript_test.coffee  --require coffeescript/register