如何通过命令行传递环境变量运行PyCharm中的Jython自动化测试项目
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:
- First install
python-dotenv(ensure Jython's pip is configured):pip install python-dotenv - Create a
.envfile in your project root:SELENIUM_HUB=http://localhost:4444/wd/hub SIKULI_IMG_PATH=/test/images TEST_ENV=staging - 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") - 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
- Go to your Jenkins job > Build Environment
- Check Inject environment variables to the build process
- Add your variables under Properties Content:
SELENIUM_HUB=http://selenium-grid:4444/wd/hub SIKULI_IMG_PATH=/var/jenkins_home/test/images - 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

