Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through the content of a file in 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.

like image 605
chingupt Avatar asked Apr 21 '14 07:04

chingupt


People also ask

How do I read the contents of a robot framework file?

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.

How do you access elements in a list in Robot Framework?

If a variable value is a list or list-like, it is also possible to use as a list variable like @{EXAMPLE}. In this case individual list items are passed in as arguments separately. The second problem is that the syntax for accessing array elements only works outside the braces if you have a single index.


1 Answers

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
like image 111
Bryan Oakley Avatar answered Oct 14 '22 15:10

Bryan Oakley