如何在nightwatch.js中显示console.log?

时间:2018-02-02 21:41:11

标签: javascript nightwatch.js

目前正在尝试显示console.logs。这是我的输出:

 'server', 'browser', 'driver' ]
 √ Element <body> was visible after 115 milliseconds.
[ { level: 'SEVERE',
...

我的测试看起来像这样:

var config = require('../conf.js');

module.exports = {
    'e2e test': function(browser) {
        browser
            .url('http://localhost:3000/')
            .getLogTypes(function(result) {
                console.log(result);
            })

            .waitForElementVisible('body')
            .useXpath()
            .click('/html/body/div/div/div[1]/div/div/div/div/div/section/div[2]/div[3]/div/div/div[4]/div/div/div[1]')
            .pause(5000)
            .click('/html/body/div/div/div[1]/div/section[2]/div/article/header/a')
            .pause(5000)
            .getLog('browser', function(result) {
                console.log(result);
            })
            .end();
    }
};

如何在测试中返回console.log输出?

1 个答案:

答案 0 :(得分:1)

.getLog('browser', function(logEntriesArray) {})将在浏览器的控制台中返回包含当前消息的数组。

要获取所有控制台消息,您可以执行以下操作:

  .getLog('browser', function(logEntriesArray) {
    if (logEntriesArray.length) {
      console.log('Log length: ' + logEntriesArray.length);
      logEntriesArray.forEach(function(log) {
        console.log(
          '[' + log.level + '] ' + log.timestamp + ' : ' + log.message
        );
      });
    }
  });