nightwatchjs / nightwatch

Integrated end-to-end testing framework written in Node.js and using W3C Webdriver API. Developed at @browserstack
https://nightwatchjs.org
MIT License
11.8k stars 1.31k forks source link

Get the env name in the test #365

Open sirLisko opened 9 years ago

sirLisko commented 9 years ago

Hello,

Forgive me for the stupid question, but is it possible to get the name of the current env in the tests? Ideally I'd like to avoid to add a global for something that i've already defined.

Thanks a lot, Luca

beatfactor commented 9 years ago

there is process.env.__NIGHTWATCH_ENV_KEY.

On Wed, Feb 18, 2015 at 8:06 PM, Luca Lischetti notifications@github.com wrote:

Hello,

Forgive me for the stupid question, but is it possible to get the name of the current env in the tests? Ideally I'd like to avoid to add a global for something that i've already defined.

Thanks a lot, Luca

— Reply to this email directly or view it on GitHub https://github.com/beatfactor/nightwatch/issues/365.

andymost commented 9 years ago

@sirLisko may be help browser.options.desiredCapabilities.browserName

sirLisko commented 9 years ago

Thanks guys for the quick reply.

@beatfactor I've tried but I didn't see __NIGHTWATCH_ENV_KEY on my process.env.

@andymost browsername is working. Although I'm not sure it's the best way to do it. Reading the Selenium documentation it seems that has to be the name of a real browser and not a custom string.

andymost commented 9 years ago

@sirLisko you are right it isn`t real browser name, it is enviroment name that you create in nightwatch.json and where you run tests, but I am not sure.

sirLisko commented 9 years ago

Looking at the _userAgent_s passed to the test I'm not sure but I'm guessing that if you use a browsername not supported Selenium is falling back on chrome. So unfortunately @andymost your "trick" is working only if you are happy using chrome in your different envs. :(

beatfactor commented 9 years ago

I've tried but I didn't see __NIGHTWATCH_ENV_KEY on my process.env.

Are you on the latest version?

sirLisko commented 9 years ago

Yep, nightwatch v0.5.36.

andymost commented 9 years ago

@sirLisko maybe you right about browser that not supported Selenium, but it correct works with PhantomJS and firefox too. I use it to identificate browser, and if it phantom take a screenshot to compare it

davidlinse commented 9 years ago

I've tried but I didn't see __NIGHTWATCH_ENV_KEY on my process.env.

I can confirm this for locally installed as well as for globally installed nightwatch Neither in before nor in the test itself there i can't find the __NIGHTWATCH_ENV_KEY.

~david

beatfactor commented 9 years ago

right, I was mistaken. It is only populated when running in parallel mode.

On Thu, Feb 19, 2015 at 7:11 PM, David Linse notifications@github.com wrote:

I've tried but I didn't see __NIGHTWATCH_ENV_KEY on my process.env. I can confirm this for locally installed as well as for globally installed nightwatch

Neither in before nor in the test itself there i can't find the __NIGHTWATCH_ENV_KEY.

~david

— Reply to this email directly or view it on GitHub https://github.com/beatfactor/nightwatch/issues/365#issuecomment-75093265 .

GrayedFox commented 9 years ago

+1 - need to get environment name due to phantom behaving badly with some tests which otherwise pass on FF and Chrome. any known workarounds?

sirLisko commented 9 years ago

@GrayedFox What I'm using at the moment is defining an env variable into the globals of an environment and then I access it in the test with this.globals.env.

GrayedFox commented 9 years ago

Ah... that's elegant. :sunglasses:

I have always been a bit confused as to how the environment based globals works however? I already have a globals file I use for all tests specified under globals_path which works well. I modified my nightwatch.json to look like this:

    "phantom": {
      "desiredCapabilities": {
        "browserName": "phantomjs",
        "javascriptEnabled": true,
        "acceptSslCerts": true,
        "phantomjs.binary.path": "node_modules/.bin/phantomjs",
        "globals": {
          "env": "phantom"
        }

Problem is, even when running tests with phantom, browser.globals.env is undefined at runtime.

GrayedFox commented 9 years ago

Found a solution here :+1: that works just fine

digggggggggg commented 9 years ago

I've been having the same issue (process.env.__NIGHTWATCH_ENV_KEY is undefined everywhere even though it's mentioned in the blog here).

@sirLisko's suggestion won't work for me because the global var would be fixed when testing multiple browsers simultaneously, like nightwatch -e firefox,chrome,safari.

So in case this helps anyone, my solution was to call browser.options.desiredCapabilities.browserName within the test.

sirLisko commented 9 years ago

@digggggggggg are you sure my trick is not working? I've just double checked with 3 browsers simultaneously and it works.

eric314 commented 7 years ago

Is there a way to have __NIGHTWATCH_ENV_KEY always defined? Even in "parallel mode", which I'm not convinced I understand, this variable is consistently undefined.

Nightwatch v0.9.14

ninebelowzero commented 6 years ago

As of Nightwatch 0.9.16, process.env.__NIGHTWATCH_ENV_KEY is not defined. Has this variable moved somewhere else?

I'd like to access it in my nightwatch.conf.js, so the other solutions listed here (e.g. browser.globals) aren't going to work.

maxgalbu commented 6 years ago

process.env.__NIGHTWATCH_ENV_KEY is not defined for me too, using parallel testing. browser.options.desiredCapabilities.browserName does work though.

RobotRogue commented 5 years ago

This may or may not be what you're looking for:

So what I've been using is process.argv[x] to get the environment value and pass it into my test.

For my command to run nightwatch: nightwatch --env local-headless --group authentication

So in my test file, I use: browser.outputComment('Test environment: ' + process.argv[3]); (outputComment is a custom command that basically is a fancy version of console.log)

Which outputs: "Test environment: local-headless" to the nightwatch console output while the tests are running.

More details about process.argv here: https://nodejs.org/docs/latest/api/process.html#process_process_argv

LiviuTrifoi commented 5 years ago

Agree with @RobotRogue Assuming that one would run nightwatch tests using a command like:

nightwatch --config nightwatch.conf.js --env chrome or nightwatch --config nightwatch.conf.js -e chrome

It's trivial to get the environment name:

function testEnvName() {
    var idx = process.argv.findIndex(arg => arg === "-e" || arg === "--env");

    return idx > -1 ? process.argv[idx + 1] : undefined;
}