Note: You are looking at a static snapshot of documentation related to Robot Framework automations. The most recent documentation is at https://robocorp.com/docs

Using environment variables to configure your robots

Environment variables can be used to make your robots more dynamic and configurable.

Consider this very simple robot that takes a screenshot of the Google front page:

*** Settings *** Documentation Simple robot that takes a screenshot of the Google front page. Library RPA.Browser.Selenium Suite Teardown Close All Browsers *** Tasks *** Open the given website and take a screenshot Open Available Browser http://www.google.com Capture Page Screenshot

What if you wanted to take a screenshot of another page? At the moment, you would need to modify the URL in the code manually. Then, you would probably upload the updated robot again to Control Room, and repeat this process each time.

That's clearly not the best way to go!

How can we make the robot "smarter", so that we can pass it the URL to take a screenshot to dynamically?

One possible way is to use an Environment variable so that the robot can read the value of the URL from the context in which it is run.

Let's modify the robot code like so:

*** Settings *** Documentation A simple robot that takes a screenshot of a given webpage, ... taking the URL from an environment variable. Library RPA.Browser.Selenium Suite Teardown Close All Browsers *** Variables *** ${URL} %{WEBSITE_URL} *** Tasks *** Open the given website and take a screenshot Open Available Browser ${URL} Capture Page Screenshot

Let's look at the changes we made:

  1. We changed the description of the robot in the *** Settings *** to explain better what the robot does.
  2. We added a *** Variables *** section:
*** Variables *** ${URL} %{WEBSITE_URL}

Here we are creating a local variable that we call ${URL}, to which we assign the value of the WEBSITE_URL environment variable.

Robot Framework uses the ${} syntax to refer to variables, and %{} to refer to environment variables.

  1. We are using the value of the variable when calling the Capture Page Screenshot keyword.

Now our robot can be configured to take screenshots of any web page!

Setting environment variables locally in your robot

  1. Create a directory called devdata in the root of your robot.
  2. Create a file called env.json and add this content to it:
{ "WEBSITE_URL": "https://example.com/" }

Save the file and run your robot, it will take a screenshot of the site that you specified in your env.json file.

Note that these settings only affect running on your local machine and are meant for development phase testing only. The chapter below describes how to set variables in Control Room.

Setting environment variables in Control Room

When you have your robot step assigned to a process in Control Room, you can easily configure the environment variables using the UI:

Setting environment variables

Now, if you want to take a screenshot of another URL, you can easily do it through the UI; no code changes needed. This also means that the robot step can be reused in multiple processes, just by changing its configuration.

An alternative approach

Instead of using environment variables to configure your robots dynamically, you can also pass parameters to them using Control Room APIs.

Last edit: May 5, 2022