Variables and User Keywords

Using variables

Variables allow you to communicate values from one keyword to another. They can also make your code cleaner and simpler.

To learn more about variables in Robot Framework, check out Robot Framework: Variables.

Let's create a new variable for the robot address, to show how to work with variables in Automation Studio. Use the "Set Variable" keyword to define a new variable, as in the picture below, below "Open Available Browser". On the left side of it, type "address".

Variables are color-coded in Automation Studio with a blue bar next to them.

New variable

The easiest way to use the variable in your code is to drag the variable from where it has been defined by grabbing the blue bar next to it, and dropping it into the field where you wish to use it (see picture).

Drag variable

That's it, now you are using a variable in your code, and everything should work just as before.

Interacting with variables will be made even easier in the next versions of Automation Studio.

If you are familiar with Robot Framework, then you can also use variables by typing in the variable name, such as ${address} in a text field.

Create a user keyword

User Keywords (Custom Keywords) are a practical way to group related functionality and make code that you want to call from multiple places. They are similar to functions, methods, or procedures in other programming languages.

To create User Keywords quickly from existing code lines, select all the keywords you want to group and click "Create User Keyword". Let's create a user keyword for filling in the form, to make the structure of our code a bit cleaner.

First, click on the "Set Variable" keyword in your code and with shift key pressed, click on the last "Click Element" keyword. All the keywords between them should be now selected and have a blue border around them.

Clicking with the shift key pressed selects all adjacent keywords between the first and the last one. You can also use the ctrl key (on a Mac, command key) to select multiple non-adjacent keywords. These same selections can be used to reorder and for copying/pasting multiple keywords.

Click on "Create User keyword" and give the user keyword the name "Fill Form". The new user keyword should now show up under the user keyword header in the left navigation.

Quick creation of User Keywords

Another way of creating user keywords is to click on the plus button (+) next to "User keywords" in the left panel or by switching to the code view and creating the keywords there.

Your task looks much simpler now!

Code with User Keyword

User keywords can also take arguments as input. Those can be defined from the right panel when the user keyword is open. This can be very powerful when combined with loops and variables to do some action for a list of results, for example.

Setup and teardown

Setup and teardown let you run keywords before and after every run of your main actions. This can be useful for structuring code and for making sure that some cleanup is always performed, no matter how the execution ended. To use them, you can toggle setup/teardown from the top of the main code area.

Setup and teardown

Loops and conditions

Something that deserves a mention in the course, even though we won't go deep into it, is that Automation Studio supports a full set of commands from looping over variables and conditional statements, such as if-statements. Have a look at the "Web Example" template robot in Automation Studio to see how to use those.

For loop

Observe, that when you drag list variables into for-loops, they get an "as list" suffix added to them to automatically unpack the lists for looping over them. You can change this by right-clicking on the variable.

Don't worry if you didn't understand everything about the items covered in this chapter, you'll test them out in more detail later on. You are doing great and the hardest part is already over. In the final chapter, we will upload your robot to Control Room.