Looping through the content of a file in RobotFramework

robotframework

How can I loop through the contents of a file within Robot Framework?

My file contents would be like this:

1001
1002
1003
1004

I want to read the contents one by one, assign it to a variable and then do some operations with it.

Best Answer

Robotframework has several built-in libraries that add a lot of functionality. Two that you can use for this task are the OperatingSystem library and the String library.

You can use the keyword Get File from the OperatingSystem library to read the file, and you can use the Split to Lines keyword from the String library to convert the file contents to a list of lines. Then it's just a matter of looping over the lines using a for loop.

For example:

*** Settings ***
| Library | OperatingSystem
| Library | String

*** Test Cases ***
| Example of looping over the lines in a file
| | ${contents}= | Get File | data.txt
| | @{lines}= | Split to lines | ${contents}
| | :FOR | ${line} | IN | @{lines}
| | | log | ${line} | WARN
Related Topic