我有以下文本文件。
[box_1]
show ethernet
show adjacency
show log
[box_2]
run ethernet
run adjacency
show log
我需要编写一个机器人文件,如果遇到[box_1],它将运行,
run ethernet
run adjacency
show log
如果遇到[box_2],它将在其下运行以下命令。
这是我的机器人代码:
${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}
但它只识别[box_1]而不是[box_2]。并且命令后面可以有多个方框。 有人可以帮忙吗?
感谢。
答案 0 :(得分:1)
您只能在
中分配${op}
一次
${op}= Get From List ${list} 0
永远不会改变它。这就是为什么它总是[box_1]
而永远不会成为[box_2]
。您必须将赋值转换为FOR
- 循环并更改其余代码,以便执行不同box
- es之间的命令。
答案 1 :(得分:0)
虽然@Psytho是对的,但解决问题还不够。
在下面的示例中,我添加了所需的库和自定义关键字来模仿运行命令Operatingsystem
关键字。
在这个例子中,我跳过标题并执行命令。以类似的方式,您可以使用${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}
编辑:添加了第二个仅依赖正则表达式的选项。
答案 2 :(得分:0)
*** Variables ***
${A} A
${B} B
${C} c
*** Keywords ***
Test
Log To Console ${A}
Log To Console ${B}
Log To Console ${C}
[Return] ${A} ${B} ${C}
*** Test Cases ***
Test1
${A0} ${B0} ${C0} Test
Log To Console ${A0}
Log To Console ${B0}
Log To Console ${C0}
When you are calling the keyword in the test case u can assign the return variables as above.`