Forum Discussion

JohanH's avatar
JohanH
Occasional Contributor
9 years ago

Jenkins Environment Variables not available in Tests

  Hi,   I'm using Jenkins to trigger scheduled tests and have recently started to use parameters in Jenkins. The parameters are set as environment variables by Jenkins and can easily be accessed by...
  • JohanH's avatar
    9 years ago

    After much hard work I figured out what the problem is.

     

    We have installed Jenkins on the Slaves using Java Web Start as a service as described in the help section. The service is running as the System user. The TestComplete jenkins plugin is running on a different account (with admin rights). 

     

    This means that any environment variables set up by Jenkins is set in the System user environment and is not accessible to the other users and processes running under those accounts, in this case the TestComplete process...

     

    I did try to run as the same user (i.e changing the user of the Jenkins slave service) and it worked for the environment variables but then I lost the possibility to interact with the desktop... very bad for GUI based application testing.

     

    So in the end we came up with this solution:

     

    1. In Jenkins add a 'Execute Windows batch command' build before the 'TestComplete Test' and write:

      set > %SystemRoot%\Temp\environment-variables-from-jenkins.txt

      Since we cannot access the environment variables we are forced to use a location we think exists in and is accessible by both accounts.

    2. In our Test we have written the following code that reads the file and sets the variables if the do not exist.
      # -------------------------------------------------
      # Read eventual environment variables file to
      # enable Jenkins (or other) to transfer settings
      # by means of environment variables
      # -------------------------------------------------
      env_file = os.path.join(os.getenv('SystemRoot'),
      'Temp', 'environment-variables-from-jenkins.txt') if os.path.exists(env_file): with open(env_file) as f: lines = f.readlines() if lines: for name, value in [line.split('=', 1) for line in lines if line]: if not os.getenv(name): os.putenv(name, value.strip()) os.unlink(env_file)
      Do not forget to remove the file after read! Otherwise environment variables from one test may taint another.

    Hope this may help others that run into the same issue. If you by chance have solved this in another way, please post your findings here.