【问题标题】:Was not able to run nightwatch.js tests with mocha "Ensure the done() callback is being called"无法使用 mocha 运行 nightwatch.js 测试“确保正在调用 done() 回调”
【发布时间】:2017-02-01 12:43:22
【问题描述】:

我在网络上找到的几乎每一个示例都没有很好地解释应该如何实现 mocha 并将其与 nightwatchJS 一起使用。

无论我做什么,我都无法避免该错误消息,即使我遵循official nightwatch how-to use mocha 的所有步骤 我唯一能做的就是至少打开 google chrome 浏览器,仅此而已。

这是我要运行的代码

var nightwatch = require('nightwatch');

describe('Google', function() {

    var client = nightwatch.initClient({
        // Pass here options from nightwatch.json

        // selenium logs; change it to true if you want to see them in console
        silent : false,

        desiredCapabilities: {
            browserName: "chrome",
            javascriptEnabled: true,
            acceptSslCerts: true
        }
    });

    var browser = client.api();

    // Mocha timeout
    this.timeout(15000);

    it('Demo test Google', function (done) {
        browser
            .url('http://www.google.com')
            .waitForElementVisible('body', 1000)
            .setValue('input[type=text]', 'nightwatch')
            .waitForElementVisible('button[name=btnG]', 1000)
            .click('button[name=btnG]')
            .pause(1000)
            .assert.containsText('#main', 'Night Watch')
            .end();


        client.start(done);
    });
});

这是浏览器弹出后总是出现在我身上的错误信息:

INFO Request: POST /wd/hub/session 
 - data:  {"desiredCapabilities":{"browserName":"firefox","javascriptEnabled":true,"acceptSslCerts":true,"platform":"ANY"}} 
 - headers:  {"Content-Type":"application/json; charset=utf-8","Content-Length":113}
    1) Demo test Google


  0 passing (15s)
  1 failing

  1) Google Demo test Google:
     Error: timeout of 15000ms exceeded. Ensure the done() callback is being called in this test.

【问题讨论】:

    标签: automated-tests mocha.js ui-automation nightwatch.js


    【解决方案1】:

    最初的问题指出,没有关于如何使用 mocha 设置 nightwatch 的优秀文档。这个周末我发现自己就是这种情况,因为我想用摩卡咖啡来设置我的夜间测试。 通过执行以下操作,我能够在没有看到 done 回调错误的情况下设置我的 nightwatch 测试:

    我使用了 nightwatch.conf.js 但我相信您也可以在 nightwatch.json 中执行以下操作:

    module.exports = {
      src_folders : ["test"],
      test_runner: {
        type: 'mocha',
        options: {
          ui: 'bdd',
          reporter: 'spec'
        }
      },
      ...
    }
    

    简单吧? 这允许 mocha 运行您的夜间测试。 更简单、更友好的语法 IMO。

    这是我的 package.json 的相关部分

    {
      "name": "nightwatch-mocha",
      "directories": {
        "test": "test"
      },
      "scripts": {
        "nightwatch": "nightwatch -c ./nightwatch.conf.js"
      },
      "devDependencies": {
        "chromedriver": "^73.0.0",
        "mocha": "^6.1.4",
        "nightwatch": "^1.0.19"
      }
    }
    
    

    我已经安装了 chromedriver,所以我可以通过 chrome 运行测试。

    Mocha 和 Nightwatch 也已安装。

    我在 scripts 对象中创建了一个名为 nightwatch 的脚本。

    当我位于项目的根目录时,当我从命令行运行 npm run nightwatch 时,这将使用 mocha 运行 nightwatch。

    此外,使用 nightwatch.json 或 nightwatch.conf.js 可以将该配置排除在您的测试之外——您不希望在编写新的测试套件时必须初始化它,而您可以这样做一次并且做完了。

    【讨论】:

      【解决方案2】:

      尝试在链的末尾调用done() 回调。我没有完全跟上client.start() 的速度,但我很确定你想在链到达终点时发出测试已完成的信号。

      var nightwatch = require('nightwatch');
      
      describe('Google', function() {
      
          var client = nightwatch.initClient({
              // Pass here options from nightwatch.json
      
              // selenium logs; change it to true if you want to see them in console
              silent : false,
      
              desiredCapabilities: {
                  browserName: "chrome",
                  javascriptEnabled: true,
                  acceptSslCerts: true
              }
          });
      
          var browser = client.api();
      
          // Mocha timeout
          this.timeout(15000);
      
          it('Demo test Google', function (done) {
              browser
                  .url('http://www.google.com')
                  .waitForElementVisible('body', 1000)
                  .setValue('input[type=text]', 'nightwatch')
                  .waitForElementVisible('button[name=btnG]', 1000)
                  .click('button[name=btnG]')
                  .pause(1000)
                  .assert.containsText('#main', 'Night Watch')
                  .end(done);
      
      
              client.start();
          });
      });

      【讨论】:

        猜你喜欢
        • 2017-03-12
        • 1970-01-01
        • 1970-01-01
        • 2015-09-16
        • 1970-01-01
        • 2015-12-16
        • 1970-01-01
        • 1970-01-01
        • 2017-12-25
        相关资源
        最近更新 更多