Robot Framework: Creating Custom Keywords in Python

Hammad Ahmed Khan
The Startup
Published in
4 min readFeb 21, 2021
Photo by James Harrison on Unsplash

In the last part which we wrote a while back, we covered the Basics of the Robot Framework. I thought to share another important part of Robot Framework which helped me a lot while setting up different Robot Framework based test automaton projects i.e. creating Custom Keywords.

While creating some large scale test automation projects there comes a point when you don’t or can’t implement complex logic in Robot Framework syntax itself, either it is too complex or it becomes cumbersome to do so. So to solve this issue the best solution is to write your own custom Keywords in Python. The benefits of it are you get the full power of Python Libraries.

Let’s stop talking about it and try it out.

Let’s Recap!

So just to recap what we had done before, we opened Google and verified the title of the website i.e. Google.

Let’s Update the Test!

Now let’s open another website rather than Google. So we will change the code at line 11.

Open Browser    http://automationpractice.com/   browser=chrome

And change the respective website names from Google to Store. We will remove the Close Browser Keyword as we want to continue with tests. The final code change should look like this:

*** Settings ***Library    SeleniumLibraryDocumentation
... My First Test
... This test will try to Signin with Wrong Email
*** Keywords ***Navigate To Store
Open Browser http://automationpractice.com/ browser=chrome
Verify Page Title Contains Store
${Get_title}= Get Title
Should Be Equal As Strings ${Get_title} STORE
*** Test Cases ***Open Store & Verify Store
Navigate To Store
Verify Page Title Contains Store

Next thing is to create a new Keyword that can be used to Sign-in with dummy credentials and verify an Error is shown.

Signin With Dummy Email
Click Element css=.login
Wait Until Page Contains Element email 20s
Input Text email test@email.com
Input Text passwd password123
Click Element SubmitLogin
Wait Until Page Contains Authentication failed.

Final Outlook should look like the following:

Once we created the new Keyword, we should add it to the test as well. Let’s run the test now.

In the command below -d results/ or outputdir results/ outputs the results into a new directory.

robot -d results/ my-first-test.robot

It should be successful and should pass the test. Yaay!

Let’s add a Custom Keyword!

Now let’s test new functionality, to do so let’s add a keyword to generate Random Email and try to login with it. Doing this with Robot Framework syntax would have to write a dirty code, which many lines long and difficult to comprehend even. So let’s create a python based Custom Keyword which will be easier to write.

The first thing to do, to achieve this is to create a folder in the root of the project we have i.e. lib.

mkdir lib

Next thing is to create a file for our custom library which we will be used to write our own custom keyword which can be imported into our Robot Framework file. So we create a file named CustomLib.py with the following code.

We added ROBOT_LIBRARY_VERSION this allows us to version our custom library. We also added ROBOT_LIBRARY_SCOPE as global so that it can be accessed globally. This whole library needs to be defined in a class so that it can be accessed easily in the Robot Framework test file.

Here we added two functions that will help us generate random emails with random domain and return them as an array. As you can see these are simple python functions. So you can do anything possible with python code or function and later it can be used in Robot Framework.

Now let’s go and update the Test file.

Library    ./lib/CustomLib.py

After importing this Custom Library we created, with a custom function, we can now use it in the Robot Framework test.

Signin With Dummy Email
Click Element css=.login
Wait Until Page Contains Element email 20s
${random_email} Generate Random Emails ${8}
Input Text email ${random_email}
Input Text passwd password123
Click Element SubmitLogin
Wait Until Page Contains Authentication failed.

So in the Keyword above we generated a random email with length 8 and assign its return value to a variable and input it into the email field.

In Robot Framework to pass an integer you need to put it inside ${}, otherwise by default it will be taken as string.

Another thing you might have noticed you don’t need to use underscores in between the function or keyword names, it automatically maps it. You also don’t need to worry about which case it is in, but my suggestion would be to stick with a cleaner and consistent casing. So rather than generate_random_emails we can simply use Generate Random Emails.

So the final code will look something like this:

In the end, we also added Close Browser, so that we close the session, otherwise, it will keep the browser open, either it fails or passed the test.

You can run this test again and verify if it passes.

robot -d results/ my-first-test.robot

I hope this article helps you in using Custom Keywords and the power of python. Stay tuned for more advanced usage of Robot Framework later.

--

--

Hammad Ahmed Khan
The Startup

Tech enthusiast, innovative thinker & storyteller. Exploring the human aspect of tech & digital journeys. Join me for insights & lively discussions.