摩卡无法使用Nightwatch运行

时间:2017-08-31 18:50:08

标签: node.js mocha selenium-chromedriver nightwatch.js

我用nighwatch.js,selenium和chromedriver运行摩卡

我能够使用describe运行它,但是我正在进行动态测试,所以我需要使用以下方式来调用,但是我无法通过这种方式使用nightwatch来运行mocha

"use strict";

var nightwatch = require('nightwatch');

var Mocha = require('mocha');
var Test = Mocha.Test;
var Suite = Mocha.Suite;

var mocha = new Mocha();
var suite = Suite.create(mocha.suite, 'Search Box');

suite.addTest(new Test('I\'m a dynamic test', function (done) {
    var client = nightwatch.initClient({
        silent : true
    });
    var browser = client.api();
    browser.url(browser.launch_url);
    client.start(done);
}));

mocha.run();

这是错误 启动selenium服务器...已启动 - PID:122624

0 passing (0ms)

  Search Box
    1) I'm a dynamic test

There was an error while starting the test runner:

Error: Uncaught, unspecified "error" event. ([object Object])
    at Nightwatch.<anonymous> (E:\gitwork\AAPT-SPZA\src\frontEnd\node_modules\nightwatch\lib\runner\clientmanager.js:66:1
    at HttpRequest.<anonymous> (E:\gitwork\AAPT-SPZA\src\frontEnd\node_modules\nightwatch\lib\index.js:501:10)
    at ClientRequest.<anonymous> (E:\gitwork\AAPT-SPZA\src\frontEnd\node_modules\nightwatch\lib\http\request.js:174:12)

如何解决这个问题,让Mocha与Nightwatch一起运行?

1 个答案:

答案 0 :(得分:0)

我使用了类似下面的格式来使用Mocha在Nightwatch.js中实现动态测试。下面的例子不是真正的测试,所以不要尝试运行它,但它应该让你很好地了解如何去做。测试结果全部显示为具有不同断言的不同测试。除了将mocha设置为测试运行器之外,conf文件应该看起来正常。

const async = require('async');
const languages = [{
    name: 'english',
    currency: '$'
    }, {
    name: 'german',
    currency: '€'
}]; 
describe('An example test', () => {
   async.each(languages, function (language) {
    it(`When I click the ${language.name} link my page should be in ${language.name}`, (browser) => {
        browser.url('http://www.google.com');
        homePage.click(`@${language.name}Link`);
        homePage.expect.element('#sub-title').text.to.equal(`Your language is ${language.name}`);
    });
   });
})

注意:使用async库之类的东西运行异步循环非常重要,nightwatch不会为您异步运行每个异常,这会导致很多错误。< / p>