logo logo

Robot Framework: Tips for Writing Automated Test Cases

Robot Framework: Tips for Writing Automated Test Cases

Creating great automated test cases is not an easy task. Especially in a hectic environment in which the client requires effects as soon as possible. To be honest, this demanding, ever changing need feels to me like my girlfriend’s bragging about something new to buy to the house 😅 You know the drill – new month, new desire – or, in terms of a project – new feature.

And what can we do? 

We need to act fast and we need to be smart.

Thus in today’s article, we will focus on those traits. Or, to be more specific, I will give you some good tips on why and how to use Robot Framework to create the best impression on your loved one.

1. Ask for the desire or what furniture we need to buy

Before we start automation testing, we need to cope with our client. Be a little personal. Ask him for what he wants to cover. But not from a technical point of view – don’t be silly – define & establish business priority. What needs to be ALWAYS working. Which feature, if broken, will make the worst impact on the company. What part has to look great each and every time. Focus on those areas as you will, most likely, need to test those parts like crazy.

RPN – risk priority number – works like magic. It is my IKEA catalog showing me what is hot or not. Just ask the client to rate each feature from 1 to 5 in three different ways:

  • Severity
  • Occurrence
  • Detection

And multiply those numbers. The higher the final number the more important it is for the client.

2. Go to IKEA

Now that we know what to do, we need to know how to do it. We will use Robot Framework as it is:

  • Quite simple for new people to use. 
  • Quite easy to maintain and handle.
  • Quite readable for each member of the team.
  • Quite flexible when it comes to syntax.
  • Quite forgiving in terms of keyword driven approach.

And it also has A LOT to offer in terms of “furnitures” or, in our language, libraries.

You want your room to be cozy and useful and Robot Framework libraries allow this. Let’s just look at some of them:

  • SeleniumLibrary – Web testing library that utilizes Selenium.
  • Allure-python – Flexible lightweight tool to report tests in easy to read format.
  • RED Robot editor – Eclipse based editor for Robot Framework that allows you to create test cases.
  • Appium library Appium library which allows you to deal with mobile tests easy and simple.
  • Docker library – Maybe you love Docker and wish to treat yourself with a little virtualization? You can do it!
  • RESTinstance – A funky way of API testing based on constraints (“we need a working sofa”) rather than values (“sofa needs to be green”).
  • RF Lint – To check our readability.
  • NPP robot – To syntax RF in notepad++ (the only true IDE for testers 😉).

3. Less is more

Once we know what and how, we need to remember about a simple rule of thumb – the cleaner the better. Because no one loves messy test cases!

Why am I saying this? Well, your test case is based on keywords. Either created by you or imported from RF libraries. Liberty is a huge privilege because you can be as high-level as possible and decide on your own how to deal with scripts.

As keywords can accept any number of arguments and as many tags as you want, we need to be a little picky. You wish to have your room as clean as possible! Separate test cases in different folders easily to call and use. Use “why” approach rather than “how”. Be consistent and precise. Try Gherkin style (Given/When/Then). Avoid unrelated checks and dependencies between tests. Move logic to libraries when possible. And for the love of god – don’t use sleeping to synchronize!

By the way, RF creators knew about zen attitude and prepared a simple way to display different types of your code:

  • $ for variables
  • @ for lists
  • & for dictionaries
  • % for environments

This is quite neat when you will imagine that you have a test case that looks like this:

*** Settings ***

Resource        awesome_keywords.robot

Suite Setup     Open My Website

Suite Teardown  Close Browser


*** Test Cases ***

User can login to website

   [Tags]   login

    Input Login    my_login

    Input Password    my_password

    Login

    Login is a success

And your keywords file looks something like this:

*** Settings ***

Library     SeleniumLibrary

 

*** Variables ***

${url}          http://sampleWebsite.com

${browser}      Chrome

 

*** Keywords ***

Open My Website

    Open browser    ${url}   ${browser}

 

Close Browsers

    Close All Browsers

Input Login

    [Arguments]      ${my_login}

     Input Text  xpath://select[@name='loginField']  ${my_login}

 

Input Password

    [Arguments]      ${my_password}

    Input Text  xpath://select[@name='passwordField']    ${my_password}

 

Login

    Click Button    css:input[type=login']

 

 Login is a success

    Wait Until Element Contains    ‘Welcome!’

And if you want to spend even less time on boring checking unique and best locators, I highly suggest using POM builder add-on. It will do plenty good to speed your writing process.

4. Execute and repeat!

Execution is as simple as writing: robot -include[tag] path/to/your/awesome/tests and I guarantee you that you will have them running like crazy. Especially that this flexible tool allows us to be creative. Use randomizers to fill some fake data. Take screenshots for better readability by using predefined Library Screenshot. Make sure you don’t overuse variables. Try to move suite setup/teardown to the directory level initialization files. Keep your flow at a steady pace with precondition, action, verification and cleanup. But always (ALWAYS!) keep in mind to have a clean room 🤗

Automation doesn’t need to be long and boring. But it requires a little attention from your side. Remember that a good test normally does not need documentation. If you need it, then it is not as clean as you thought. Sometimes making less steps can give amazing results. Trust me on this one.

About the author

Marcin Sikorski

Public speaker, writer, tester, influencer, mentor. Specialist of Internet of Things and Industry 4.0 working in IT since 2013. Advisor for Polish government (IoT department) in area of where the IoT can be implemented in Poland and how exactly this can be achieved. Currently working with a smart solution for nuclear power plants.

After hours, an enthusiast of optimization, China and mountain climbing.

Website – https://www.smartrzeczy.pl

Leave a Reply

FacebookLinkedInTwitterEmail