二、robotframework接口测试-常用关键字介绍
1.常用关键字介绍:
a. 打印:log 用法:log 打印内容
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
b.定义变量:Set Variable 用法:${name} Set Variable hello world
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
c.创建字典:Create Dictionary 用法:${dict} Create Dictionary a=1 b=2 或 ${dict} Create Dictionary a 1 b 2
例子:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
d.post请求发送:requests.Post 用法:${resp}= requests.Post ${test_url} ${postdata}
打印返回内容:log ${resp.text}
例子:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
e.关键字之断言:
断言一:比较字符串:should be equal as strings and should not be equal as strings
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
f.生成随机数字
${num} Evaluate random.randint(10000,99999) random
log ${num}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
g.生成随机字符串
${str} evaluate string.ascii_letters string #大小写字母全部
#${str} evaluate string.ascii_lowercase string #小写字母
log ${str}
${len} evaluate len('${str}')
log ${len}
${num} set variable 10
${name} set variable 2018
: FOR ${index} IN RANGE ${num}
\ ${i} evaluate random.randint(0,int(${len})-1) random
\ ${tmp} set variable ${str[int(${i})-1]}
\ ${newStr} set variable ${name}${tmp}
log ${name}
--------------------------------------------------输出:
20180717 17:12:59.286 : INFO : ${str} = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
20180717 17:12:59.287 : INFO : abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
20180717 17:12:59.289 : INFO : ${len} = 52
20180717 17:12:59.290 : INFO : 52
20180717 17:12:59.291 : INFO : ${num} = 10
20180717 17:12:59.292 : INFO : ${name} = 2018
20180717 17:12:59.294 : INFO : ${i} = 13
20180717 17:12:59.295 : INFO : ${tmp} = m
20180717 17:12:59.297 : INFO : ${newStr} = 2018m
20180717 17:12:59.298 : INFO : ${i} = 32
20180717 17:12:59.300 : INFO : ${tmp} = F
20180717 17:12:59.302 : INFO : ${newStr} = 2018F
20180717 17:12:59.304 : INFO : ${i} = 13
20180717 17:12:59.305 : INFO : ${tmp} = m
20180717 17:12:59.307 : INFO : ${newStr} = 2018m
20180717 17:12:59.309 : INFO : ${i} = 50
20180717 17:12:59.310 : INFO : ${tmp} = X
20180717 17:12:59.311 : INFO : ${newStr} = 2018X
20180717 17:12:59.313 : INFO : ${i} = 42
20180717 17:12:59.314 : INFO : ${tmp} = P
20180717 17:12:59.315 : INFO : ${newStr} = 2018P
20180717 17:12:59.318 : INFO : ${i} = 48
20180717 17:12:59.319 : INFO : ${tmp} = V
20180717 17:12:59.321 : INFO : ${newStr} = 2018V
20180717 17:12:59.322 : INFO : ${i} = 37
20180717 17:12:59.324 : INFO : ${tmp} = K
20180717 17:12:59.325 : INFO : ${newStr} = 2018K
20180717 17:12:59.327 : INFO : ${i} = 36
20180717 17:12:59.328 : INFO : ${tmp} = J
20180717 17:12:59.329 : INFO : ${newStr} = 2018J
20180717 17:12:59.331 : INFO : ${i} = 34
20180717 17:12:59.333 : INFO : ${tmp} = H
20180717 17:12:59.334 : INFO : ${newStr} = 2018H
20180717 17:12:59.336 : INFO : ${i} = 49
20180717 17:12:59.337 : INFO : ${tmp} = W
20180717 17:12:59.339 : INFO : ${newStr} = 2018W
20180717 17:12:59.340 : INFO : 2018
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
h.循环关键字: :FOR ${I} IN @{list}
log ${i}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
j.rf中resp.text和resp.content区别:
requests.content返回的是二进制响应内容
而requests.text则是根据网页的响应来猜测编码,如果服务器不指定的话,默认编码是"
ISO-8859-1"(我当初看到这里的时候,在想为啥默认编码不设置为utf8呢,然后看到了原来是http协议是这样的,所以...)所以这是为什么你用 response.text 返回的是乱码的原因。
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
k.Append to List关键字:
语法:Arguments:[ list_ | *values ]
例子:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
l.if条件从句: ${a} Run Keyword If 条件 Set Variable 1 ELSE Set Variable 2
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
m.Set Variable If 用法:${a} Set Variable If 条件 结果1 结果2
${c} Set Variable 0
${result1} Set Variable If ${c}==0 结果为0 结果不为0
${result1} Set Variable If ${c}>0 value1 value2
${result1} Set Variable If ${c}<0 value3 value4