Looping through the content of a file in RobotFramework

时间秒杀一切 提交于 2020-01-01 05:50:09

问题


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.


回答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



回答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!




回答3:


Below is a list of different examples how to use FOR & While loops in your Robot Framework Test Cases.

http://robotframework.googlecode.com/svn/tags/robotframework-2.5.3/atest/testdata/running/for.txt




回答4:


My strategy, that I've used successfully with .csv files, would be to create a Python-based keyword that will grab the nth item in a file. The way I did it involved importing the CSV Python library, so to give a more complete answer I'd have to know what file type you're trying to read from.



来源:https://stackoverflow.com/questions/23193216/looping-through-the-content-of-a-file-in-robotframework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!