Open sirLisko opened 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.
@sirLisko may be help
browser.options.desiredCapabilities.browserName
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.
@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.
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. :(
I've tried but I didn't see __NIGHTWATCH_ENV_KEY on my process.env.
Are you on the latest version?
Yep, nightwatch v0.5.36
.
@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
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
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 .
+1 - need to get environment name due to phantom behaving badly with some tests which otherwise pass on FF and Chrome. any known workarounds?
@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
.
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.
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.
@digggggggggg are you sure my trick is not working? I've just double checked with 3 browsers simultaneously and it works.
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
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.
process.env.__NIGHTWATCH_ENV_KEY
is not defined for me too, using parallel testing.
browser.options.desiredCapabilities.browserName
does work though.
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
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;
}
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