How do I use environment variables in Jenkins in Java?

I am trying to access environment variables in Jenkins via 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";
            ...
        }
    }

      

Command output 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 there is a NullPointerException error, I set up Jenkins like this:

enter image description here

enter image description here

Do you need to declare environment variables at the end of your .bashrc file or is Jenkins himself smart enough to share these "Global Properties" variables with the operating system?

+4


source to share


1 answer


If you can use a cloud selenium mesh for testing, I would recommend SauceLabs and Saucery3 as it will take care of all the plumbing for you.



Check out the project page .

0


source







All Articles