0

I'm trying to get full browser console logs in the NightWatch.js test.

This is my test script.

module.exports = {
    'Console Log Test': function (browser) {
        browser
            // Navigate to the test page
            .url(`${environment.API_PATH_LOCAL}/log-test`)

            // Wait for the keypad is loaded
            .waitForElementVisible('ion-button[data-e2e="dial-digit-5"]', 10000)
            .waitForElementVisible('ion-button[data-e2e="dial-digit-2"]', 10000)
            .waitForElementVisible('ion-button[data-e2e="dial-digit-0"]', 10000)
            
            // Wait for the call button is loaded
            .waitForElementVisible('#callBtn', 10000)

            // Click the digits
            .click('ion-button[data-e2e="dial-digit-5"]')
            .click('ion-button[data-e2e="dial-digit-2"]')
            .click('ion-button[data-e2e="dial-digit-0"]')
            .click('ion-button[data-e2e="dial-digit-0"]')

            // Click the call button
            .click('#callBtn')
            
            // Get the full browser console logs
            .getLog('browser', function(logEntriesArray) {
                console.log('Log length: ' + logEntriesArray.length);
                  logEntriesArray.forEach(function(log) {
                      console.log('[' + log.level + '] ' + log.timestamp + ' : ' + log.message);
                  });
              });

        // End the test
        browser.end();
    }
}

This is my nightwatch.conf.js file.

module.exports = {
    src_folders: ["tests"],
    
    webdriver: {
      start_process: true,
      port: 4444,
      server_path: require('chromedriver').path,
      cli_args: []
    },
    
    test_settings: {
      default: {
        launch_url: 'https://nightwatchjs.org',
        desiredCapabilities : {
          browserName : 'chrome',
          'goog:chromeOptions' : {
            w3c: true,
            args: [
              '--no-sandbox',
              '--disable-dev-shm-usage'
            ]
          },
          handleAlerts: true,
          loggingPrefs: { 'browser': 'ALL' }
        }
      }
    }
  };

I also tried this way but didn't get the full browser console log either.

module.exports = {
    'Console Log Test': function (browser) {
        browser
            // Navigate to the test page
            .url(`${environment.API_PATH_LOCAL}/log-test`)

            // Wait for the keypad is loaded
            .waitForElementVisible('ion-button[data-e2e="dial-digit-5"]', 10000)
            .waitForElementVisible('ion-button[data-e2e="dial-digit-2"]', 10000)
            .waitForElementVisible('ion-button[data-e2e="dial-digit-0"]', 10000)
            
            // Wait for the call button is loaded
            .waitForElementVisible('#callBtn', 10000)

            // Click the digits
            .click('ion-button[data-e2e="dial-digit-5"]')
            .click('ion-button[data-e2e="dial-digit-2"]')
            .click('ion-button[data-e2e="dial-digit-0"]')
            .click('ion-button[data-e2e="dial-digit-0"]')

            // Click the call button
            .click('#callBtn')
            
            // Get the full browser console logs
            .captureBrowserConsoleLogs((event) => {
                console.log('event', event)
            })

        // End the test
        browser.end();
    }
}

During the test, there are nearly 300+ logs logged in browser console but I only get 8 logs. How can I get full browser console logs?

4
  • In your last example, have you tried putting the call to captureBrowserConsoleLogs first?
    – J. Titus
    Commented Apr 14 at 3:49
  • I'm sorry but I don't understand you. Could you please provide the code?
    – skaihugger
    Commented Apr 14 at 11:46
  • browser.captureBowserConsoleLogs(...).url(...). etc. This way the console log hook will be registered prior to everything else happening.
    – J. Titus
    Commented Apr 14 at 17:16
  • so should I call captureBrowserConsoleLogs() before this .url(${environment.API_PATH_LOCAL}/log-test) ?
    – skaihugger
    Commented Apr 15 at 0:46

1 Answer 1

0

Thanks, J.Titus. I should have called captureBrowserConsoleLos() before url().

This is the modified code.

module.exports = {
    'Console Log Test': function (browser) {
        browser
            // Get the full browser console logs
            .captureBrowserConsoleLogs((event) => {
                console.log('event', event)
            })
            // Navigate to the test page
            .url(`${environment.API_PATH_LOCAL}/log-test`)

            // Wait for the keypad is loaded
            .waitForElementVisible('ion-button[data-e2e="dial-digit-5"]', 10000)
            .waitForElementVisible('ion-button[data-e2e="dial-digit-2"]', 10000)
            .waitForElementVisible('ion-button[data-e2e="dial-digit-0"]', 10000)
            
            // Wait for the call button is loaded
            .waitForElementVisible('#callBtn', 10000)

            // Click the digits
            .click('ion-button[data-e2e="dial-digit-5"]')
            .click('ion-button[data-e2e="dial-digit-2"]')
            .click('ion-button[data-e2e="dial-digit-0"]')
            .click('ion-button[data-e2e="dial-digit-0"]')

            // Click the call button
            .click('#callBtn')

        // End the test
        browser.end();
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.