Robot Framework Tutorial – Loops, Conditional Execution and more


Robot Framework Tutorial

Part I: Robot Framework Tutorial – Overview
Part II: Robot Framework – A complete example
Part III: Robot Framework IDE
Part IV: How to Structure a Scalable And Maintainable Acceptance Test Suite
Part V: Robot Framework Tutorial – Writing Keyword Libraries in Java
Part VI: Robot Framework Tutorial – Loops, Conditional Execution and more
Part VII: Robot Framework – Testing Windows Applications
Appendix A: Robot Framework – Compact Sheet

The new Robot Framework Tutorial 2016 series

So far this blog series was dealing more with the higher-level concepts of the Robot Framework. Now this means it is really about time to dig into some very basic features the Robot Framework is offering. All of those features are coming directly with the Standard Libraries which are installed right away with every Robot Framework installation. Thus to follow the examples from this article there is nothing more needed that a pure Robot Framework installation. (Please note that you can download all examples at the end of this blog post.)

Loops and Repeating Keywords

Let’s start with Loops. Those can come in quite handy at times and the Robot Framework is supporting them in quite some different flavours:

  • Looping over a list of elements.
  • Looping over a range of numbers.
  • Repeating a single keyword several times.

The latter one is a bit different from the real loops as it means that all actions are really put into one Keyword (sometimes this might be meaningful and sometimes not). Furthermore it is not possible to exit (break) such a loop before all iterations have been finished.

Let’s take a look at some examples:

The Tests

*** Settings ***
Library           String

*** Test Cases ***
    : FOR    ${INDEX}    IN RANGE    1    3
    \    Log    ${INDEX}
    \    ${RANDOM_STRING}=    Generate Random String    ${INDEX}
    \    Log    ${RANDOM_STRING}

    @{ITEMS}    Create List    Star Trek    Star Wars    Perry Rhodan
    :FOR    ${ELEMENT}    IN    @{ITEMS}
    \    Log    ${ELEMENT}
    \    ${ELEMENT}    Replace String    ${ELEMENT}    ${SPACE}    ${EMPTY}
    \    Log    ${ELEMENT}

    @{ITEMS}    Create List    Good Element 1    Break On Me    Good Element 2
    :FOR    ${ELEMENT}    IN    @{ITEMS}
    \    Log    ${ELEMENT}
    \    Run Keyword If    '${ELEMENT}' == 'Break On Me'    Exit For Loop
    \    Log    Do more actions here ...

    Repeat Keyword    2    Log    Repeating this ...

The Output

Starting test: StandardLoopDemo.For-Loop-In-Range
20130426 11:24:14.389 :  INFO : 1
20130426 11:24:14.390 :  INFO : ${RANDOM_STRING} = B
20130426 11:24:14.390 :  INFO : B
20130426 11:24:14.391 :  INFO : 2
20130426 11:24:14.392 :  INFO : ${RANDOM_STRING} = ih
20130426 11:24:14.392 :  INFO : ih
Ending test:   StandardLoopDemo.For-Loop-In-Range

Starting test: StandardLoopDemo.For-Loop-Elements
20130426 11:24:14.394 :  INFO : @{ITEMS} = [ Star Trek | Star Wars | Perry Rhodan ]
20130426 11:24:14.395 :  INFO : Star Trek
20130426 11:24:14.396 :  INFO : ${ELEMENT} = StarTrek
20130426 11:24:14.396 :  INFO : StarTrek
20130426 11:24:14.397 :  INFO : Star Wars
20130426 11:24:14.398 :  INFO : ${ELEMENT} = StarWars
20130426 11:24:14.398 :  INFO : StarWars
20130426 11:24:14.399 :  INFO : Perry Rhodan
20130426 11:24:14.400 :  INFO : ${ELEMENT} = PerryRhodan
20130426 11:24:14.400 :  INFO : PerryRhodan
Ending test:   StandardLoopDemo.For-Loop-Elements

