下一篇:https://www.jianshu.com/p/0afa59b25ab1
一、需要使用到的库:
1、Library HttpLibrary.HTTP——sudo pip install robotframework-HttpLibrary
2、RequestsLibrary库的 执行 pip install robotframework-requests
3、requests库的 执行pip install requests
4、DatabaseLibrary库 执行pip install robotframework_databaselibrary
5、json
6、Collections
二、Robot Framework接口测试的常用关键字(不区分大小写)
${test1}:scalar #可以理解成单值变量
@{test2}:list #多值变量
&{test3}:dict #字典
具体用法下面会有。
1、log:输出
>>> ${test1} set variable 1
>>> ${test2} set variable ${1}
>>> log ${result} #代表输出${result}参数
>>> log ${test2}
>>> ${type1} evaluate type(${test1})
>>> ${type2} evaluate type(${test2})
输出:
1
1
${type1} =<type 'int'>
${type2} =<type 'int'>
对比发现1或者${1}两种方式赋值输出的类型都为整形
>>> ${test1} set variable 'www'
>>> log ${test1}
>>> ${test2} set variable www
>>> log ${test2}
>>> ${type1} evaluate type(${test1})
>>> ${type2} evaluate type(${test2})
输出:
${test1} = 'www'
'www'
${test2} = www
www
${type1} =<type 'str'>
Evaluating expression 'type(www)' failed: NameError: name 'www' is not defined
不加单引号的字符串没有类型
2、log many:类似于log 关键字,不同的是,log关键字只可以接收一个参数,而log many 关键字可以同时接收多个参数
>>> ${test1} set variable 'www'
>>> ${test2} set variable www
>>> log many ${test1} ${test2}
输出:
${test1} = 'www'
${test2} = www
'www'
www
...可以一次输出多个参数
3、set variable和set variable if :通常最主要的方式就是使用它对变量进行赋值
>>> ${test1} set variable 'www'
>>> ${test2} set variable if ${test1}=='www' 'aaa' 'bbb'
>>> ${result3} set variable 1 2 3 4
>>> @{result4} set variable 1 2 3 4
>>> log ${test2}
输出:
${test1} = 'www'
${test2} = 'aaa'
${result3} = ["1", "2", "3", "4"]
@{result4} = [ 1 | 2 | 3 | 4 ]
'aaa'
...set variable if 表示${test1}=='www'为真,将aaa赋值给 ${test2}。为假,将bbb赋值给 ${test2}。
4、${testurl["yyq"]}、${testurl} 获取&里的内容
首先创建一个dict变量。
>>> &{testurl} yyq=https://www.baidu.com/ yyq2=https://www.taobao.com/ #这个代码最好还是在RF中创建new dictionary variable。
>>> ${test1} set variable ${testurl["yyq"]}#必须使用$
>>> ${test2} set variable ${testurl} #必须使用$
>>> log ${test1}
>>> log ${test2}
输出:
${test1} = https://www.baidu.com/
${test2} = {"yyq": "https://www.baidu.com/", "yyq2": "https://www.taobao.com/"}
https://www.baidu.com/
{"yyq": "https://www.baidu.com/", "yyq2": "https://www.taobao.com/"}
注意:需要使用到&{testur},在case中需要把它变成 ${testurl}。如果创建一个同名参数${testurl}。则优先使用${testurl}
5、获取$变量里的内容
首先创建一个scalar变量。
>>> ${scalartest} hello
#这个代码最好还是在RF中创建new scalar。
>>> ${test1} set variable ${scalartest}
>>> log ${test1}
输出:
${test1} = hello
hello
6、@{listtest} =${listtest}、${listtest[0]}、get from list ${listtest} 0 : 获取@变量里的内容
首先创建一个list变量。
>>> @{listtest} 'a' b c
#这个代码最好还是在RF中创建new list variable。
>>> ${test1} set variable @{listtest} #可以使用@或$。这里和dict不同
>>> ${test2} set variable ${listtest[0]} #必须使用$
>>> ${test3} set variable ${listtest[1]}
>>> ${test4} get from list ${listtest} 0 #这种方式也可以从list中取出对应位置对值
>>> log many ${test1} ${test2} ${test3} ${test4}
>>> ${type2} evaluate type(${test2})
>>> ${type3} evaluate type(${test3})
输出:
${test1} = [" 'a' ", "b", "c"]
${test2} = 'a'
${test3} = b
${test4} = 'a'
[" 'a' ", "b", "c"]
'a'
b
'a'
${type2} =<type 'str'>
Evaluating expression 'type(b)' failed: NameError: name 'b' is not defined
上面例子可以看出:最好就使用$去调用参数。且声明参数如果为字符时,最好加上单引号,否则类型会出错。如果三阿拉伯数字,不加单引号为in类型,加了单引号为str。
create list:创建list
>>> @{varlist} create list 1 a b
>>> log ${varlist}
>>> log ${varlist[2]}
输出:
@{varlist} = [ 1 | a | b ]
["1", "a", "b"]
b
7、create dictionary:创建字典参数${params}
>>> ${params} create dictionary orderCode=aa #必须为${},不能@{}
>>> log ${params}
>>> log ${params["orderCode"]}
输出:
${params} = {"orderCode": "aa"}
{"orderCode": "aa"}
aa
8、set to dictionary :增加字典参数(set to dictionary ${params} key value)
>>> ${params} create dictionaryorderCode=aa
>>> log ${params}
>>> set to dictionary ${params} a b #${params}添加一对数据
>>> log ${params}
输出:
${params} = {"orderCode": "aa"}
{"orderCode": "aa"}
{"orderCode": "aa", "a": "b"}
9、变量的作用域
1、set global variable:设置全局变量
2、set suite variable:设置suite变量
3、set test variable:设置test变量
issuite1.yyq1:
>>> set global variable ${test1} 'global'
>>> set suite variable ${test2} 'suite'
>>> set test variable ${test3} 'test'
issuite1.yyq2:
>>> log ${test1}
>>> log ${test2}
>>> log ${test3}
issuite2.istestcase:
>>> log ${test1}
>>> log ${test2}
>>> log ${test3}
输出:
Issuite1.yyq1:
${test1} = 'global'
${test2} = 'suite'
${test3} = 'test'
Issuite1.yyq2:
'global'
'suite'
'${test3}' not found. Did you mean: ${test2} ${test1}
Issuite2.istestcase:
'global'
'${test2}' not found. Did you mean: ${test1}
如上图所示:issuite1、issuite2都是suite,yyq1、yyq2、istestcase都是testcase简称test
global variable:可以在不同的suite中的testcase中调用
suite variable:可以在同一个suite中不同的testcase中调用
test variable:只能在同一个suite中同一个testcase
10、evaluate:这个方法很好用,经常需要使用到。可以理解成导入python语法
例1:
>>> ${year} evaluate random.randint(1000,2000) random
>>> log ${year}
输出:
${year} = 1082
1082
random.randint(1000,2000) 代表随机取1000-2000之间任意的整数。
例2:
>>> ${connect} evaluate ["a={},b={},c={}".format(i,i+1,i+2) for i in range(2,6,2)]
>>> log ${connect}
输出:
${connect} =["a=2,b=3,c=4", "a=4,b=5,c=6"]
["a=2,b=3,c=4", "a=4,b=5,c=6"]
11、if语句:run keyword if
>>> ${result} set variable 1
>>> run keyword if ${result}==1 log success
输出:
${result} = 1
success
11.1多个条件判断and:
>>> ${result} set variable 1
>>> ${result2} set variable 2
>>> run keyword if '${result}'=='1'and'${result2}'=='2' log success
输出:
${result} = 1
${result2} = 2
success
11.2多个条件判断or:
>>> ${result} set variable 1
>>> ${result2} set variable 2
>>> run keyword if '${result}'=='1'or'${result2}'=='3' log success
输出:
${result} = 1
${result2} = 2
success
11.3ELSE IF:必须大写
>>> ${result} set variable 1
>>> ${result2} set variable 2
>>> run keyword if '${result}'=='3'or'${result2}'=='3' log success
>>> ... ELSE IF ${result}<1 log 'result<1'
>>> ... ELSE IF ${result}<0 log 'result'<0'
>>> ... ELSE log 'result<2'
输出:
${result} = 1
${result2} = 2
'result<2'
11.4Run Keywords:执行多个语句( Run Keywords AND)。AND必须大写
>>> ${result} set variable 1
>>> ${result2} set variable 2
>>> run keyword if '${result}'=='1'or'${result2}'=='3' Run Keywords log success AND log 'b' AND log 1
输出:
${result} = 1
${result2} = 2
success
'b'
1
11.5用IF关键字给变量赋值
>>> ${result} set variable 1
>>> ${result2} set variable 2
>>> ${result3} run keyword if ${result}==2 set variable 'right'
>>> ... ELSE set variable 'error'
>>> log ${result3}
输出:
${result} = 1
${result2} = 2
${result3} = 'error'
'error'
也可以使用上文提到的set variable if 。
11.6Unless关键字:当判断条件为'false'时,后面的语句才会执行
>>> ${result} set variable 1
>>> ${result3} run keyword Unless ${result}==2 set variable 'right'
输出:
${result} = 1
${result3} = 'right'
'right'
(使用Unless之后不能后面加ELSE,会出错)如:
>>> ${result} set variable 1
>>> ${result3} run keyword Unless ${result}==2 set variable 'right'
>>> ...ELSE set variable 'error'
>>> log ${result3}
输出:
${result} = 1
${result3} = ["'right'", "ELSE", "set variable", "'error'"]
["'right'", "ELSE", "set variable", "'error'"]
12、FOR循环
12.1列出所有值
>>> :FOR ${result} IN 1 2 a
>>> \ log ${result} #注意一定要加\表示锁进,后面的代码在for的作用域中
输出:
1
2
a
12.2从列表中取值
>>> ${result} set variable 1 2 3 4
>>> :FOR ${result2} IN ${result}
>>> \ log ${result2}
输出:
${result} = ["1", "2", "3", "4"]
["1", "2", "3", "4"]
12.3多重循环:需要通过关键字传递做到。
*** Keywords ***
>>> for1
>>> [Arguments] ${row}
>>> : FOR ${cell} IN ${row}
>>> \ log ${cell}
>>> for2
>>> [Arguments] ${table}
>>> : FOR ${row} IN ${table}
>>> \ for1 ${row}
*** Test Cases ***
>>> yyq1
>>> ${result} set variable 1 2 3 4
>>> for2 ${result}
输出:
${result} = ["1", "2", "3", "4"]
["1", "2", "3", "4"]
12.4 in range
>>> : FOR ${result} in range 5 #取0-4的整数
>>> \ log ${result}
>>> : FOR ${result2} in range 1 5 #取1-4的整数
>>> \ log resule2=${result2}
>>> : FOR ${result3} in range 1 15 3 #取1-14之间,间隔3位的整数(1,4,7,10,13)
>>> \ log resule3=${result3}
输出:
0
1
2
3
4
resule2=1
resule2=2
resule2=3
resule2=4
resule3=1
resule3=4
resule3=7
resule3=10
resule3=13
12.5中断循环:Exit For Loop 或者 Exit For Loop If
>>> :FOR ${result} in range 5
>>> \ run keyword if ${result}==2 exit for loop
>>> \ log ${result}
输出:
0
1
Exiting for loop altogether.
>>> :FOR ${result} in range 5
>>> \ exit for loop if ${result}==2
>>> \ log ${result}
输出:
0
1
Exiting for loop altogether.
12.6 跳过循环体中某些进程:Continue For Loop或者Continue For Loop If。用法和上面的中断循环类似,这里举一个例子
>>> :FOR ${result} in range 5
>>> \ continue for loop if ${result}==2
>>> \ log ${result}
输出:
0
1
Continuing for loop from the next iteration.
3
4
12.7 repeat keyword:设置执行次数
>>> repeat keyword 3 log hello #执行3次,每次执行log hello
输出:
Repeating keyword, round 1/3.
hello
Repeating keyword, round 2/3.
hello
Repeating keyword, round 3/3.
hello
13、连接数据库
首先在需要连接数据库的suite中添加DatabaseLibrary
>>> Connect To Database Using Custom Params pymysql database='finance', user='root', password='sddd1111', host='111.21.13.141', port=3306,charset='utf8'
>>> ${result} query select customer_name from dispose where id=2
输出:
[["李四"]]
Connect To Database Using Custom Params pymysql: 为固定写法。
database='finance':代表数据库名。
query:固定写法,后面加上操作数据库的语句。
pymysql和query后面的语句太长,可以写在变量中,通过变量调用。
连接数据库以及操作数据库也可简单封装成一个关键字(方法)
[Teardown] Disconnect From Database :Teardown代表此关键字结束。结束时执行断开数据库连接的操作
[Return] ${result}:返回变量${result}
从数据库取数据中文乱码解决:
1.在连接数据库的语句中加入, charset='utf8。例如:database='databasename', user='root', password='111111', host='132.23.14.151', port=3306,charset='utf8' #以上数据库随意填写的,根据自己的数据库填写对应参数。
2.找到Python安装目录下的\Lib\site-packages\robot\utils\unic.py文件,这是mac。
3.引入json库:import json
4. 将下面代码复制到如图位置,注意对齐方式:
if isinstance(item, (list, dict, tuple)): try: item = json.dumps(item, ensure_ascii=False, encoding='cp936') except UnicodeDecodeError: try: item = json.dumps(item, ensure_ascii=False, encoding='cp936') except: pass except: pass
5.然后重启下RIDE
如何插入和删除数据:Execute Sql String
Execute Sql String INSERT INTO city(NAME,countrycode,district,population) VALUES('beijing' ,'ZH','China',217100
Execute Sql String delete from city where NAME='beijing'
14、get请求:
Get HTTP请求:
>>> Create Session httpbin http://httpbin.org
>>> &{params}= Create Dictionary key=value key2=value2
>>> ${resp}= Get Request httpbin /get params=${params}
>>> Should Be Equal As Strings ${resp.status_code} 200 #Should Be是断言,判断参数和200是否相等
>>> ${jsondata}= To Json ${resp.content} #转化为json格式
>>> Should be Equal ${jsondata['args']} ${params}
Get HTTPS请求(获取HTTPS和验证证书):
>>> Create Session httpbin https://httpbin.org verify=True
>>> ${resp}= Get Request httpbin /get
>>> Should Be Equal As Strings ${resp.status_code} 200
15、post 请求:
Post 请求不带数据 :
>>> Create Session httpbin http://httpbin.org
>>> ${resp}= Post Request httpbin /post
>>> Should Be Equal As Strings ${resp.status_code} 200
Post 请求带数据 :
>>> Create Session httpbin http://httpbin.org
>>> &{params}= Create Dictionary key=value key2=value2
>>> ${resp}= Post Request httpbin /post params=${params}
>>> Should Be Equal As Strings ${resp.status_code} 200
Post 请求带数据和header:
>>> Create Session httpbin http://httpbin.org
>>> &{data}= Create Dictionary name=bulkan surname=evcimen #创建字典
>>> &{headers}= Create Dictionary Content-Type=application/x-www-form-urlencoded
>>> ${resp}= Post Request httpbin /post data=${data} headers=${headers}
>>> Dictionary Should Contain Value ${resp.json()['form']} bulkan
>>> Dictionary Should Contain Value ${resp.json()['form']} evcimen
注意:Suite Teardown Delete All Sessions #删除Sessions,在suite中设置结束suite时执行删除sessions,如下图:
下一篇:https://www.jianshu.com/p/0afa59b25ab1