我无法在 NightWatch.js 测试中获取完整的 Chrome 浏览器控制台日志

问题描述 投票:0回答:1

我正在尝试在 NightWatch.js 测试中获取完整的浏览器控制台日志。

这是我的测试脚本。

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

这是我的

nightwatch.conf.js
文件。

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' }
        }
      }
    }
  };

我也尝试过这种方式,但也没有获得完整的浏览器控制台日志。

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

在测试过程中,浏览器控制台中记录了近 300 多个日志,但我只收到了 8 个日志。如何获取完整的浏览器控制台日志?

javascript node.js testing e2e-testing nightwatch.js
1个回答
0
投票

谢谢,J.Titus。 我应该在

captureBrowserConsoleLos()
之前打电话给
url()

这是修改后的代码。

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();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.