Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving environment variable with Robot

According to the docs here

Get Environment Variable    (name, default=None)    
Returns the value of an environment variable with the given name.
If no such environment variable is set, returns the default value, if given. Otherwise fails the test case.
Returned variables are automatically decoded to Unicode using the system encoding.
Note that you can also access environment variables directly using the variable syntax %{ENV_VAR_NAME}.

I should be able to use

${env_var}= Get Environment Variable STAGING_SERVER Log To Console ${env_var}

But Robot keeps complaining that there's no such variable. But when I flip over to my terminal and run echo $STAGING_SERVER I get desired output.

What can I do ?

like image 969
Mark Avatar asked Oct 29 '25 22:10

Mark


2 Answers

although you pasted docu for "Get Environment Variable" from "OperatingSystem" library you missed a tiny but helpful information:

.... access environment variables directly using the variable syntax %{ENV_VAR_NAME}

So you can easily adapt that in your robot code (eg STAGING_SERVER)

${env_var}=  %{STAGING_SERVER}
Log To Console  ${env_var}
like image 169
domi27 Avatar answered Nov 02 '25 07:11

domi27


The variable is not set in the terminal you are using. See this example in Windows command window:

C:\Testes>echo %STAGING_SERVER%
%STAGING_SERVER%

C:\Testes>robot -t env_var Example.robot
==============================================================================
Example
==============================================================================
env var                                                               | FAIL |
Environment variable 'STAGING_SERVER' does not exist.
------------------------------------------------------------------------------
Example                                                               | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================
Output:  C:\Testes\output.xml
Log:     C:\Testes\log.html
Report:  C:\Testes\report.html

C:\Testes>set STAGING_SERVER="The variable is set"

C:\Testes>robot -t env_var Example.robot
==============================================================================
Example
==============================================================================
env var                                                               ."The variable is set"
env var                                                               | PASS |
------------------------------------------------------------------------------
Example                                                               | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output:  C:\Testes\output.xml
Log:     C:\Testes\log.html
Report:  C:\Testes\report.html

C:\Testes>echo %STAGING_SERVER%
"The variable is set"

For completeness, this is the test suite:

*** Settings ***
Library           OperatingSystem

*** Test Cases ***
env var
    ${env_var}=    Get Environment Variable    STAGING_SERVER
    Log To Console    ${env_var}

like image 21
Helio Avatar answered Nov 02 '25 08:11

Helio