Mocha unable to run with Nightwatch

2019-06-02 04:11发布

I'm running mocha with nighwatch.js, selenium and chromedriver

I'm able to run with describe, and it, but I'm making a dynamic tests, so I need use the following way to invoke, but I'm unable to get mocha running this way with nightwatch

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

Here's the error Starting selenium server... started - 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)

How to fix this and get Mocha running with Nightwatch?

1条回答
Melony?
2楼-- · 2019-06-02 04:43

I used a format similar to the below to achieve dynamic tests in Nightwatch.js using Mocha. The below example isn't a real test so don't try to run it but it should give you a good idea of how to go about it. The test results all show up as different tests with different assertions. The conf file should look like it does normally besides setting mocha as the test runner.

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}`);
    });
   });
})

Note: It's extremely important to run an async loop using something like the async library, nightwatch won't run a normal for each asynchronously for you and it will result in a lot of errors.

查看更多
登录 后发表回答