Robot Framework数据库连接
安装
1、安装DatabaseLibrary,进入CMD命令窗口切换到Python目录:
安装之前可以使用pip list 查看是否安装一下依赖包,如果有的话就跳过该步骤
1 | pip install robotframework-databaselibrary |
安装pymysql1
pip install pymysql
2、安装数据库驱动1
pip install pyodbc
3、安装mysql-connector-odbc
下载地址:http://dev.mysql.com/downloads/connector/odbc/
进入之后点击:Looking for previous GA versions?如图所示:
选择64位ZIP或者MSI进行下载(注意:这个选择多少位对应你自己的python安装的是多少位的,如果下载错误可以导致后面连接的时候报错)
下载完成之后解压。双击:setup.bat运行
安装成功后进入控制面板–管理工具–数据源–点击添加就可以看到安装成功的数据源
选择:MySQL ODBC 5.3 Unicode Driver
出现如图所示配置数据库
配置完成之后可以连接‘Test’按钮测试一下是否可以准确连接
使用
1、在robotframework中导入databaselibrary包
2、连接数据库1
2
3 1 Connect To Database Using Custom Params pymysql host='10.165.117.109',port=3307,user='***',password='****L',database='snow_humres_test',charset='utf8'
2 ${reslut} Query select * from emp_main where emp_code='GS0001'
3 log ${reslut}
3、运行结果如图:
4、查询结果字符处理
将下面代码复制到C:\Python27\Lib\site-packages\robot\utils\unic.py文件中(请先备份一份原有的,以备未更新成功之需)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100# Copyright 2008-2015 Nokia Networks
# Copyright 2016- Robot Framework Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from pprint import PrettyPrinter
from .platform import IRONPYTHON, JYTHON, PY2
from .robottypes import is_bytes, is_unicode
import json
if PY2:
def unic(item):
if isinstance(item, unicode):
return item
if isinstance(item, (bytes, bytearray)):
try:
return item.decode('ASCII')
except UnicodeError:
return u''.join(chr(b) if b < 128 else '\\x%x' % b
for b in bytearray(item))
if isinstance(item, (list, dict, tuple)):
try:
item = json.dumps(item, ensure_ascii=False, encoding='utf-8')
except UnicodeDecodeError:
try:
item = json.dumps(item, ensure_ascii=False, encoding='gbk')
except:
pass
except:
pass
try:
try:
return unicode(item)
except UnicodeError:
return unic(str(item))
except:
return _unrepresentable_object(item)
else:
def unic(item):
if isinstance(item, str):
return item
if isinstance(item, (bytes, bytearray)):
try:
return item.decode('ASCII')
except UnicodeError:
return ''.join(chr(b) if b < 128 else '\\x%x' % b
for b in item)
try:
return str(item)
except:
return _unrepresentable_object(item)
# JVM and .NET seem to handle Unicode normalization automatically. Importing
# unicodedata on Jython also takes some time so it's better to avoid it.
if not (JYTHON or IRONPYTHON):
from unicodedata import normalize
_unic = unic
def unic(item):
return normalize('NFC', _unic(item))
def prepr(item, width=400):
return unic(PrettyRepr(width=width).pformat(item))
class PrettyRepr(PrettyPrinter):
def format(self, object, context, maxlevels, level):
try:
if is_unicode(object):
return repr(object).lstrip('u'), True, False
if is_bytes(object):
return 'b' + repr(object).lstrip('b'), True, False
return PrettyPrinter.format(self, object, context, maxlevels, level)
except:
return _unrepresentable_object(object), True, False
def _unrepresentable_object(item):
from .error import get_error_message
return u"<Unrepresentable object %s. Error: %s>" \
% (item.__class__.__name__, get_error_message())
在robotframework中加入下面两行代码如图所示:
1 | Set Environment Variable NLS_LANG SIMPLIFIED CHINESE_CHINA.ZHS16GBK |
运行结果如图: