Beliebte Suchanfragen
//

Robot Framework Tutorial 2016 – Working with Collections

16.12.2016 | 5 minutes of reading time

Robot Framework Tutorial 2016

Part 1: Installation
Part 2: Keywords
Part 3: Implementing Keywords in Java
Part 4: Selenium2Library as a drop-in replacement for SeleniumLibrary
Part 5: Integration with TeamCity CI-Server
Part 6: Integration with Jenkins
Part 7: File Processing
Part 8: Working with Collections
Part 9: Wrap-Up and Conclusion

The “old” Robot Framework Tutorial.


The Robot Framework supports working with Collections for writing tests and keywords. One scenario for lists is for example using them in loops. The System under Test might also return lists that can then be easily further processed in the tests. We have seen some examples for this already in the keywords-related post of this series . But in this article we want to dive a bit deeper into the topic. Before starting it should be noted that the “Collections” library is a standard library of the Robot Framework and thus included in every installation without the need to install anything on top.

A very simple Example

Let’s take a look at this first very simple example. We are defining a list using the “Create List” keyword. Then we are storing the size of the list using the “Get Length” keyword. Finally we are checking our expectations. This is a valid though totally meaningless test :-).


*** Settings ***
Library           Collections

*** Test Cases ***
Check Element Number
    @{MY_SIMPLE_LIST}=  Create List   Star Trek   Star Wars   Battleship Galactica
    ${NUM}=  Get Length  ${MY_SIMPLE_LIST}
    Should be Equal as Integers  ${NUM}  3

The funny thing about the above example is that the import of the “Collections” library is not needed at all. All keywords used are part of the Robot Framework Build-In library. Anyway this hopefully gives some initial insight on working with lists.

The examples shown here have been added to the robot-keyword-tutorial on Git. This way it is hopefully pretty easy to play around with the examples presented here.

One thing that can be a bit confusing when starting to work with lists are the different notations used. Sometimes variables representing lists are prefixed with “@” and sometimes with “$”. Luckily the solution to this is pretty simple: The “$”-notation is used when addressing the list as such (most keywords working with lists expect this). The “@”-notation is used to address the individual elements from the list. If a keyword is for example expecting two scalar values as parameters you can also use a list containing two elements using the “@”-notation.

A small extension of the above example shows how this works.


*** Settings ***
Library           Collections

*** Test Cases ***
Check Element Number
    @{MY_SIMPLE_LIST}=  Create List   Star Trek   Star Wars   Battleship Galactica
    ${NUM}=  Get Length  ${MY_SIMPLE_LIST}
    Should be Equal as Integers  ${NUM}  3
    Demo Keyword    Star Trek   Star Wars   Battleship Galactica
    Demo Keyword    @{MY_SIMPLE_LIST}

*** Keywords ***
Demo Keyword
    [Arguments]   ${NAME_1}   ${NAME_2}   ${NAME_3}
    Log Many   "Name 1:"    ${NAME_1}
    Log Many   "Name 2:"    ${NAME_2}
    Log Many   "Name 3:"    ${NAME_3}

Both calls to the “Demo Keyword” are equally working.

A more advanced Example

Let’s dive a little bit deeper into working with collections. First of all we are seeing another way of defining a list as a variable that can then be globally used in all testcases of the testsuite.

Note: Do not use the “Create List” keyword when defining global variables as this is not required and it would be added as an element to the list instead!

In the “Check List Test” we are using some straightforward keywords to do some checks on our list. In the “Process List Test” we are really modifying our list, which has of course an impact on all testcases following this one that are using this list. This is done here for demonstration purposes only and is not really a recommendation for real life projects. It is too easy to mess things up e.g. when switching the order of the tests.


*** Settings ***
Library           Collections

*** Variables ***
@{MY_SIMPLE_LIST}=  Star Trek   Star Wars   Battleship Galactica

*** Test Cases ***
Check List Test
  List Should Contain Value   ${MY_SIMPLE_LIST}   Star Wars
  List Should Not Contain Duplicates   ${MY_SIMPLE_LIST}

Process List Test
  ${ELEMENT}=   Remove from List   ${MY_SIMPLE_LIST}   0
  Should be equal   ${ELEMENT}   Star Trek

Further Process List Test
  ${NUM}=  Get Length  ${MY_SIMPLE_LIST}
  Should be Equal as Integers  ${NUM}  2
  Append To List   ${MY_SIMPLE_LIST}   Space 2063
  @{TEMP_LIST}=  Create List   Star Wars   Space 2063
  List should contain Sub List   ${MY_SIMPLE_LIST}   ${TEMP_LIST}

The last test “Further Process List Test” demonstrates that the list has changed :-). In addition it shows another way of modifying lists by appending an element and one way to compare the content of two lists.

Working with Dictionary Elements

Again let’s start with a trivial example. Same as working with lists we do not need to include the “Collections” library as basic support for the dictionary data type is included in the build-in library.


*** Settings ***

*** Test Cases ***
Dictionary Test
    &{MOVIE_CATEGORY_SAMPLE}=  Create Dictionary  action=Die Hard  scifi=Star Trek
    ${NUM}=  Get Length  ${MOVIE_CATEGORY_SAMPLE}
    Should be Equal as Integers  ${NUM}  2

What is called dictionary in the Robot Framework is probably better known as hashmap or hashtable. Not sure how often this is really needed when writing tests. Honestly I have never seen nor used this in any real life project so far. Anyway, the syntax is quite similar to what we have seen from working with lists. Main difference in the example above is the notation using the “&”-symbol to define the dictionary and of course elements are composed of key/value-pairs here.


*** Settings ***
Library           Collections

*** Test Cases ***
Check Elements Test
    &{MOVIE_CATEGORY_SAMPLE}=  Create Dictionary	 action=Die Hard  scifi=Star Trek
    Dictionary Should Contain Value  ${MOVIE_CATEGORY_SAMPLE}   Die Hard
    Dictionary Should Contain Key    ${MOVIE_CATEGORY_SAMPLE}   scifi

In the above example we are utilizing some keywords from the “Collections” library to demonstrate that we can work on the keys of a dictionary or on its values. This is documented in the corresponding keywords.

Conclusion

Collections – probably mainly Lists – can be quite useful when writing tests with the Robot Framework. The “Collections”-library is the place to look for corresponding keywords. Besides of course the very basic stuff that comes with the build-in library anyway. As with all keyword libraries it is always a good idea to browse a bit through the documentation to get an idea what functionality is available.

share post

//

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.