RF内置库Collections
- Append To List
- Combine Lists
- Convert To Dictionary
- Convert To List
- Copy Dictionary
- Copy List
- Count Values In List
- Dictionaries Should Be Equal
- Dictionary Should Contain Item
- Dictionary Should Contain Key
- Dictionary Should Contain Value
- Dictionary Should Contain Sub Dictionary
- Get Dictionary Items
- Get Dictionary Keys
- Get Dictionary Values
- Get From Dictionary
- Get From List
- Get Index From List
- Get Match Count
- Get Matches
- Get Slice From List
- Insert Into List
- Keep In Dictionary
- List Should Contain Sub List
- List Should Contain Value
- List Should Not Contain Duplicates
- Lists Should Be Equal
- Log Dictionary
- Log List
- Pop From Dictionary
- Remove Duplicates
- Remove From Dictionary
- Remove From List
- Remove Values From List
- Reverse List
- Set List Value
- Set To Dictionary
- Should Contain Match
- Should Not Contain Match
- Sort List
内容转移至:www.xiwenqin.com 自建系统,不定时开启。
这个库,里面都是针对列表,字典的操作。这2个对象所有的需求,都能查这个库。
Append To List
列表里面加值,等价python的list.append()方法
Combine Lists
合并列表,官方示例很直观
Convert To Dictionary
转换为字典型,不过不论怎么用都报错,除非一个字典作为参数放进去,于是这个方法用不来。
Convert To List
${abc} Convert To List dsfsf
结果:
${abc} = [‘d’, ‘s’, ‘f’, ‘s’, ‘f’]
鉴于效果奇特,这个方法只能认为是切割字符串,还是每一个字符的切割。并非字面上的类型转换!!!
Copy Dictionary
先理解下概念:字典的拷贝与直接赋值的区别
这个方法就是等价python的dict.copy()方法。
使用时,浅拷贝就能区分直接赋值,明确是个新字典。但假如存在子对象(子列表,子字典),那么子对象仍然还是引用关系,会一起变。所以一般我们要用就用深拷贝
Copy List
同上,列表的直接赋值,与拷贝是有区别的。
Count Values In List
统计list里面某个值的出现次数。return出现次数。
${qq} Set Variable abcd
${qq} Convert To List ${qq}
${qq1} Count Values In List ${qq} d
运行结果:1
${qq} Set Variable abcddd
${qq} Convert To List ${qq}
${qq1} Count Values In List ${qq} d
运行结果:3
Dictionaries Should Be Equal
用于2个字典型比较,只能是2个${}型,不能是&{}型。没有返回值,不相等直接报错。
Dictionary Should Contain Item
从字典里面寻找key,value键值对,要是查到了就通过,否则直接报错。
Dictionary Should Contain Key
从字典里面寻找key,查不到就直接报错
Dictionary Should Contain Value
从字典里面寻找value,查不到就直接报错
Dictionary Should Contain Sub Dictionary
除非从dict1中找到dict2中的所有项,否则失败。
但是实际测试下来,始终都是成功:
${dict1} Create Dictionary a=1 b=2 c=3
${dict2} Create Dictionary c=4
&{dict1} Create Dictionary a=1 b=2 c=3
&{dict2} Create Dictionary a=1 b=2 c=3
${result} Dictionary Should Contain Sub Dictionary ${dict1} ${dict2}
运行结果:20200703 17:08:28.519 : INFO : ${result} = None
不是很明白为何总是成功,所以一个字典是否包含另一个字典,不知道怎么写了。
Get Dictionary Items
获取字典所有的建跟值,而且是会排序的。示例:
A系统案例1
${dict1} Create Dictionary b=2 b=1 c=3
${dict2} Create Dictionary c=4
&{dict1} Create Dictionary a=1 b=2 c=3
&{dict2} Create Dictionary a=1 b=2 c=3
${result} Get Dictionary Items ${dict1}
运行结果:
20200703 17:12:17.443 : INFO : ${result} = [‘a’, ‘1’, ‘b’, ‘2’, ‘c’, ‘3’]
从结果上看,似乎没啥用,加入不排序,说不定还有点用。
Get Dictionary Keys
${result} Get Dictionary Keys ${dict1}
上述案例数据直接吼,结果如下
20200703 17:17:49.964 : INFO : ${result} = [‘a’, ‘b’, ‘c’]
Get Dictionary Values
${result} Get Dictionary Values ${dict1}
运行结果:20200703 17:22:03.131 : INFO : ${result} = [‘1’, ‘2’, ‘3’]
Get From Dictionary
从字典中根据key 获取value
${dict1} Create Dictionary b=2 b=1 c=3
${dict2} Create Dictionary c=4
&{dict1} Create Dictionary a=1 b=2 c=3
&{dict2} Create Dictionary a=1 b=2 c=3
${result} Get From Dictionary ${dict1} b
运行结果:20200703 17:24:04.771 : INFO : ${result} = 2
Get From List
从列表里面根据下标索引,返回值
${list} Create List a b c
${result} Get From List ${list} 2
运行结果:20200703 17:29:28.995 : INFO : ${result} = c
Get Index From List
从列表里面根据值查找下标索引:
${list} Create List a b c
${result} Get Index From List ${list} c
运行结果:20200703 17:30:43.510 : INFO : ${result} = 2
Get Match Count
从列表查找,并返回匹配项的数量,可以应用通配符,和正则
${list} Create List a b c ad a
${result} Get Match Count ${list} a*
运行结果:20200703 17:36:10.519 : INFO : ${result} = 3
Get Matches
从列表里查找,并返回匹配项,可以应用通配符,和正则
${list} Create List a b c ad a
${result} Get Matches ${list} a*
运行结果:20200703 17:38:13.059 : INFO : ${result} = [‘a’, ‘ad’, ‘a’]
Get Slice From List
列表切片。返回开始下标,结束下标中间的部分
${list} Create List a b c ad a
${result} Get Slice From List ${list} 1 2
运行结果:20200703 17:42:30.328 : INFO : ${result} = [‘b’]
Insert Into List
在列表中间插入值,指定插入的下标:
${list} Create List a b c ad a
${result} Insert Into List ${list} 1 fsd
log ${list}
运行结果:20200703 17:45:03.127 : INFO : [‘a’, ‘fsd’, ‘b’, ‘c’, ‘ad’, ‘a’]
Keep In Dictionary
在字典中保留给定的键并删除所有其他键。示例:
&{dict2} Create Dictionary a=1 b=2 c=3
${result} Keep In Dictionary ${dict2} a b
log ${dict2}
运行结果:20200703 17:55:21.700 : INFO : Removed item with key ‘c’ and value ‘3’.
20200703 17:55:21.700 : INFO : {‘a’: ‘1’, ‘b’: ‘2’}
这个方法没有返回值,直接对原对象生效。另外这边也意外发现&{dict2}的值可以写成‘$’型,也能获得定义的字典。
List Should Contain Sub List
A列表包含B列表的断言
${dict1} Create lIST 1 2 3 4
${dict2} Create LIST 2 3
${result} List Should Contain Sub List ${dict1} ${dict2}
运行结果:
20200706 11:00:09.200 : INFO : ${dict1} = [‘1’, ‘2’, ‘3’, ‘4’]
20200706 11:00:09.200 : INFO : ${dict2} = [‘2’, ‘3’]
20200706 11:00:09.201 : INFO : ${result} = None
List Should Contain Value
列表是否包含某个值的断言
List Should Not Contain Duplicates
列表断言其自身不含重复项
${dict1} Create lIST 1 2 3 4 1
${dict2} Create LIST 2 3
${result} List Should not Contain Duplicates ${dict1}
运行结果:
20200706 11:05:44.639 : INFO : ${dict1} = [‘1’, ‘2’, ‘3’, ‘4’, ‘1’]
20200706 11:05:44.639 : INFO : ${dict2} = [‘2’, ‘3’]
20200706 11:05:44.640 : INFO : ‘1’ found 2 times.
20200706 11:05:44.640 : FAIL : ‘1’ found multiple times.
Lists Should Be Equal
判断2个列表是否相等,乱序会被认为不相等
Log Dictionary
相比于普通log,其区别是:按键值打印。运行结果类似下面:
20200706 11:10:17.046 : INFO :
Dictionary size is 4 and it contains following items:
a: 1
b: 2
c: 3
d: 4
该方法同样不能识别&{dict1}这种形式。
Log List
同上,漂亮打印。
Pop From Dictionary
删除指定key的键值对:
${dict1} Create Dictionary a=1 b=2 c=3 d=4
${result} Pop From Dictionary ${dict1} a
log ${dict1}
运行结果:
20200706 11:17:06.894 : INFO : ${dict1} = {‘a’: ‘1’, ‘b’: ‘2’, ‘c’: ‘3’, ‘d’: ‘4’}
20200706 11:17:06.895 : INFO : ${result} = 1
20200706 11:17:06.895 : INFO : {‘b’: ‘2’, ‘c’: ‘3’, ‘d’: ‘4’}
Remove Duplicates
移除重复的项,参数是个列表,只能对列表来操作。注意,这不影响原值,只是return
${dict1} Create List 1 1 2 1
${result} Remove Duplicates ${dict1}
log ${dict1}
运行结果:
20200706 11:23:03.160 : INFO : ${dict1} = [‘1’, ‘1’, ‘2’, ‘1’]
20200706 11:23:03.161 : INFO : 2 duplicates removed.
20200706 11:23:03.162 : INFO : ${result} = [‘1’, ‘2’]
20200706 11:23:03.162 : INFO : [‘1’, ‘1’, ‘2’, ‘1’]
Remove From Dictionary
移除字典里指定key的值,对原变量生效:
${dict1} Create Dictionary a=1 b=1 a=2 c=3
${result} Remove From Dictionary ${dict1} a
log ${dict1}
运行结果:
20200706 11:32:27.526 : INFO : ${dict1} = {‘a’: ‘2’, ‘b’: ‘1’, ‘c’: ‘3’}
20200706 11:32:27.526 : INFO : Removed item with key ‘a’ and value ‘2’.
20200706 11:32:27.526 : INFO : ${result} = None
20200706 11:32:27.527 : INFO : {‘b’: ‘1’, ‘c’: ‘3’}
Remove From List
移除列表的指定下标的值,对原变量生效。
${dict1} Create list 1 1 2 3 1
${result} Remove From List ${dict1} 1
log ${dict1}
运行结果:
20200706 11:35:01.732 : INFO : ${dict1} = [‘1’, ‘1’, ‘2’, ‘3’, ‘1’]
20200706 11:35:01.732 : INFO : ${result} = 1
20200706 11:35:01.733 : INFO : [‘1’, ‘2’, ‘3’, ‘1’]
Remove Values From List
移除列表里指定值,可以是多个复数。对于相同value值也都会被移除,对原变量生效。
${dict1} Create list 1 1 2 3 1
${result} Remove Values From List ${dict1} 1 2
log ${dict1}
运行结果:
20200706 14:08:00.268 : INFO : ${dict1} = [‘1’, ‘1’, ‘2’, ‘3’, ‘1’]
20200706 14:08:00.268 : INFO : ${result} = None
20200706 14:08:00.268 : INFO : [‘3’]
Reverse List
反转列表值的排列,对原变量生效,示例:
${dict1} Create list 1 1 2 3 1
${result} Reverse List ${dict1}
log ${dict1}
运行结果:
20200706 14:10:24.627 : INFO : ${dict1} = [‘1’, ‘1’, ‘2’, ‘3’, ‘1’]
20200706 14:10:24.627 : INFO : ${result} = None
20200706 14:10:24.627 : INFO : [‘1’, ‘3’, ‘2’, ‘1’, ‘1’]
Set List Value
修改列表指定下表的值,对原变量生效:
${dict1} Create list 1 1 2 3 1
${result} Set List Value ${dict1} 1 6
log ${dict1}
运行结果:
20200706 14:11:34.381 : INFO : ${dict1} = [‘1’, ‘1’, ‘2’, ‘3’, ‘1’]
20200706 14:11:34.381 : INFO : ${result} = None
20200706 14:11:34.381 : INFO : [‘1’, ‘6’, ‘2’, ‘3’, ‘1’]
Set To Dictionary
修改或增加字典键值对。示例:
${dict1} Create Dictionary a=1 b=2 c=3
${result} Set To Dictionary ${dict1} c 6 d 4
log ${dict1}
运行结果:
20200706 14:15:55.806 : INFO : ${dict1} = {‘a’: ‘1’, ‘b’: ‘2’, ‘c’: ‘3’}
20200706 14:15:55.815 : INFO : ${result} = {‘a’: ‘1’, ‘b’: ‘2’, ‘c’: ‘6’, ‘d’: ‘4’}
20200706 14:15:55.815 : INFO : {‘a’: ‘1’, ‘b’: ‘2’, ‘c’: ‘6’, ‘d’: ‘4’}
另一种写法:Set To Dictionary ${dict1} c=6 d=4 也行
Should Contain Match
Should Not Contain Match
这2个是用于list的模式匹配,用作断言。不是很建议用,有经验的可以用。
默认情况下,模式匹配类似于shell中的匹配文件,并且区分大小写和空格。在模式语法中,*匹配任何和?匹配任何单个字符。您还可以在模式前面加上glob=以显式使用此模式匹配行为
如果在模式前面加上regexp=,那么将根据Python re module正则表达式语法使用模式。重要提示:反斜杠是转义字符,必须用另一个反斜杠转义(例如,regexp=\d{6}来搜索\d{6})。看到了吗内置。应该Match Regexp
以获取更多详细信息。如果不区分大小写的值是真的(请参见“Boolean arguments”),模式匹配将忽略大小写;如果将不区分空白的值指定为真值(请参见“Boolean arguments”),则模式匹配将忽略空白。匹配模式时,将忽略列表中的非字符串值
Sort List
对列表排序。