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.
In today’s article on the Robot Framework we will take a closer look at its file processing capabilities. At the same time – I believe – this is a very good example to take a closer look how to implement a bit more complex functionality entirely using Robot Framework Keywords concept .
The Problem
Let’s assume we want to process a CSV file that has the following kind of content:
# ID, Text, Value
10, First Item, 300
20, Second Item, 200
30, Third Item, 100
Background could be to test an application that is summing up the values (third column) in such files and we want to test if that calculation is correct. Of course we could pre-sum those values manually up-front and then test against this value. But that approach would be somehow counterproductive for the purpose of this blog post ;-).
The solution is developed in an iterative way by adding more processing steps bit by bit. Hopefully this makes it easier to understand the final solution.
Step 1 – Reading in the file
Let’s start with reading in the content of a text file which is really really straightforward:
*** Settings ***
Library OperatingSystem
*** Variables ***
*** Test Cases ***
Process Data File
[Tags] file-reading
${FILE_CONTENT}= Get File testdata.csv
Log File Content: ${FILE_CONTENT}
We can easily check that this is really doing what we expect it to do by inspecting the Robot Framework log file.
Here we have taken our first Standard Test Library into use, namely the Operating System Library . That was definitly the easy part. Now it is going to get a bit more complex.
Please note that you can find all examples also from GitHub here, which makes it hopefully more convenient to try those out and play around a bit with it:
https://github.com/ThomasJaspers/robot-keyword-tutorial#sample-9-file-processing
Step 2 – Splitting the File Content in Lines and removing the Header
The next step is to split the file content into lines so we can process those in a loop. At the same time we want to remove the first line from the list as it only contains the header and must not be processed.
*** Settings ***
Library OperatingSystem
Library String
Library Collections
*** Variables ***
*** Test Cases ***
Process Data File
[Tags] file-reading
${FILE_CONTENT}= Get File testdata.csv
Log File Content: ${FILE_CONTENT}
@{LINES}= Split to Lines and Remove Header ${FILE_CONTENT}
Log ${LINES}
*** Keywords ***
Split to Lines and Remove Header
[Arguments] ${FILE_CONTENT}
@{LINES}= Split To Lines ${FILE_CONTENT}
Remove From List ${LINES} 0
[Return] @{LINES}
For this to work we need the String Library to split the lines from the file into an array using the “Split to Lines” keyword. Then we are using the Collections Library to remove the first element from the array as it contains only header information.
Again we are checking the result by logging out the array containing the remaining lines from the file. Surprisingly this looks promising so far :-).
Step 3 – Aggregating the values from the third column
In the final step we are looping over the list, splitting the individual elements into its columns and then processing the sum of the values from the third columns from that list (and thus from the file).
*** Settings ***
Library OperatingSystem
Library String
Library Collections
*** Variables ***
*** Test Cases ***
Process Data File
[Tags] file-reading
${FILE_CONTENT}= Get File testdata.csv
Log File Content: ${FILE_CONTENT}
@{LINES}= Split to Lines and Remove Header ${FILE_CONTENT}
Log ${LINES}
${RESULT}= Calculate Sum From List ${LINES}
Log ${RESULT}
*** Keywords ***
Split to Lines and Remove Header
[Arguments] ${FILE_CONTENT}
@{LINES}= Split To Lines ${FILE_CONTENT}
Remove From List ${LINES} 0
[Return] @{LINES}
Calculate Sum From List
[Arguments] ${LIST}
${RESULT}= Set Variable 0
: FOR ${LINE} IN @{LIST}
\ Log ${LINE}
\ @{COLUMNS}= Split String ${LINE} separator=,
\ ${VALUE}= Get From List ${COLUMNS} 2
\ Log ${VALUE}
\ ${RESULT}= Evaluate ${RESULT}+${VALUE}
[Return] ${RESULT}
Especially interesting here might be the “Evaluate” keyword that allows us for example to add two values and stores the result to another variable. And we see the result again from the logged value.
Basically that is it for the sake of demonstrating a bit of the “programming possibilities” that are available from the Robot Framework keywords concept.
Conclusion
We have seen a nice mixture of keywords from different Robot Framework test libraries together with the usage of “control structures” for looping over array elements. If you are new to the Robot Framework it might be surprising how powerful the keyword concept is. Personally I think that this way of implementing test functionality is much easier for “pure testers” in a team than implementing those in Java entirely (for example using JBehave ).
Processing huge files this way might fail due to the extensive logging the Robot Framework is doing. In that case it might be needed to refactor everything into one keyword and then using the flattening keywords option of the Robot Framework .
Let’s see what comes next. There are still some default test libraries I would like to take a closer look at. Stay tuned :-).
More articles
fromThomas Jaspers
Your job at codecentric?
Jobs
Agile Developer und Consultant (w/d/m)
Alle Standorte
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.
Blog author
Thomas Jaspers
Do you still have questions? Just send me a message.
Do you still have questions? Just send me a message.