运行Nightwatch.jstesting时如何获得当前运行testing的浏览器名称?

情况 :我们使用Nightwatch在几个浏览器上运行testing
通过Saucelabs;一切运行良好Saucelabs )。

期望的 :我们想知道哪个浏览器 正在运行testing,所以我们可以保存截图,包括浏览器名称。

是否有可能确定哪个浏览器正在运行testing?

它非常简单,当运行Nightwatchtesting时,传入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(); } }; 

browser对象包含以下forms的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' } } 

所以我们写了一个辅助函数来把浏览器的名字格式化为一个string,我们可以在截图文件名中包含这个string:

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

然后用作:

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

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

使用~ (“tilde”)作为单词分隔符的原因是我们稍后可以在我们的截图查看器中对这个字符进行split 。 有关更多详细信息,请参阅: https : //github.com/dwyl/learn-nightwatch 。