When running Nightwatch.js test how can I get the name of browser currently running the tests? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of When running Nightwatch.js test how can I get the name of browser currently running the tests? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Situation: We are running tests in several browsers using Nightwatch
(via Saucelabs; everything runs fine on 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?

Answer

Its quite simple, when running a Nightwatch test, the browser (or client) parameter is passed in, eg:

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:

{ 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

The reason for using ~ (“tilde”) as the word separator was so we can later split on this character in our screenshot viewer. See: https://github.com/dwyl/learn-nightwatch for more detail.

We are here to answer your question about When running Nightwatch.js test how can I get the name of browser currently running the tests? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji