I am trying to access environment variables in Jenkins through Java:
public static WebDriver getJenkinsDriver() throws MalformedURLException {
DesiredCapabilities caps = new DesiredCapabilities();
String url = "";
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
System.out.format("%s=%s%n", envName, env.get(envName));
}
if (System.getenv("SELENIUM_SERVICE").equalsIgnoreCase("saucelabs")) {
caps.setBrowserName(System.getenv("SELENIUM_BROWSER"));
caps.setVersion(System.getenv("SELENIUM_VERSION"));
caps.setCapability(CapabilityType.PLATFORM, System.getenv("SELENIUM_PLATFORM"));
url = "http://ricardo...saucelabs.com:80/wd/hub";
...
}
}
Output of command for:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:JAVA_HOME=/usr/lib/jvm/java-8-oracle/:CLASSPATH=$JAVA_HOME/lib/:CLASSPATH:PATH=$JAVA_HOME/bin/:PATH:$M2_HOME=/usr/share/maven/:M2=$M2_HOME/bin:PATH=$M2:$PATH
XAUTHORITY=/home/ricardoramos/.Xauthority
XMODIFIERS=@im=ibus
XDG_DATA_DIRS=/usr/share/ubuntu:/usr/share/gnome:/usr/local/share/:/usr/share/
GDMSESSION=ubuntu
MANDATORY_PATH=/usr/share/gconf/ubuntu.mandatory.path
TEXTDOMAINDIR=/usr/share/locale/
GTK_IM_MODULE=ibus
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-JQr3Phzwn6
DEFAULTS_PATH=/usr/share/gconf/ubuntu.default.path
XDG_CURRENT_DESKTOP=Unity
SWT_GTK3=0
UPSTART_SESSION=unix:abstract=/com/ubuntu/upstart-session/1000/2230
QT4_IM_MODULE=ibus
SESSION_MANAGER=local/falcon:@/tmp/.ICE-unix/2511,unix/falcon:/tmp/.ICE-unix/2511
LOGNAME=ricardoramos
JOB=dbus
PWD=/home/ricardoramos
IM_CONFIG_PHASE=1
LANGUAGE=pt_BR:pt:en
SHELL=/bin/bash
...
But when you enter the if condition is displayed the message of NullPointerException, I set up the Jenkins as follows:
Must declare the environment variables at the end of .bashrc file or Jenkins himself It is smart enough to share these variables of the "Global properties" with the operating system?
If you are able to use a cloud-based selenium grid to perform your testing I would recommend SauceLabs and Saucery3 as it will take care of all the plumbing for you.
Check out the project page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With