-
21-12-2019 - |
题
我需要定义一个二维数组变量。
我已经能够使用列表变量,但现在我正在处理的项目需要一个数组。
这是怎么做到的?
如何“循环”二维数组?
解决方案
尝试使用列表列表。您可以使用扩展变量语法访问该项目内部列表。
*** Settings ***
Library Collections
*** Variables ***
@{colors} red green blue
@{animals} cow pig dog
@{things} ${animals} ${colors}
*** Test Cases ***
2-Dimensional List
Log List ${things}
Log The ${things[0][1]} is ${things[1][1]}
.
列表长度为2,它包含以下项目: 0:[U'cow',U'pig',U'Dog'] 1:[你',U'Green',U'Blue']
猪是绿色的
其他提示
使用列表
Robot 对数组变量的术语是“列表”。您可以使用 @{...}
将变量指定为列表。下面的示例展示了如何在变量表中创建列表,以及如何在测试中使用 创建列表 关键词:
*** Variables ***
| # create a list in a variable table
| @{Colors} | red | orange | green | blue
*** Test Cases ***
| Example of using lists
| | # create an array inside a test
| | @{Names}= | Create list | homer | marge | bart
| | # Verify that the elements are arrays
| | Length should be | ${Colors} | 4
| | Length should be | ${Names} | 3
要创建二维列表,您可以创建列表的列表:
| | ${array}= | Create list | ${Names} | ${Colors}
这 扩展变量语法 允许您访问各个元素:
| | log | element 1,1: ${array[1][1]}
有关更多信息,请参阅标题为 列出变量 在里面 机器人框架用户指南
使用词典
您可以使用字典来模拟多维数组。例如:
*** Settings ***
| Library | Collections
*** Test Cases ***
| Example of using a dictionary to simulate a two dimensional array
| | ${array}= | Create dictionary
| | ... | 0,0 | zero, zero
| | ... | 0,1 | zero, one
| | ... | 1,0 | one, zero
| | ... | 1,1 | one, one
| | Should be equal | ${array["1,0"]} | one, zero
我找到了一种方法来循环列表:
*** Settings ***
Library Collections
*** Variables ***
@{colors} red green blue
@{animals} cow pig dog
@{things} ${animals} ${colors}
*** Test Cases ***
Nested for loop example
: FOR ${x} IN @{animals}
\ Keyword with for loop ${x}
*** Keywords ***
Keyword with for loop
[Arguments] ${x}
:FOR ${y} IN @{colors}
\ Log The ${x} is ${y}
.
信用进入 Ombre42 。谢谢!
不隶属于 StackOverflow