Robot Framework(关键字、变量、循环)

Robot Framework关键字的使用

  1)在robot framework标准库

  2)在robot framework官方文档:http://robotframework.org

常用语法

 定义变量
      ${var1}     set variable  word   #定义字面量
      ${var2}     convert to integer  1  # 定义整型
      ${var3}     convert to number  3.4  # 定义数字,可以是浮点数
      log to console  ${var1}
      log to console  ${var2}
      log to console  ${var3}
      log to console  ${4+3}  # 7  数字变量
 常用关键字
 # should be true  注意点:数为python表达式,布尔类型,空格不能多于2个,变量前面加一个$
 # should be true 它的参数本身是python 表达式语句,RF会直接用python解释器 的eval函数,来解释这个参数表达式。
     ${var1}     set variable  word
     ${var2}     convert to integer  2  # 定义整型
     ${var3}     convert to integer  3  # 定义整型
     ${var4}     create list             #定义空列表
     should be true  3==2   # 参数为python表达式,布尔类型,空格不能多于2个,变量前面加一个$
     should be true  []    # 空列表为False
     should contain  hello     he   #第一个参数包含第二个参数
     should start with  hello    h  # 第一个参数以h开头
     should end with  hello  o   # 第一个参数以o结尾
     should be empty  ${var4}    # 判断是否为空
 
 判断两个对象是否相等
     #should be equal  ${var2}    ${var3}
     should be equal  a  a
     should be equal as integers  10     010   #转换成数字对象比较,结果是true
     should be equal  10     010     #字符串对象比较,结果是False
     should be equal as numbers  1.2     1.2
 字符串比较
 # $符号加变量,直接引用变量本身
     ${var}  set variable  word
     #报错Evaluating expression 'word == 'word'' failed: NameError: name 'word' is not defined
     #should be true  ${var} == 'word'
     should be true  '${var}' == 'word'  #True
     should be true  $var == 'word'  #True
 整数比较
     ${var}  convert to integer  2
     log to console  ${2}   # 2
     should be true  ${var} == ${2}
     should be true  $var == ${2}
     should be true  $var == 2
     
 其他杂项
      log to console  打印输出      (注释:在终端输出判断是否相等这句话)
      Import Library   导入某个模块
      sleep  (注释:休眠的时间(可写天数))
      Convert To Integer(把参数中的数转换成整数)
    get length 获取列表长度

实例:

#用例定义在表格中

#用例表----写法:首字母大写,中间有空格
*** Test Cases ***
#用例标题顶格来写
case01打印输出
    #用例主题是由关键字组成
    #用例步骤要和左侧两个空格以上,关键字于空格也是两格以上,多个参数之间也是两格空格以上,两个空格是rf的分隔符

    #步骤一:
    log to console  helloworld  #log to console类似于打印输出
    #步骤二:
    log to console  rf

case02定义一个变量
    #格式:${变量名}   set variable  值
    ${var1}     set variable  helloworld
    log to console  ${var1}

case03定义一个列表
    #格式:${列表名}  create list  值1  值2  值3
    ${name_list}    create list  王子文  甘婷婷   金晨
    log to console  ${name_list}

case04定义一个字典
    #格式:${字典名}  create dictionary  name=Augus  age=30
    ${dict1}    create dictionary  name=Augus  age=30
    log to console  ${dict1}

case05两个字符串拼接
    #catenate用于拼接字符串,将结果赋值给一个变量
    #格式:${变量名}  catenate  字符串1  字符串2
    ${var1}  set variable  我的名字叫
    ${var2}  set variable  Augus
    ${var3}  catenate  ${var1}  ${var2}
    log to console  ${var3}

case06rf中判断
    #在rf分支结构中新版中ELSE必须要大写
    #RF中解决语句太长的问题,可以用下一行前面加上三个点的省略号,且下一行的省略号和关键字ELSE之间必须有连个空格
    ${num1}  set variable  5
    ${num2}  set variable  6
    run keyword if  int(${num1})>int(${num2})   log to console  ${num1}>${num2}
    ...  ELSE IF  int(${num1})<int(${num2})  log to console  ${num1}<${num2}
    ...  ELSE  log to console  ${num1}==${num2}

case07rf中循环
    #rf中只有for循环
    #Exit For Loop 关键字实现break功能,用Continue For Loop 关键字实现continue 功能,也可以使用 等价的关键字Continue For Loop If    Exit For Loop If
    #如果要遍历列表,for循环后面需要注意格式如: @{列表名}
    ${name_list1}    create list  王子文    甘婷婷   金晨
    FOR   ${name}   IN    @{name_list1}
      log to console   ${name}
          log to console    *************
    END
    log to console   for循环外

case07在rf中实现类似while循环
    #rf中想实现while的功能需要使用大Range
    #注意IN RANGE中间只能有一个空格
    FOR  ${var}  IN RANGE  5
        LOG TO CONSOLE  ${var}
    END

    #指定起始范围,不包括最后一个元素
    FOR  ${var}  IN RANGE  1  5
        LOG TO CONSOLE  ${var}
    END

    #指定步长
    FOR  ${var}  IN RANGE  1  20  2
        LOG TO CONSOLE  ${var}
    END

 给列表或者字典添加值

给列表字典添加值的时候需要注意要导入RF中标准库Collections

给列表添加值:

*** Settings ***
Library  Collections

*** Test Cases ***
case11给列表中添加值
    ${list1}  create list
    append to list  ${list1}  马蓉   李小璐    #同时可以添加多个值,多个值直接使用逗号隔开
    log to console  ${list1}

给字典中添加值

*** Settings ***
Library  Collections

*** Test Cases ***
case12给字典中添加值
    ${dict1}  create dictionary
    set to dictionary  ${dict1}   name=李四  age=23
    log to console  ${dict1}
posted @ 2018-11-16 17:33  酒剑仙*  阅读(650)  评论(0编辑  收藏  举报