Starting test: StandardLoopDemo.For-Loop-Exiting
20130426 11:24:14.402 :  INFO : @{ITEMS} = [ Good Element 1 | Break On Me | Good Element 2 ]
20130426 11:24:14.402 :  INFO : Good Element 1
20130426 11:24:14.403 :  INFO : Do more actions here ...
20130426 11:24:14.404 :  INFO : Break On Me
Ending test:   StandardLoopDemo.For-Loop-Exiting

Starting test: StandardLoopDemo.Repeat-Action
20130426 11:24:14.408 :  INFO : Repeating keyword, round 1/2
20130426 11:24:14.408 :  INFO : Repeating this ...
20130426 11:24:14.408 :  INFO : Repeating keyword, round 2/2
20130426 11:24:14.409 :  INFO : Repeating this ...
Ending test:   StandardLoopDemo.Repeat-Action

Probably there is not too much to explain here as the syntax is pretty straight forward. One important thing to consider is that Keywords belonging to a loop-block have to be escaped as shown above using a “\”.

FAIL : FOR loop contains no keywords. – Not escaping the lines that belong to a certain loop will result in the before mentioned error message when executing the tests.

The above examples also already show some of the String Operations and features for Conditional Execution that are discussed in more detail in the following chapters.

Conditional Execution of Keywords

Conditional Execution of test code is something that can lead to quite some controversial discussions. No worries, I am not going to dig into those. The only thing that we should keep in mind is that the implementation of tests should be as simple and clear as possible. Adding a lot of conditions might contradict this approach.

In the following examples these related Keywords have been used:

  • Run Keyword – This one is pretty cool as it allows to pass in a Keyword in a Variable. This makes it possible to create quite some dynamic to the tests as the Keyword to be executed could be returned from another keyword. Can be used to do either pretty neat things or to produce total chaos ;-).
  • Run Keyword If – This is very helpful when testing complex structures. This could be for example dynamic web pages that enable/disable certain fields based on some input data. When used properly it can be used to create if/else-like structures. But be careful: The more programming there is inside the tests the more difficult it is to do troubleshooting if tests are failing.
  • Run Keyword And Ignore Error – Honestly I did not yet had any real-life usecase for this. It is just another example what is possible with the Robot Framework.

Looking into the documentation of the Build-In Library one can see that there are a lot more of those Keywords. Especially Keywords like “Run Keyword If Test Failed” can be very useful for troubleshooting by putting for example screenshots or other information to the Robot Log in case of failing testcases.

The Tests

*** Test Cases ***
    ${MY_KEYWORD}=    Set Variable    Log
    Run Keyword    ${MY_KEYWORD}    Test

    ${TYPE}=    Set Variable    V1
    Run Keyword If    '${TYPE}' == 'V1'    Log     Testing Variant 1
    Run Keyword If    '${TYPE}' == 'V2'    Log    Testing Variant 2
    Run Keyword If    '${TYPE}' == 'V3'    Log    Testing Variant 3

    @{CAPTAINS}    Create List    Picard    Kirk    Archer
    Run Keyword And Ignore Error    Should Be Empty    ${CAPTAINS}
    Log    Reached this point despite of error

The Output

Starting test: Robot Blog.StandardConditionDemo.Run-Keyword
20130426 13:34:50.840 :  INFO : ${MY_KEYWORD} = Log
20130426 13:34:50.841 :  INFO : Test
Ending test:   Robot Blog.StandardConditionDemo.Run-Keyword

Starting test: Robot Blog.StandardConditionDemo.Run-Keyword-If
20130426 13:34:50.843 :  INFO : ${TYPE} = V1
20130426 13:34:50.844 :  INFO : Testing Variant 1
Ending test:   Robot Blog.StandardConditionDemo.Run-Keyword-If

