Search code examples
javascriptnode.jstestinge2e-testingnightwatch.js

I can't get full chrome browser console logs in the NightWatch.js test


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?


Solution

  • 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();
        }
    }