20

How can I get all console messages on nightwatch.js debugging?

At phantom it's possible to use page.onError handler. Can I do the same with nightwatch?

I know about window.onerror but is there way to save all console messages?

Can anyone share working config / code ?

Vasiliy Vanchuk
  • 6,280
  • 2
  • 18
  • 39

2 Answers2

22

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

Looks like it worth read source code instead of documentation

Below jsdoc for this functions from source code :

/**
 * 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
 */
Vasiliy Vanchuk
  • 6,280
  • 2
  • 18
  • 39
  • I'm getting an empty log using this technique with Chrome. Which browser are you using? Do you have any suggestions? – David Braun Apr 29 '15 at 04:53
  • This bin was created anonymously and its free preview time has expired - just create another one for test – Vasiliy Vanchuk Apr 29 '15 at 11:50
  • The bin seems fine and I see the console log manually. I'm using the Dockerized Selenium server with Chrome and wonder if I have to tell it to turn on logging some how. – David Braun Apr 29 '15 at 16:56
  • I no longer remember, sorry. – David Braun Jul 10 '16 at 15:11
  • @VasiliyVanchuk - in your example results, you show a log with level "INFO". I can get errors to show this way (using console.error()), but I can't see anything logged with console.log(). How did you get it to show INFO messages for you? Do you know what client-side code you used? – rocketmonkeys Oct 25 '16 at 19:46
  • @VasiliyVanchuk no console.log is displayed. – bier hier Feb 02 '18 at 21:26
  • @bier hier You have to set the loggingPrefs on the selenium web driver desiredCapabilities otherwise it will not show them. See my answer below. – Liviu Trifoi Dec 14 '18 at 13:49
  • Any idea how to make it work in **Firefox** (geckodriver)? – rustyx Oct 08 '19 at 08:08
2

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. My nightwatch.conf.js looks something like:

            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. e.g.

   browser.getLog('browser', function(logEntriesArray) {
      console.log('Log length: ' + logEntriesArray.length);
        logEntriesArray.forEach(function(log) {
            console.log('[' + log.level + '] ' + log.timestamp + ' : ' + log.message);
        });
    });
Liviu Trifoi
  • 2,901
  • 1
  • 18
  • 27