您的位置:首页 > 数据库

Robot Framework 教程 - 连接Oracel数据库

2016-07-21 11:28 525 查看


Robot Framework 教程 - 连接Oracel数据库

2015年08月27日 10:31供稿中心: 课工场

摘要:

Robot Framework 提供了多种Library。其中Database Library可用来连接操作数据库。

1.安装Database Library

打开Robot Framework官网,找到Database Library,官网提供了Java 和Python两个版本的Database Library,这里我使用的是Python版,地址为
http://franz-see.github.io/Robotframework-Database-Library/
其中的软件Requirement一节是Database Library的运行环境需求,具体需求包括

[align=left]Python/Jython/IronPython[/align]
[align=left]Robotframework[/align]
[align=left]A Database API Specification 2.0 Python Module that you will use to connect to the database.[/align]

其中的Python与Robot Framework我们已经安装过,现在需要安装一个Database API Specification 2.0 Python Module,也就是用于连接数据库的Python模块。

这里的逻辑是这个样子的,Database Library实际上是一个处在Robot Framework和Database Interfaces 中间的代理模块,Robot Framework 通过Database Library 所提供的接口,间接的调用Database Interfaces,从而实现操作数据库的目的。



打开Database Interfaces页面,找到Oracle的API驱动及下载地址,找到你对目前python环境对应版本的安装文件,下载安装

之后,下载安装DatabaseLibrary。

安装完成后,在你的 Python27Libsite-packages目录下,可以看到Database Library文件夹和cx_Oracle.pyd文件



2.编写测试脚本

运行RIDE,按F5查看Database Library的关键字。



我们编写一个数据库查询的测试用例,脚本如下:





Connect To Database Using Custom Params    cx_Oracle    'bpm','bpm','orcl'@{data} 
  query    select * from sys_userlog many    @{data}log    ${data[0][1]}${count}    Get length    ${data}log    ${count}Disconnect From Database





运行脚本,结果如下



运行后查看Log,表中的中文全部变成了乱码。cx_Oracle的默认编码不是Utf-8,需要人工转换一下。DataBaseLibrary自身没有提供转换函数,为了解决这个问题,我们需要对DataBaseLibrary进行扩展。

3.解决Database Library中文乱码问题

打开安装目录下的 query.py文件,添加一个decode函数,用于对字符串进行解码

def decode(self,customstr,mode): 
     return customstr.decode(mode)

更改测试脚本,增加转码过程



再次运行,得到系统输出,测试通过



总结:

至此,完成了Robot Framework连接数据库并进行操作的过程。具体在使用过程中,还需要针对业务逻辑设计TestCase、编写sql来实现。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: