Looping through the content of a file in RobotFramework
Solution 1
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
Solution 2
This solve my issue same like yours !
${File}= Get File Path\\FileName.txt
@{list}= Split to lines ${File}
:FOR ${line} IN @{list}
\ Log ${line}
\ ${Value}= Get Variable Value ${line}
\ Log ${Value}
I am reading from 'text' file and 'Get Variable Value' is part of builtin library. Thanks!
chingupt
Updated on August 01, 2020Comments
-
chingupt over 3 years
How can I loop through the contents of a file within Robot Framework?
My file contents would be like this:
1001
1002
1003
1004I want to read the contents one by one, assign it to a variable and then do some operations with it.
-
chingupt about 10 yearsThanks Mario, but it does not tell me how to read from a file directly and loop through the contents. For e.g. the way we do in shell scripts: for var in 'cat file`; do something done;
-
chingupt about 10 yearscan i read through the file similiar to what is explained here?stackoverflow.com/questions/17237464/…
-
Mario Galea about 10 yearsYou can read through the file using the robot.libraries.OperatingSystem library. There's a particular method which fits your request: def grep_file(self, path, pattern, encoding='UTF-8'): Check out the library: robot-framework.readthedocs.org/en/2.7.7/_modules/robot/…
-
DPalharini about 3 yearsThe FOR loop is in the old syntax of robot framework. The new version does not required the ':'.
-
Russell Smith almost 2 yearsFWIW, the link in this answer is now dead.