如何让Nightwatch在记者中展示Chai主张?

时间:2017-12-08 19:27:03

标签: chai nightwatch.js

我使用Nightwatch JS v0.9.16在我的localhost上运行selenium / chai测试。所有的断言都适用于夜班,但是我不能在报告中看到这个断言。

这个问题已经在这里讨论了:https://github.com/nightwatchjs/nightwatch/issues/601但它已被关闭但是没有解决方案......任何人都可以让它工作吗?

这是我的测试:

var assert = require('chai').assert;

module.exports = {
  'pressing the "Servers" tab should change the URL to #servers' : function (client) {
    const pages = client.page,
          home_page = pages.home(),
          servers_page = pages.servers();

    home_page.navigate();
    servers_page.expect.element('body').to.be.present.before(1000);
    client.pause(1000);
    servers_page.click('@nav');
    servers_page.expect.element('@servers_div').to.be.present.before(1000);
    client.url(function(response){
        var currentUrl = response.value.replace(client.launch_url,"");
        console.log("url is ",response.value);

        //***CHAI ASSERTION doesn't get shown on reporter:***
        assert(currentUrl.indexOf('#servers')!=-1);

        client.end();
    });

  }
};

此测试通过时的屏幕截图显示除chai one之外的所有断言:

chai test passed

当它失败时会显示AssertionError: Unspecified AssertionError

chai test failure

这是我的测试设置(nightwatch.json)

{
  "src_folders" : ["tests"],
  "output_folder" : "reports",
  "custom_commands_path" : "",
  "custom_assertions_path" : "",
  "page_objects_path" : "./config/pages/",
  "globals_path" : "./config/globals.js",

  "selenium" : {
    "start_process" : false,
    "server_path" : "./libs/selenium-server-standalone-2-53-1.jar",
    "log_path" : "./logs/",
    "port" : 4444
  },

  "test_settings" : {
    "default" : {
      "launch_url" : "http://localhost:8081",
      "selenium_port"  : 9515,
      "selenium_host"  : "localhost",
      "default_path_prefix" : "",
      "screenshots" : {
        "enabled" : true,
        "path" : "./screens/",
        "on_failure": true
      },
      "desiredCapabilities": {
        "browserName": "chrome",
        "chromeOptions" : {
          "args" : ["--no-sandbox"]
        },
        "acceptSslCerts": true
      }
    }
  }
}

版本:

"selenium-webdriver": "^3.0.1",
"nightwatch": "^0.9.8",
"chromedriver": "^2.25.2",
"chai": "latest"

1 个答案:

答案 0 :(得分:1)

根据文档,assert()将消息视为第二个arg(http://www.chaijs.com/api/assert/),您是否尝试过?如果是的话,是否显示了消息?

或者,您是否尝试过更简单的测试(一个失败,一个失败),例如:assert.isOk(false)& assert.isOk(true)

最后,您是否尝试在client.url()的回调之外运行您的chai断言?

我对Nightwatch并不熟悉,但这就是我的尝试。