RF常用方法

关于比较

${rtn}    run keyword if    '${id}'=='02381'

字符串比较时需要加上引号,其中:

  • ${None}和${null}均表示空
  • ${Empty}和"",’’’’ 表示空字符串
  • ${Space}表示空格
新增用户
    [Documentation]  Auth:Jim
    [Tags]  test  smoke
    ${result}  获取用户列表  YH1102216
	${user_list_id}  set variable  ${None}
    RUN KEYWORD IF  ${result["data"]["list"]}!=[]  ${user_list_id}  Get Random Data From List  list_data=${result["data"]["list"]}  index=0  keyword=id
    RUN KEYWORD IF  ${result["data"]["list"]}!=[]  根据id删除用户  id=${user_list_id}
*** Test Cases ***
Test01
    Should be True  '1'!='2'  发生相等
    
    ${l}  create list
    RUN KEYWORD IF  ${l}==[]  log to console  相等
    should be true  ${null}==${None}
    should be true  '${EMPTY}'==""
    should be true  '${EMPTY}'==''
    should be true  '${Space}'==' '

关于空符串赋值

${already}  set variable if  '${already}'=='${EMPTY}'  0

生成随机数字和字符串

    ${phoneNumber}    Evaluate    random.randint(13775570000,13775579999)    random
	${str}  evaluate  "".join(random.sample(string.digits+string.ascii_lowercase,11))  random,string
    ${str}  evaluate  "".join(random.sample(string.digits+string.ascii_letters,11))  random,string

Setup and Teardown

*** Settings ***
#Test Setup       Open Application    App A
Test Setup       run keywords  KeywordA  AND  KeywordB #AND必须大写
Test Teardown    Close Application

*** Test Cases ***
Default values
    [Documentation]    Setup and teardown from setting table
    Do Something

Overridden setup
    [Documentation]    Own setup, teardown from setting table
    [Setup]    Open Application    App B
    Do Something

No teardown
    [Documentation]    Default setup, no teardown at all
    Do Something
    [Teardown]

No teardown 2
    [Documentation]    Setup and teardown can be disabled also with special value NONE
    Do Something
    [Teardown]    NONE

Using variables
    [Documentation]    Setup and teardown specified using variables
    [Setup]    ${SETUP}
    Do Something
    [Teardown]    ${TEARDOWN}

evaluate关键字

  1. random()生成0<=n<1之间的随机实数–它会生成一个随机的浮点数,范围是在0.0~1.0之间。:   ${num} evaluate random.random() random
  2. choice(seq)从序列中返回随机的元素:
      ${a1} evaluate random.choice([‘130’,‘133’,‘187’,‘159’,‘180’,‘177’,‘150’,‘156’,‘aaa’]) random
  3. random.uniform(a,b)正好弥补了1中函数的不足,它可以设定浮点数的范围,一个是上限,一个是下限
      ${a2} evaluate random.uniform(1.5,5.6) random
  4. random.randint(a,b):随机生一个整数int类型,可以指定这个整数的范围,同样有上限和下限值
      ${a3} evaluate random.randint(1000,9999) random
  5. random.sample()—sample(seq, n) 从序列seq中选择n个随机且独立的元素;
       a 6 e v a l u a t e r a n d o m . s a m p l e ( " 0123456789 " , 5 ) r a n d o m , s t r i n g     {a6} evaluate random.sample("0123456789",5) random,string    a6evaluaterandom.sample("0123456789",5)random,string  {a7} evaluate “”.join(random.sample(“0123456789”,5)) random,string
  6. . ${planName} evaluate “”.join(random.sample(“一二三四五六七八九十”,1))
    random,string
  7. ${var} evaluate testEvaluate # 可以是字符串
  8. ${var1} evaluate [‘hello’,‘world’,‘2019’] # 可以是列表
  9. ${var2} evauate [‘hello’,0] #可以是复杂的表达式

RF3.2版本还支持:

Evaluating Python expressions involving Robot Framework's variables 
  (${{len('${var}') > 3}}, ${{$var[0] if $var is not None else None}}).
Creating values that are not Python base types 
  (${{decimal.Decimal('0.11')}}, ${{datatime.date(2019, 11, 5)}}).
Creating values dynamically 
  (${{random.randint(0, 100)}}, ${{datetime.date.today()}}).
Constructing collections, especially nested collections 
  (${{[1, 2, 3, 4]}}, ${{ {'id': 1, 'name': 'Example', children: [7, 9]} }}).
Accessing constants and other useful attributes in Python modules 
  (${{math.pi}}, ${{platform.system()}}).

FOR

*** Test Cases ***
Template and for
    [Template]    Example keyword
    FOR    ${item}    IN    @{ITEMS}
        ${item}    2nd arg
    END
    FOR    ${index}    IN RANGE    42
        1st arg    ${index}
    END
Repeat Keyword    ${var}    Another Keyword    argument

Variable

*** Variables ***
${STRING}     Hello, world!
${INTEGER}    ${42}
@{LIST}       one         two
&{DICT}       one=yksi    two=kaksi
# 变量取出时使用@和&可以单项传入参数

List

*** Variables ***
@{LIST}         a    b    c

*** Test Cases ***
Manage index manually
    ${index} =    Set Variable    -1
    FOR    ${item}    IN    @{LIST}
        ${index} =    Evaluate    ${index} + 1
        My Keyword    ${index}    ${item}
    END

For-in-enumerate
    FOR    ${index}    ${item}    IN ENUMERATE    @{LIST}
        My Keyword    ${index}    ${item}
    END

Dict

*** Variables ***
&{DICT}          a=1    b=2    c=3

*** Test Cases ***
Dictionary iteration
    FOR    ${key}    ${value}    IN    &{DICT}
        Log    Key is '${key}' and value is '${value}'.
        Continue For Loop If    '${var}' == 'two'
        Run Keyword If    '${var}' == 'two'    Exit For Loop
    END

Dictionary iteration with enumerate
    FOR    ${index}    ${key}    ${value}    IN ENUMERATE    &{DICT}
        Log    On round ${index} key is '${key}' and value is '${value}'.
    END

Multiple dictionaries and extra items in 'key=value' syntax
    &{more} =    Create Dictionary    e=5    f=6
    FOR    ${key}    ${value}    IN    &{DICT}    d=4    &{more}    g=7
        Log    Key is '${key}' and value is '${value}'.
    END

Refer

Refer:document

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值