[英]Iterating through a list in robot framework
I have the following text file. 我有以下文本文件。
[box_1]
show ethernet
show adjacency
show log
[box_2]
run ethernet
run adjacency
show log
I need to write a robot file, where if it encounters [box_1], it will run, 我需要编写一个机器人文件,如果遇到[box_1],它将在其中运行,
run ethernet
run adjacency
show log
And if it encounters [box_2], it will run the following commands under that. 如果遇到[box_2],它将在该命令下运行以下命令。
This is my robot code: 这是我的机器人代码:
${File}= Get File Resources\\Source\\textfile.txt
@{list}= Split to lines ${File}
Log ${list}
${op}= Get From List ${list} 0
log ${op}
${first_line}= Get Slice From List ${list} 1
:FOR ${line} IN @{first_line}
\ ${result}= Run Keyword If '${op}'=='[${box_1}]' and run_command ${line}
\ ${result}= Run Keyword If '${op}'=='[${box_2}]' and run_command ${line}
But its only taking identifying the [box_1] and not going to [box_2]. 但它仅用于标识[box_1]而不会转到[box_2]。 And there can be multiple number of boxes followed by the commands. 并且命令后面可以有多个框。 Can someone please help? 有人可以帮忙吗?
Thanks. 谢谢。
You assign ${op}
only once in 您只分配一次${op}
${op}= Get From List ${list} 0
and never change it. 永远不要改变它。 That is why it is always [box_1]
and never becomes [box_2]
. 这就是为什么它始终为[box_1]
而从不成为[box_2]
。 You must moe the assignment into FOR
-loop and change the rest of code so that the commands between different box
-es are executed. 您必须将赋值放入FOR
-loop并更改其余代码,以便执行不同box
-es之间的命令。
Although @Psytho is right, it is not entirely sufficient to solve the issues. 尽管@Psytho是正确的,但不足以解决问题。
In the below example I've added the required Libraries and a custom keyword to mimic the run command Operatingsystem
keyword. 在下面的示例中,我添加了必需的库和自定义关键字来模仿运行命令Operatingsystem
关键字。
In this example I skip the headers and execute the commands. 在此示例中,我跳过标题并执行命令。 In a similar way you can use the value in ${match[0]}
to learn which header is currently active and use it in your determination what to do. 以类似的方式,您可以使用${match[0]}
来了解哪个标题当前处于活动状态,并在确定操作时使用它。
*** Settings ***
Library String
Library Collections
Library OperatingSystem
*** Test Cases ***
TC v1
${File}= Get File textfile.txt
@{lines}= Split to lines ${File}
:FOR ${line} IN @{lines}
\ ${match} Get Regexp Matches ${line} \\[(.*)\\] 1
\ ${count} Get Length ${match}
\ ${result} Run Keyword If ${count} == 0 run_custom_command ${line}
TC v2
@{commands} Get Commands box_2 textfile.txt
:FOR ${command} IN @{commands}
\ run_custom_command ${command}
@{commands} Get Commands box_1 textfile.txt
:FOR ${command} IN @{commands}
\ run_custom_command ${command}
*** Keywords ***
Run Custom Command
[Arguments] ${command}
Log Run Command: ${command}
[return] SUCCESS
Get Commands
[Arguments] ${ENV} ${filepath}
${string}= Get File ${filepath}
${match}= Get Regexp Matches ${string} \\[${ENV}\\]\\n((.|\\n)*?)(\\[|$) 1
@{lines}= Split to lines ${match[0]}
[Return] ${lines}
EDIT: Added a second option that relies on just a regular expression. 编辑:添加了仅依赖于正则表达式的第二个选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.