三、robotframework封装的关键字-数据库使用

1.      从数据库查询结果:

     数据库连接:Connect To Database Using Custom Params           用法:Connect To Database Using Custom Params   pymysql   host='${base_ip}',user='root',passwd='aaaa',db='aaaa',port=3306,charset='utf8'

     数据库查询:query                                                                        用法:@{db}    query  ${sql}

   退出数据库:Disconnect From Database

  

 

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2.  自定义定义数据库查询结果(自定义IP):

    数据库连接:Connect To Database Using Custom Params 用法:Connect To Database Using Custom Params   pymysql host='${ip}',user='${username}',passwd='${password}',db='${db_name}',port=3306,charset='utf8'

    数据库查询:query                                                                        用法:${db}    query  select 字段 from 表 where 条件

    退出数据库:Disconnect From Database

  例子:

  

 

 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

3.获得长度为N的相同值的列表

  [Arguments] ${num_list} ${list_value} ${mode}=1

  [Documentation] 例如:N为3,list_value为None,mode为1则生成一个列表为['None','None','None'],如果mode为2则生成[None,None,None]

  ${list} Create list
  ${num_list} evaluate range(0,${num_list})
  : FOR ${i} IN @{num_list}
  \ ${list2} run keyword if ${list_value}=='' evaluate ${list_value}.decode('utf-8')
  \ ... ELSE set variable ${list_value}
  \ ${string} run keyword if ${mode}==1 evaluate str(${list2})
  \ ... ELSE set variable ${list2}
  \ append to list ${list} ${string}
  [Return] ${list}

    

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

4.生成随机手机号码

  [Arguments] ${length}=8 @{num}  

  [Documentation] ${length}=8生成的手机号码位数,不包含开头的3位,默认8位,@{num}:自定义手机号开头三位

  ${result} evaluate random.choice(['130','133','187','159','180','177','150','156',@{num}])+"".join(random.choice("0123456789")for i in range(${length})) random
  [Return] ${result}

  

 

----------------------------------------输出

 

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

5.生成随机字符串   

  [Arguments] ${num}  

  [Documentation] ${num}生成的位数,${result}返回的字符串

  ${result} evaluate "".join(random.sample(string.letters+string.digits,int(${num}))) random,string
  [Return] ${result}

  

 

------------------------------------------输出

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

6.获取N位长度随机整数

  [Arguments] ${n} ${type}=1
  [Documentation] ${type}==1:默认等于1,返回字符串类型的随机数,等于2时返回整型的随机数,${n}为生成的字符串长度

--------------------------------------------------
  ${num} Set Variable ${${n}}
  ${min} Evaluate 10**(${num}-1) math
  log ${min}
  ${max} Evaluate 10**($num)-${1} math
  log ${max}
  ${result} Evaluate random.randint(${min},${max}) random
  log ${result}
  ${result_string} Convert To String ${result}
  ${rand} Set Variable If ${type}==1 ${result_string} ${result}
  [Return] ${rand}

 

posted @ 2018-07-17 17:01  旅行没有终点  阅读(502)  评论(0编辑  收藏  举报