Nagarjuna
Nagarjuna

Reputation: 43

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?

Upvotes: 1

Views: 7415

Answers (3)

Chris
Chris

Reputation: 1

If you're trying to use an environment variable in Robot, it should be preceded by % instead of $. Robot framework uses $ to refer to variables, and % to refer to environment variables.

So it should look something like this...

Set Environment Variable    %{myVar}    myVal

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 385970

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

Upvotes: 5

pavelsaman
pavelsaman

Reputation: 8322

Does Set Environment Variable replace the existing variable value?

Yes. But it also sets up a new environment variable.

I guess the problem is in ${myVar} which is not set.

Upvotes: 1

Related Questions