Test Automation 101: (3)Creating Keywords and Tests in Robot Framework
*Please read the tutorial introduction, in case you haven’t yet. Here’s the link -> [LINK]
*Previous article in the tutorial series -> [LINK]
Robot Framework is known for its keyword-driven testing approach. Basically, the keywords are English-like action words which makes it easier to read and understand.
Types of keywords in Robot Framework
- Library Keywords — implemented in Test Libraries. Think of these as the predefined keywords already available for use upon setup.
- User Keywords — your keywords. These are the custom keywords that you create to suit your needs.
Here are examples of Library Keywords
Run Keyword If | condition | keyword-to-run | args
It runs the given keyword with the given arguments, if condition is true.
Should Be Equal | first-val | second-val | error-message
Fails if the given objects are unequal and displays the error message, if given with a value.
Selenium2Library — http://robotframework.org/Selenium2Library/Selenium2Library.html
Input Text | locator | text
Types the given text into text field identified by locator.
Click Button | locator
Clicks the button identified by the locator.
There are many other keywords the can readily be used. It would be helpful to familiarize yourself with them so I highly suggest going through the links above.
Creating User Keywords
Most of the time, there will be a need to create your own keywords. For organizing a set of actions, for performing actions which aren’t supported by the libraries, or other reasons. So knowing how to create your own keywords is going to be useful. Let’s get straight to it.
For reference, this is our current test case, the one we created in the previous article.
Now, what we will be doing is to group the Input Text keywords into one User Keyword (Custom). Let’s call it “Enter values in Facebook Page.”
What we did?
- Create a keywords section.
- Defined the User Keyword — “Enter values in Facebook Page.”
- Called the keyword inside the Test Case.
That’s how simple it is. You can also think of keywords as functions, similar to other programming languages.
But the job is not done yet. 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 we did?
- Added Arguments just below the keyword name.
- Replaced the hard-coded values with the argument variables.
- 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 value being entered. If we don’t have the keyword, we have to enter the 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. This was more efficient compared to doing the same steps without a keyword.
When is it advisable to create keywords?
- reusable actions. ex. Login to an application, Logout, Populating fields in a commonly used page, etc.
- actions which are not supported by the Library Keywords
- standardization. ex. you want people in the team to have a standard flow when creating a certain type of transaction.
The structure of how you group the keywords that you will be creating will vary from one project to another. But for now, at least you are equipped with the know-how to create one! Well done.
You may want the results to show several tests instead of just one. In cases like that, you can just split your test case. Using the same example earlier, it would look like this:
If you run the whole test suite, the results should display 3 tests.
In Robot Framework, you can create several test cases in one Robot Test Suite file. You have the freedom to decide how you are going to organize your test.
So far so good!
In the next tutorial we will discuss how to integrate our local project to a remote repository in GitHub.
If you ran into some issues, I’d be happy to assist. Just leave a comment and I’ll respond as soon as I can.