Saturday, November 21, 2026

Robot Framework command-line arguments using the --variable option

 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.

In Robot Framework, you can pass command-line arguments using the --variable option.


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.robot

You can also use short syntax:

robot -v URL:https://google.com -v BROWSER:firefox test.robot

2. Pass multiple command-line arguments

robot -v USERNAME:admin -v PASSWORD:admin123 -v ENV:QA test.robot

Robot 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.robot

5. 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.robot

6. Pass environment variables

Windows CMD:

set ENV=QA
set BROWSER=chrome
robot test.robot

Linux / macOS:

export ENV=QA
export BROWSER=chrome
robot test.robot

Robot 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

-v

Pass variable

--variablefile

Load variables from file

-i

Include tests by tag

-e

Exclude tests by tag

-d

Set output directory

-t

Run test by name

-s

Run suite by name

-L

Set log level

-o

Set output XML filename

-r

Set report filename

-l

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.robot

Expected console output:

Environment: QA
Browser: chrome
Application URL: https://qa.example.com

Best practice
Use --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