1.安装Database-Library
输入命令:pip install robotframework_databaselibrary
2.添加Database的Library
3.实例
*** Test Cases *** 查询单条数据 ${data} create dictionary shopCode=xxx coordinate= ${response} Rest.Get /api/shop/current.json ${data} form ${hosts["weidian"]} Connect To Database Using Custom Params pymysql host='115.29.xx.xxx', port=3306, user='xx', passwd='xxx', db='test', charset= 'utf8' ${name} query select nickname from authshop where code='xxx' log ${response["data"]["shop"]["shopName"]} should be equal ${response['data']['shop']['shopName']} ${name[0][0]} log ${name[0][0]} 查询多条数据 数据库中存在 #Check If Exists In Database ${data} create dictionary shopCode=xxx coordinate= ${response} Rest.Get /api/shop/current.json ${data} form ${hosts["weidian"]} Connect To Database Using Custom Params pymysql host='115.29.xx.xx', port=3306, user='test', passwd='xxx', db='test', charset= 'utf8' Check If Exists In Database select nickname from authshop where code='xxx' 数据库中不存在 #Check If Not Exists In Database ${data} create dictionary shopCode=xxx coordinate= ${response} Rest.Get /api/shop/current.json ${data} form ${hosts["weidian"]} Connect To Database Using Custom Params pymysql host='115.29.xx.xxx', port=3306, user='xx', passwd='xx', db='test', charset= 'utf8' Check If Exists In Database select nickname from authshop where code='xxx' 查询 #Description ${data} create dictionary shopCode=xxx coordinate= ${response} Rest.Get /api/shop/current.json ${data} form ${hosts["weidian"]} Connect To Database Using Custom Params pymysql host='115.29.xx.xxx', port=3306, user='xxx', passwd='xxx', db='test', charset= 'utf8' ${qeeryResults} query select nickname from authshop where code='xxx' log ${qeeryResults} 返回查询结果总行数 #Row Count ${data} create dictionary shopCode=xxx coordinate= ${response} Rest.Get /api/shop/current.json ${data} form ${hosts["weidian"]} Connect To Database Using Custom Params pymysql host='115.29.xx.xxx', port=3306, user='xxx', passwd='xxx', db='xxx', charset= 'utf8' ${qeeryResults} row count select shop_code from authshop_relation where site_id='1' log ${qeeryResults}
4.api详情
API | 参数 | 说明 |
Check If Exists In Database | 查询语句 | 数据库中存在:存在,则为PASS,不存在,则为FAIL |
Check If Not Exists In Database | 查询语句 | 数据库中不存在:不存在,则为PASS,存在,则为FAIL |
Connect To Database | dbapiModuleName=None, dbName=None, dbUsername=None, dbPassword=None, dbHost=localhost, dbPort=5432, dbConfigFile=./resources/db.cfg | 连接数据库配制信息,通过配制各项属性完成 |
Connect To Database Using Custom Params | dbapiModuleName=None, db_connect_string= | 连接数据库配制信息,通过db_connect_string来传递。如database='my_db_test', user='postgres', password='s3cr3t', host='tiger.foobar.com', port=5432 |
Delete All Rows From Table | 表名 | 删除数据库中表的所有行:删除成功,则PASS;失败,则Fail |
Description | 查询语句 | 返回查询语句的结果,内容格式为: [Column(name='id', type_code=1043, display_size=None, internal_size=255, precision=None, scale=None, null_ok=None)] [Column(name='first_name', type_code=1043, display_size=None, internal_size=255, precision=None, scale=None, null_ok=None)] [Column(name='last_name', type_code=1043, display_size=None, internal_size=255, precision=None, scale=None, null_ok=None)] |
Disconnect From Database | 无 | 断开数据库链接 |
Execute Sql Script | SqlScriptFileName | 执行脚本文件。SqlScriptFileName物理路径+FileName。 |
Execute Sql String | SqlString | 执行Sql语句 |
Query | 查询语句 | 返回查询语句的查询结果 |
Row Count | 查询语句 | 返回查询语句的查询结果行总数 |
Row Count Is 0 | 查询语句 | 查询语句的查询结果行总数:为0,则PASS;非0,则FAIL |
Row Count Is Equal To X | 查询语句,X |
查询语句的查询结果行总数:为X,则PASS;非X,则FAIL X,为预期的查询结果行数,第二个需要传递的参数 |
Row Count Is Greater Than X | 查询语句,X |
查询语句的查询结果行总数:大于X,则PASS;小于或等于X,则FAIL X,为预期的查询结果行数,第二个需要传递的参数 |
Row Count Is Less Than X | 查询语句,X |
查询语句的查询结果行总数:大于X,则PASS;小于或等于X,则FAIL X,为预期的查询结果行数,第二个需要传递的参数 |
Table Must Exist | 表名 | 表名必须存在:存在,则PASS;不存在,则FAIL |
5.实例二
#Connect To Database Using Custom Params:使用参数来连接数据库 pymysql:连接的数据库 charset=‘utf8’解决中文显示成???的问题 Connect To Database Using Custom Params pymysql host=‘115.29.10.xxx’, port=3306, user=‘xxx’, passwd=‘xxx’, db=‘xxx’,charset=‘utf8’ #使用Query方法来执行"select name from wx_account_detail where type=0”,并将查询的结果返回对变量@{name} @{name} query select name from wx_account_detail where type=0 #循环获取多条记录的值,并打印 : FOR ${i} IN RANGE 0 len(@{name}) log @{name}[${i}]
6.实例三
- 查询
把查询的结果赋给${菜单id列表}:
- 新增一条:前面是字段,后面是参数
- 新增多条数据
1.新增多条的语法,多条数据用括号分隔:insert into t_sys_lev (lev_id, lev_name, del_tag, create_time, create_user) values (("${科技层级id}", "${lev_name}","0", "${创建时间}", "${超级管理员id}"),("${科技层级id}", "${lev_name}","0", "${创建时间}", "${超级管理员id}))
2.可以把多条数据放在一个list中,用for循环调用insert语句插入
- 新增一个列表,把数据都放在一列,然后删掉多余的
然后列表界面就变成:
实现:
- 删除
- 连接数据库
封装成关键字
调用关键字,把参数传进去