Test Automation 101: (3) Creating Keywords and Tests in Robot Framework

Vince Reyes Tech
4 min readFeb 25, 2024

--

Robot Framework is known for its keyword-driven testing approach. The keywords are English-like action words, making it easier to read and understand.

Types of Keywords in Robot Framework

  1. Library Keywords are implemented in Test Libraries. Think of these as the predefined keywords available in imported libraries.
  2. User Keywords are the custom keywords you create to suit your needs.

Here are examples of Library Keywords

BuiltIn Library

Run Keyword If | condition | keyword-to-run | args

It runs the given keyword with the given arguments if the condition is true.

Should Be Equal | first-val | second-val | error-message

It fails if the given objects are unequal and displays the error message if given with a value.

SeleniumLibrary

Input Text | locator | text

Types the given text into the text field identified by the locator.

Click Button | locator

It clicks the button identified by the locator.

You can readily use many other keywords. It would be helpful to familiarize yourself with them, so I suggest going through the links above.

Creating User Keywords

It’s essential to create your keywords to organize a set of actions or perform actions not supported by libraries. This skill will come in handy for many reasons. So, let’s learn how to create your keywords.

The image below is the test case we created in the previous article.

Now, we will group the Input Text keywords into one User Keyword. Let’s call it “Input values in Puppy Bank Login Page.”

What did I do?

  1. I created a keywords section.
  2. I defined the user keyword , “Enter values in Puppy Bank login page.”
  3. I called the user keyword inside the Test Case.

That’s how simple it is. Like other programming languages, you can think of keywords as functions or methods.

But the work still needs to be completed. If you look closely, our keyword has hard-coded values. It means that every time we call that keyword, it inputs the same values.

To avoid that, we are going to use Arguments.

What did I do?

  1. I added Arguments just below the keyword name.
  2. I replaced the hard-coded values with the argument variables.
  3. I entered the actual values in the test.

What is the benefit of using arguments?

We can call the keyword multiple times and change only the entered values. If we don’t have the keyword, we have to enter several “Input Text” keywords over and over again.

For example, we can do this.

We called the same keyword multiple times but entered different values. It is more efficient than doing the same steps without a keyword.

When is it advisable to create keywords?

  • Reusable actions. Ex. Login to an application, Log out, Populate fields in a commonly used page, etc.
  • Actions that the Library Keywords do not support.
  • Standardization. Ex. You want people in the team to have a standard flow when creating a particular type of transaction.

The structure of how you group the keywords you will be creating will vary from one project to another. But for now, at least you have the know-how to create one! Job well done to you.

Creating Tests, Suite Setup, and Suite Teardown.

You can split your big test case to show several tests instead of just one. Using the same example earlier, it would look like this:

What did I do?

  1. Added Suite Setup* and Suite Teardown** in the Settings section and moved the appropriate steps.
  2. I created 3 test cases that contain different input values.

*Suite Setup is executed once at the beginning.

**Suite Teardown is executed also once but at the very end.

The results should display three tests if you run the whole test suite.

In Robot Framework, you can create several tests in one Robot Test Suite file. You have the freedom to decide how you will organize your test.

So far, so good!

The following tutorial will discuss applying a better project structure to our code.

--

--