Robot Framework05-RF之BuiltIn库常用关键字2

关键字是RF的核心,它提供了用例执行的能力,RF用例之所以可以执行起来全都靠关键字。本篇继续对BuiltIn库常用关键字进行展开讲解,主要包括“time,sleep,random,create list,create dictionary,Catenate,get length等”。想要了解更多关键字的用法可以参考官网文档具体用法,在这里就不一一讲述。希望感兴趣的小伙伴可以坚持看下去同时欢迎提出宝贵的意见让我们一起进步!

01:get time

1)关键字含义:以请求的格式返回给定时间

2)关键字参数:format=timestamp, time_=NOW

*** Settings ***
Library  SeleniumLibrary
*** Test Cases ***
case 01:
    ${time}    get time
    log to console    ${\n}${time}
02:sleep

1)关键字含义:用来设置休眠一定时间

2)关键字参数:time_, reason=None

*** Settings ***
Library  SeleniumLibrary
*** Test Cases ***
case 02:
    ${t1}    get time
    sleep  5
    log to console  ${t1}
    ${t2}    get time
    log to console  ${t2}
03:evaluate

1)关键字含义:在python中计算给定表达式并返回结果

2)关键字参数:expression, modules=None, namespace=None

3)注意:在RF中大量的使用evaluate会造成用例的可读性下降。对于不懂代码的人来说阅读起来有点困难

*** Settings ***
Library  SeleniumLibrary
*** Test Cases ***
case 03:
    #定义一个整数
    ${var}  evaluate  890
    #定义一个列表
    ${list}  evaluate  [1,2,3]*2
    #定义一个字典
    ${dict}  evaluate  {'a':1,'b':2}
04:evaluate(生成随机数)
*** Settings ***
Library  SeleniumLibrary
*** Test Cases ***
case 04:
    #随机生一个整数:random.randint(a,b)
    ${num1}   evaluate  random.randint(1000,9999)  random
    log to console  ${\n}${num1}
case 05:
    #生成一个随机浮点数,范围是在0.0~1.0之间:random()
    ${num2}   evaluate  random.random()  random
    log to console  ${\n}${num2}
case 06:
    #从序列中返回随机的元素:choice(seq)
    ${num3}  evaluate  random.choice(['130','133','187','159','180','177','150','156'])  random
    log to console  ${\n}${num3}
case 07:
    #设定浮点数的范围:random.uniform(a,b)
    ${num4}  evaluate  random.uniform(1.5,5.6)   random
    log to console  ${\n}${num4}
case 08:
    #从序列seq中选择n个随机且独立的元素:random.sample()---sample(seq, n)
    ${num5}   evaluate   random.sample("0123456789",5)      random,string
    ${num6}    evaluate   "".join(random.sample("0123456789",5))      random,string
    log to console  ${\n}${num5}
    log to console  ${\n}${num6}
05:create list

1)关键字含义:创建一个列表

2)关键字参数:items

*** Settings ***
Library  SeleniumLibrary
*** Test Cases ***
case 09:
    @{hi}    create list    hello    world
    log to console  @{\n}${hi}
06:create dictionary

1)关键字含义:创建一个字典

2)关键字参数:items

*** Settings ***
Library  SeleniumLibrary
*** Test Cases ***
case 10:
    &{dict}  create dictionary  name=lily   score=95
    log to console  &{\n}${dict}
07:Catenate

1)关键字含义:链接多个对象

2)关键字参数:*items

*** Settings ***
Library  SeleniumLibrary
*** Test Cases ***
case 11:
    ${hi}    Catenate    hello    world
    log to console  ${\n}${hi}
08:get length

1)关键字含义:以整数形式返回并记录给定项的长度

2)关键字参数:items

*** Settings ***
Library  SeleniumLibrary
*** Test Cases ***
case 12:
    ${length}   get length  Hello,world!
    log to console  ${\n}${length}