robotframework 自定义library

作用说明:本例中自定义的library作用就是从Redis中获取短信验证码。

 

1.在D:\I_python\Python27\Lib\site-packages(这个路径一定要在系统path路径中)下面创建一个CustomRedisClient文件夹

2.在该文件夹下创建两个文件,分别是__init__.py以及getcodefromredis.py(其中需要先安装redis和regex)

代码如下

getcodefromredis.py:

 1 #-*- coding:utf-8 -*-
 2 '''
 3     created by hch 2016-12-7
 4 '''
 5 import redis
 6 import regex
 7 
 8 __version__ = '0.1'
 9 
10 class Getcodefromredis(object):
11 
12     def getsmscode(self,host,port,dbindex,keyvalue):
13         '''获取redis中的code。例
14             | getsmscode          | host               | port   | dbindex   | phonenum   |
15         '''
16         
17         print host
18         print port
19         print dbindex
20         print keyvalue
21         r=redis.StrictRedis(host,port,dbindex)
22         
23         value=r.get(keyvalue)
24         valuecontent = regex.findall(r"\d{6}",value)[0]
25         print valuecontent

__init__.py:

 1 #-*- coding:utf-8 -*-
 2 '''
 3     created by hch 2016-12-8
 4 '''
 5 
 6 
 7 
 8 from getcodefromredis import Getcodefromredis
 9 
10 
11 __version__ = '0.1'
12 
13 class CustomRedisClient(Getcodefromredis):   
14     ROBOT_LIBRARY_SCOPE = 'GLOBAL'

 3. 重新启动ride,并添加library

4.通过F5查看library以及key说明

5.编写用例

6. 执行结果

 

posted @ 2016-12-08 09:07  月色深潭  阅读(5261)  评论(0编辑  收藏  举报