简体   繁体   English

在nightwatch.js中保存控制台消息以进行调试

[英]Save console messages for debugging in nightwatch.js

How can I get all console messages on nightwatch.js debugging? 如何在nightwatch.js调试中获取所有控制台消息?

At phantom it's possible to use page.onError handler. 在幻像中,可以使用page.onError处理程序。 Can I do the same with nightwatch? 我可以用夜视仪做同样的事情吗?

I know about window.onerror but is there way to save all console messages? 我知道window.onerror但有没有办法保存所有控制台消息?

Can anyone share working config / code ? 任何人都可以共享工作配置/代码?

Solution: 解:

module.exports = {
  'Check getting log messages' : function (client) {
    client
      .url('http://jsbin.com/rohilugegi/1/')
      .getLogTypes(function(result) {
        console.log(result);
      })
      .getLog('browser', function(result) {
        console.log(result);
      })
    ;

    return client;
  },

It will give output 它会给出输出

 [Start] Test Suite ================== Running: Check getting log messages [ 'har', 'browser', 'client', 'server' ] [ { message: 'Test error\\n error (:0)', timestamp: 1428447687315, level: 'WARNING' }, { message: 'Test log (:)', timestamp: 1428447687315, level: 'INFO' } ] No assertions ran. 

Commands getLogTypes and getLog are already implemented at client commands but their description are absent at site API 命令getLogTypesgetLog已经在客户端命令中实现,但它们的描述在站点API中不存在

Looks like it worth read source code instead of documentation 看起来值得阅读源代码而不是文档

Below jsdoc for this functions from source code : jsdoc下面的源代码来自这个函数:

/**
 * Gets the available log types
 *
 * ```
 * this.demoTest = function(client) {
 *   this.getLogTypes(function( typesArray ) {
 *     
 *   });
 * };
 * ```
 *
 * @method getLogTypes
 * @param {function} [callback] Optional callback function to be called when the command finishes.
 * @api commands
 * @see logTypes
 */

and

/**
 * Gets a log from selenium
 *
 * ```
 * this.demoTest = function(client) {
 *   this.getLog( 'browser', function( logEntriesArray ) {
 *     console.log( "Log length: " + logEntriesArray.length );
 *     logEntriesArray.forEach( function( log ) {
 *        console.log( "[" + log.level + "] " + log.timestamp + " : " + log.message );
 *      } );
 *   });
 * };
 * ```
 *
 * @method getLog
 * @param {string} typeString Log type to request
 * @param {function} [callback] Optional callback function to be called when the command finishes.
 * @api commands
 * @see log
 */

First make sure you set loggingPrefs to { 'browser' : 'ALL' } in your nightwatch configuration otherwise you won't see entries in the browser console that were written with console.log. 首先确保在nightwatch配置中将loggingPref设置为{'browser':'ALL'},否则您将无法在浏览器控制台中看到使用console.log编写的条目。 My nightwatch.conf.js looks something like: 我的nightwatch.conf.js看起来像:

            desiredCapabilities: {
              browserName: 'chrome',
              handlesAlerts: true,
              loggingPrefs: { 'browser': 'ALL' }
            }

Next, like the others have said you just have to use the built-in getLog function on the client. 接下来,像其他人一样说你只需要在客户端上使用内置的getLog函数。 eg 例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM