简体   繁体   English

在运行Nightwatch.js测试时,如何获取当前正在运行测试的浏览器名称?

[英]When running Nightwatch.js test how can I get the name of browser currently running the tests?

Situation : We are running tests in several browsers using Nightwatch 现状 :我们正在使用Nightwatch在多个浏览器中运行测试
( via Saucelabs; everything runs fine on Saucelabs ). 通过Saucelabs;在Saucelabs上一切正常 )。

Desired : we want to know which browser the test is currently running in so we can save screenshots including the browser name. 所需 :我们想知道测试当前正在运行的浏览器 ,因此我们可以保存包括浏览器名称在内的屏幕截图。

Is it possible to determine which browser is running the tests? 是否可以确定哪个浏览器正在运行测试?

Its quite simple, when running a Nightwatch test, the browser ( or client ) parameter is passed in, eg: 非常简单,在运行Nightwatch测试时,会传入browserclient )参数,例如:

module.exports = {
  'Demo test GitHub': function (browser) {
    console.log(browser.options); // this will output the browser details
    browser
      .url('http://www.github.com/dwyl')   // visit the url
      .waitForElementVisible('body'); // wait for the body to be rendered
      .assert.containsText('body', 'do what you love') // assert contains
      .saveScreenshot('dwyl_github.png')
      .end();
  }
};

The browser Object contains an options Object with the following form: browser对象包含具有以下形式的options对象:

{ screenshots: true,
  screenshotsPath: './node_modules/nightwatch/screenshots/1.0.20/',
  skip_testcases_on_fail: true,
  log_screenshot_data: true,
  username: 'thisguy',
  accessKey: 'notimportant',
  desiredCapabilities: 
   { browserName: 'internet explorer',
     javascriptEnabled: true,
     acceptSslCerts: true,
     platform: 'Windows 10',
     version: '11.0',
     name: 'Github' } }

So we wrote a little helper function to format the name of the browser into a string we could include in the screenshot file name: 因此,我们编写了一个辅助函数,将浏览器的名称格式化为可包含在屏幕快照文件名中的字符串:

function userAgent(browser) { // see: https://git.io/vobdn
  var a = browser.options.desiredCapabilities;
  return (a.platform + '~' + a.browserName + '~' + a.version).replace(/ /g, '');
}

Which is then used as: 然后用作:

module.exports = {
  'Demo test GitHub': function (browser) {
    console.log(browser.options); // this will output the browser details
    browser
      .url('http://www.github.com/dwyl')   // visit the url
      .waitForElementVisible('body'); // wait for the body to be rendered
      .assert.containsText('body', 'do what you love') // assert contains
      .saveScreenshot(userAgent(browser) + '_dwyl_github.png')
      .end();
  }
};

Example filename: Windows10~internetexplorer~11.0~dwyl_github.png 文件名示例: Windows10~internetexplorer~11.0~dwyl_github.png

The reason for using ~ ("tilde") as the word separator was so we can later split on this character in our screenshot viewer. 使用~ (“波浪号”)作为单词分隔符的原因是为了以后我们可以在屏幕截图查看器中对该字符进行split See: https://github.com/dwyl/learn-nightwatch for more detail. 请参阅: https//github.com/dwyl/learn-nightwatch,以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM