Beliebte Suchanfragen

Cloud Native

DevOps

IT-Security

Agile Methoden

Java

//

Robot Framework Tutorial – Loops, Conditional Execution and more

5.5.2013 | 6 minutes of reading time

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-Loop-In-Range
    : FOR    ${INDEX}    IN RANGE    1    3
    \    Log    ${INDEX}
    \    ${RANDOM_STRING}=    Generate Random String    ${INDEX}
    \    Log    ${RANDOM_STRING}

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

For-Loop-Exiting
    @{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-Action
    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 ***
Run-Keyword
    ${MY_KEYWORD}=    Set Variable    Log
    Run Keyword    ${MY_KEYWORD}    Test

Run-Keyword-If
    ${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

Run-Keyword-Ignore-Error
    @{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 ***
StringsAndLists
    ${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

Executing Testcases from RIDE

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

share post

Likes

0

//

More articles in this subject area

Discover exciting further topics and let the codecentric world inspire you.

//

Gemeinsam bessere Projekte umsetzen.

Wir helfen deinem Unternehmen.

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.