如何获取 nightwatch 控制台中显示的浏览器版本和 OS 信息并在代码中使用?

How to get browser version and OS information shown in nightwatch console and use in code?

从上图中可以看到,当我们启动 运行 nightwatch 时,在控制台中我们可以看到浏览器名称、版本以及 OS 名称和版本。

有没有办法获取版本值并在代码中使用,就像我们过去获取平台一样

process.platform=win32 or darwin

我从https://github.com/nightwatchjs/nightwatch/issues/2445那里得到了答案。

module.exports = {
  'Demo test GitHub': function (browser) {
    console.log(browser); // this will output all the details
    browser
      .url('http://www.google.com')   // visit the url
  }
};
所以浏览器对象提供以下详细信息:

NightwatchAPI {
  capabilities:
   { acceptInsecureCerts: false,
     acceptSslCerts: false,
     applicationCacheEnabled: false,
     browserConnectionEnabled: false,
     browserName: 'chrome',
     chrome:
      { chromedriverVersion:
         '83.0.4103.39 (ccbf011cb2d2b19b506d844400483861342c20cd-refs/branch-heads/4103@{#416})',
        userDataDir:
         '/var/folders/5l/mgjzx80j4pb1trj_zlqrn7cc0000gp/T/.com.google.Chrome.WEyC7u' },
     cssSelectorsEnabled: true,
     databaseEnabled: false,
     'goog:chromeOptions': { debuggerAddress: 'localhost:51082' },
     handlesAlerts: true,
     hasTouchScreen: false,
     javascriptEnabled: true,
     locationContextEnabled: true,
     mobileEmulationEnabled: false,
     nativeEvents: true,
     networkConnectionEnabled: false,
     pageLoadStrategy: 'normal',
     platform: 'Mac OS X',
     proxy: {},
     rotatable: false,
     setWindowRect: true,
     strictFileInteractability: false,
     takesHeapSnapshot: true,
     takesScreenshot: true,
     timeouts: { implicit: 0, pageLoad: 300000, script: 30000 },
     unexpectedAlertBehaviour: 'ignore',
     version: '83.0.4103.116',
     webStorageEnabled: true,
     'webauthn:virtualAuthenticators': true,
     'webdriver.remote.sessionid': '034ce20513343a06554a87cb14d45ce9' },
  currentTest: [Getter],
  desiredCapabilities: null,
  sessionId: '034ce20513343a06554a87cb14d45ce9',
  .
  .
  .
  

因此,如果我们想要浏览器名称、版本、OS 名称等,请使用以下内容:

console.log(browser.capabilities.platform) //prints... Mac OS X
console.log(browser.capabilities.browserName) //prints.. chrome
console.log(browser.capabilities.version) //prints... 83.0.4103.116