How to run robot tests on different environments like dev, qa, prod?

Hi,

How to run robot tests on different environments like dev, qa, prod? Please help.

Thanks
Sudheer

Hello @sudheer1987

I have been using nested variables. When I need to use specific environment, only able to call with argument.

Variables:


Nested Variable for call environment from Arguments:

open browser ${url_${environment}} ${browser}

Call with any keyword’s arguments

1 Like

Hi @hrnaltnts,

Can you please explain me more elaborately. My scenario is like I have to launch the specific environment URL in the specified browser based on the input we give from command while running the robot test. Please help.

Thanks,
Sudheer

Take a look at the following example:

*** Variables ***
${ENV}    QA
${QA_URL}    https://qa.example.com/
${DEV_URL}    https://dev.example.com/

*** Test Cases ***
Demo
    Log    ${${ENV}_URL}

By changing ENV variable you could change what url used in test cases.

@JaPyR Thanks for explaining. What if I want to switch to dev environment then how I have to pass that value to the test

You could pass it as command line argument:

robot -v ENV:DEV <rest of arguments>

@Thanks a lot. It’s working. Will develop script further. If I have any queries will get back to you.

@JaPyR What If I have to pass different browsers along with the URL as well

Create a variable for BROWSER, similar to ENV

*** Variables ***
${BROWSER}    Chrome
${ENV}    QA
${QA_URL}    https://qa.example.com/
${DEV_URL}    https://dev.example.com/

*** Test Cases ***
Demo
    Log    ${${ENV}_URL}

Then pass the browser you want in the command line

robot -v BROWSER:Edge -v ENV:DEV <rest of arguments>
1 Like

Team, related to this. I wanted to use ${ENV} value in IF condition , If it is QA , do some actions if it is DEV , do some actions. When I am comparing I am getting Name error.
Evaluating IF condition failed: Evaluating expression ‘QA == ‘QA’’ failed: NameError: name ‘QA’ is not defined nor importable as module
Could you please help me in this.
I am getting values into init file using CLI , based on above condition I wanted to perform some action.

Quotes.

IF    '${ENV}' == 'QA'
    Do Something
END
1 Like

Hi , Thank you so much. It worked.

A more convenient way is to create settings dictionary. And get the key to that dict in the env variable.

My go-to is to create YAML files that encapsulate essential differences between environments. If you focus on just the things that change, then your maintenance load will be lower.

I like YAML because you can represent the 3 primitive data types supported - scalar, list, and dictionary. You’ll need to install PyYAML first:

pip install pyyaml

To “slurp up” the variables at runtime, just pass the -V switch, along with the path to the YAML file you want to use (you can use relative or absolute paths - I recommend the former). So, for example, if the environment is ‘QA’ and you want to use the file called qa_env.yaml at the root of your project, then you’d have a command-line string like this:

robot -V qa_env.yaml mytestdir/

Here’s a quick example of what the YAML could look like:

# These are scalars:
base_url: https://qaserver.mycompany.com:8080
admin_user: iAmAdMiN
admin_password: eieioscoobydoo1234
# Now, a list:
my_list:
     - Item 1
     - Item two
     - 3
# Finally, a dictionary:
a_dict:
     key_1: A string
     key_2: 1 # an int

All the data structures are imported at runtime as the “final say” for variable values. As a super-quick example, to assign them to existing variables at runtime, you’d do something like this:

*** Variables ***
${app_url}     ${base_url}
@{the_list}    @{my_list}
&{the_dict}    &{a_dict}

Hopefully that all made sense. For the official word on this, see Variable file as YAML.