You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过命令行传递环境变量运行PyCharm中的Jython自动化测试项目

How to Pass Environment Variables to Jython Scripts via Command Line (For Sikuli/Selenium Test Framework)

Hey there! Let's work through this environment variable issue for your Jython-based automation framework. Since Jython runs on the JVM and you're integrating Sikuli (which has JVM dependencies) and Selenium, here are practical, OS-specific and Jenkins-ready methods to pass environment variables via command line:

1. Direct Command-Line Assignment (OS-Specific)

This is the simplest way for one-off variables:

  • Windows (Command Prompt):
    set SELENIUM_HUB=http://localhost:4444/wd/hub && set SIKULI_IMG_PATH=C:\test\images && jython your_test_script.py
    
  • Windows (PowerShell):
    $env:SELENIUM_HUB="http://localhost:4444/wd/hub"; $env:SIKULI_IMG_PATH="C:\test\images"; jython your_test_script.py
    
  • Linux/macOS:
    SELENIUM_HUB=http://localhost:4444/wd/hub SIKULI_IMG_PATH=/test/images jython your_test_script.py
    

You can stack multiple variables by separating them with spaces (Linux/macOS) or using multiple set commands (Windows).

2. JVM System Properties (Ideal for Jython/Sikuli)

Since Jython runs on the JVM, you can pass variables as JVM system properties with the -D flag—this is especially useful for Sikuli, which relies on JVM-level configurations:

jython -Dselenium.hub=http://localhost:4444/wd/hub -Dsikuli.image.path=/test/images your_test_script.py

To access these in your Jython script:

import java.lang.System
selenium_hub = System.getProperty("selenium.hub")
sikuli_img_path = System.getProperty("sikuli.image.path")

3. Load from a .env File (Scalable for Multiple Variables)

For managing lots of variables (and making it easy to replicate in Jenkins), use a .env file with a Jython-compatible dotenv library:

  1. First install python-dotenv (ensure Jython's pip is configured):
    pip install python-dotenv
    
  2. Create a .env file in your project root:
    SELENIUM_HUB=http://localhost:4444/wd/hub
    SIKULI_IMG_PATH=/test/images
    TEST_ENV=staging
    
  3. Load the variables in your script:
    from dotenv import load_dotenv
    import os
    
    load_dotenv()  # Loads variables from .env
    selenium_hub = os.getenv("SELENIUM_HUB")
    sikuli_img_path = os.getenv("SIKULI_IMG_PATH")
    
  4. Run your script normally—variables will be loaded automatically:
    jython your_test_script.py
    

4. Jenkins-Specific Configuration (Final Deployment)

Since you're targeting Jenkins, here's how to inject variables directly into your build environment:

Freestyle Job

  1. Go to your Jenkins job > Build Environment
  2. Check Inject environment variables to the build process
  3. Add your variables under Properties Content:
    SELENIUM_HUB=http://selenium-grid:4444/wd/hub
    SIKULI_IMG_PATH=/var/jenkins_home/test/images
    
  4. In the build step, just run your Jython script—Jenkins will inject the variables into the environment automatically.

Pipeline Job (Jenkinsfile)

Use the environment block to define variables:

pipeline {
    agent any
    environment {
        SELENIUM_HUB = 'http://selenium-grid:4444/wd/hub'
        SIKULI_IMG_PATH = '/var/jenkins_home/test/images'
    }
    stages {
        stage('Run Automation Tests') {
            steps {
                sh 'jython your_test_script.py'
            }
        }
    }
}

Quick Debug Tip

To verify variables are being passed correctly, add this to the top of your Jython script:

import os
print("Loaded Environment Variables:")
for key, value in os.environ.items():
    print(f"{key} = {value}")

This will print all available variables so you can confirm your target variables are present.

内容的提问来源于stack exchange,提问作者anish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:27:35