In Robot Framework, you can pass command-line arguments using the --variable option. You can also pass variables through variable files, environment variables, and custom arguments.
1. Pass variables from command line
Robot test file: test.robot
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${URL} https://example.com
${BROWSER} chrome
*** Test Cases ***
Login Test
Log To Console URL: ${URL}
Log To Console Browser: ${BROWSER}Run command:
robot --variable URL:https://google.com --variable BROWSER:firefox test.robotYou can also use short syntax:
robot -v URL:https://google.com -v BROWSER:firefox test.robot2. Pass multiple command-line arguments
robot -v USERNAME:admin -v PASSWORD:admin123 -v ENV:QA test.robotRobot file:
*** Test Cases ***
Login Test
Log To Console Username: ${USERNAME}
Log To Console Password: ${PASSWORD}
Log To Console Environment: ${ENV}3. Pass command-line arguments to a test suite
robot -v ENV:QA -v BROWSER:chrome -v URL:https://example.com tests/This executes all Robot Framework test files inside the tests directory.
4. Pass arguments using spaces
Use --variable with a value containing spaces.
robot -v "USERNAME:Sumit Kumar" test.robot5. Pass arguments using variable files
You can also maintain variables in a Python file.
config.py
ENV = "QA"BROWSER = "chrome"URL = "https://example.com"Robot file:
*** Settings ***
Variables config.py
*** Test Cases ***
Test Configuration
Log To Console ${ENV}
Log To Console ${BROWSER}
Log To Console ${URL}Run:
robot --variablefile config.py test.robot6. Pass environment variables
Windows CMD:
set ENV=QA
set BROWSER=chrome
robot test.robotLinux / macOS:
export ENV=QA
export BROWSER=chrome
robot test.robotRobot file:
*** Test Cases ***
Environment Test
${env}= Get Environment Variable ENV
${browser}= Get Environment Variable BROWSER
Log To Console ${env}
Log To Console ${browser}7. Pass arguments with tags and output directory
You can combine command-line options:
robot -v ENV:QA -i smoke -d results --loglevel DEBUG tests/Option | Description |
|---|---|
| Pass variable |
| Load variables from file |
| Include tests by tag |
| Exclude tests by tag |
| Set output directory |
| Run test by name |
| Run suite by name |
| Set log level |
| Set output XML filename |
| Set report filename |
| Set log filename |
8. Practical example: Environment-based execution
test_login.robot
*** Settings ***
Library SeleniumLibrary
*** Test Cases ***
Login Test
Log To Console Environment: ${ENV}
Log To Console Browser: ${BROWSER}
Log To Console Application URL: ${URL}Execute:
robot -v ENV:QA -v BROWSER:chrome -v URL:https://qa.example.com test_login.robotExpected console output:
Environment: QA
Browser: chrome
Application URL: https://qa.example.com--variable for runtime configuration such as browser, URL, environment, and test data. Use environment variables or a secure secrets manager for sensitive credentials rather than exposing passwords in command history.

No comments:
Post a Comment