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
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 Robocorp Cloud, 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     Simple robot that takes a screenshot of a given webpage,
...               taking the URL from an environment variable.
Library           RPA.Browser
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 Robocorp Lab

  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, run your script by clicking on the >> button in the toolbar, and Robocorp Lab will take a screenshot of the site that you specified in your env.json file:

Running environment variable configured robot

Setting environment variables in Robocorp Cloud

When you have your robot step assigned to a process in Robocorp Cloud, 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 the Robocorp Cloud Process API.