我正在尝试通过Java访问Jenkins中的环境变量:
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";
...
}
}
命令的输出结果:
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
...
但是当您进入if条件时,会显示NullPointerException消息,我设置Jenkins如下:
必须在 .bashrc 文件的末尾或 Jenkins 本身中声明环境变量。Jenkins 聪明地通过“全局属性”与操作系统共享这些变量。