Starting test: Robot Blog.StandardConditionDemo.Run-Keyword-Ignore-Error
20130426 13:34:50.847 :  INFO : @{CAPTAINS} = [ Picard | Kirk | Archer ]
20130426 13:34:50.848 :  INFO : Length is 3
20130426 13:34:50.849 :  FAIL : '[u'Picard', u'Kirk', u'Archer']' should be empty
20130426 13:34:50.850 :  INFO : Reached this point despite of error
Ending test:   Robot Blog.StandardConditionDemo.Run-Keyword-Ignore-Error

Working with Strings and Lists

Well, basically there is a full programming interface – as used from higher level programming languages – available here. The following short example shows some very basic usage. It is probably a very good idea to browse the available keywords of the Collection Library and the String Library just to get an idea what functionality is available.

The Tests

*** Settings ***
Library           String
Library           Collections

*** Test Cases ***
    ${SOME_VALUE}=    Set Variable    "Test Value"
    Log    ${SOME_VALUE}
    @{WORDS}=    Split String    ${SOME_VALUE}    ${SPACE}
    ${FIRST}=    Get From List    ${WORDS}    0
    Log    ${FIRST}

The Output

Starting test: Robot Blog.StandardStringsAndListsDemo.StringsAndLists
20130506 21:21:05.880 :  INFO : ${SOME_VALUE} = "Test Value"
20130506 21:21:05.881 :  INFO : "Test Value"
20130506 21:21:05.882 :  INFO : @{WORDS} = [ "Test | Value" ]
20130506 21:21:05.882 :  INFO : ${FIRST} = "Test
20130506 21:21:05.883 :  INFO : "Test
Ending test:   Robot Blog.StandardStringsAndListsDemo.StringsAndLists

So far to get some ideas of the more basic functionality of the Robot Framework. At the time of writing this blog I was also trying out the latest version (in this case 1.1) of the Robot-IDE (RIDE). And I have to say it worked really well and tests could be executed right away from the IDE without any problems. Here are two screenshots to give some impressions.

Editing Testcases with RIDE

Editing Testcases with RIDE

Executing Testcases from RIDE

Executing Testcases from RIDE

You can download all examples from the following ZIP-file.

Long-term experience in agile software projects using
Java enterprise technologies. Interested in test automation tools and concepts.


  • Tushar

    HI i want to store value of each output i get in for loop in robot framework

    something like:

    :for ${n} IN RANGE 0 100
    \ ${Z} = Execute command cat /dds/store/${value}${n}
    should be empty ${Z}

    currently it is executing the command from n = 0 to n = 100 and returning the output of each execution and storing the value in z in every line
    and making sure the last output that is for 100 is empty .

    but i want to check it should be empty for every output from zero to 100.

    Please help!


  • @halperinko - Kobi Halperin

    First of all – I would like to thank you for these elaborate posts – these really makes it easier to grasp Robot framework abilities.

    One thing I found useful (but normally lacking in most frameworks), was enabling of Keywords usage during Manual Tests.
    As we already put lot’s of effort in the generic keywords of all levels – Why not make use of these in Manual & Exploratory types of tests which complement automation?

    I was wondering if a simple GUI can be made – which can be kept running in background,
    When the tester wish to invoke the help of the automation framework – S/he can select the desired Keyword out of active libraries, set values to the required parameters based on keyword structure, and execute it.
    If needed, one can define to run same keyword xN times.
    Of course if a more extensive activity is needed – one can use existing keywords to build the newly desired one.

    This way we can make the already available automation resource support manual testing as well – saving vast amount of time taken by tedius tasks.

    Hope to see that as part of the Robot Framework, for the benefit of us all.

    @halperinko – Kobi Halperin

  • @halperinko - Kobi Halperin

    One other thing I was hoping to find in these extensive tutorial posts, was regarding an optional interrelation between the Robot Framework and ALM / Test Management Tools.
    Can the Robot automation be managed from an ALM?
    Can we get the results sent back to ALM tools for easier higher management summaries and Dashboards?

    @halperinko – Kobi Halperin

  • Ery

    Is there any way to use the loop backwards and decreasing? From 10 to 5 for example?



Your email address will not be published.