Mocha来自Nightwatch assert.containsText失败

时间:2017-07-06 17:02:41

标签: javascript mocha nightwatch.js

相当新鲜。运行使用来自Nightwatch的Mocha 示例http://nightwatchjs.org/guide#using-mocha,逐字测试 - 它失败了。无法弄清楚原因。

浏览器启动,“守夜人”进入谷歌搜索....

$ npm test

> jbserver@0.0.101 test /var/www/html/3jbserver
> node ./node_modules/nightwatch/bin/nightwatch -c ./test/nightwatch.json ./test/google.test.js

  1) Google demo test for Mocha with Nightwatch uses BDD to run the Google simple test

  0 passing (8s)
  1 failing

  1) Google demo test for Mocha with Nightwatch uses BDD to run the Google simple test:
     Testing if element <#main> contains text: "Night Watch". - Expected "Night Watch" but got: ""
      at Context.<anonymous> (test/google.test.js:30:17)
      at Context.<anonymous> (test/google.test.js:20:7)

npm ERR! Test failed.  See above for more details.

在nightwatch.json中......

  ...
  "test_runner" : {
    "type" : "mocha",
    "options" : {
      "ui" : "bdd",
      "reporter" : "list"
    }
  }
  ...

google.test.js就是给定的例子

...
it('uses BDD to run the Google simple test', function(client) {
  client
    .url('http://google.com')
    .expect.element('body').to.be.present.before(1000);

  client.setValue('input[type=text]', ['nightwatch', client.Keys.ENTER])
    .pause(1000)
    .assert.containsText('#main', 'Night Watch');
});
...

的package.json

  "scripts": {
    "test": "node ./node_modules/nightwatch/bin/nightwatch -c ./test/nightwatch.json ./test/google.test.js"
   ...

使用(npm)selenium-standalone运行。

2 个答案:

答案 0 :(得分:1)

当你使用像#main这样一般的东西(这是google的整个页面,有点多)时,测试往往会中断。 Nightwatch中的文档可能会更好一些。

你可以这样做,看看Nightwatch的工作原理。我使用了更具体的选择器。

.assert.containsText('#rhs_title span', 'Night Watch');

这将选择#rhs_title元素中的span,并检查它是否包含&#39;守夜&#39;。

尽量避免使用.pause,而是使用.waitForElementVisible(time, 'element')。暂停将强制暂停1000毫秒,而.waitForElementVisible将等待最大 1000毫秒,但如果找到该元素,测试将继续。这节省了宝贵的时间。

它将一起看起来像:

.waitForElementVisible(1000, '#rhs_title span')
.assert.containsText('#rhs_title span', 'Night Watch');

答案 1 :(得分:0)

将此作为测试设置

"test_settings" : {
    "default" : {
      "launch_url" : "http://localhost",
      "selenium_port"  : 4444,
      "selenium_host"  : "localhost",
      "test_runner" : {
    "type" : "mocha",
    "options" : {
      "ui" : "bdd",
      "reporter" : "list"
    }
  },
      "desiredCapabilities": {
        "browserName": "chrome",
        "javascriptEnabled": true,
        "acceptSslCerts": true
      }
    }
  }

现在断言

it('uses BDD to run the application test', function(client) {
      client
        .url('url')
        .expect.element('body').to.be.present.before(1000);

      client.setValue('input[type=text]', ['ranjan', client.Keys.ENTER])
        .pause(1000)
        .assert.containsText('#main', 'ranjan');
    });

此代码为我醒来,请将网址更改为谷歌