Search code examples
robotframework

How to create a new Environment variable if not exist in Robot framework?


I am trying to create a new Environment variable in Robot framework. I used

Set Environment Variable    ${myVar}    myVal

I am getting error as:

Variable '${myVar}' not found

so does "Set Environment Variable" use to replace the existing variable value? If so how to create a new variable which is not defined?


Solution

  • The first argument to Set Environment Variable must be the environment variable name. Environment variables are not named the same as robot variables, they do not use the dollar sign or curly brace.

    It's not clear what environment variable you want to set, but assuming you want to create an environment variable named "MYVAR", you would do this:

    Set environment variable  MYVAR  myVal
    

    You can then reference this environment variable later in your test with robot syntax (eg: %{MYVAR})

    Note: the first argument can be a variable, if that variable itself contains the name of the environment variable. The following two lines do the exact same thing as the code above:

    ${myvar}=  Set variable  MYVAR
    Set environment variable  ${myvar}  myVal