RobotFramework学习02-基础关键字

RobotFramework学习笔记目录


本文包含内容:

  1. 基础关键字
  2. 基础关键字(高级用法)
  3. 内置关键字查询

关键字

关键字类似于python中的函数

  • Go To

访问某个网址(前提是已经打开过浏览器)(可在初始化中打开浏览器)

Go To    https://www.baidu.com
  • Should Be Equal
  • Should Be ...
  • Should Be True

如果是字符串,需要加引号;数字不需要

# 示例
${str1}=  set variable  hello
Should Be True  $str1 == 'hello'
Should Be True  '${str1}' == 'hello'

${str1}=  convert to interger  20
Should Be True  ${str1} == 19
Should Be True  $str1 == 19
  • convert to interger 转换为整数

  • convert to Number 转换为浮点数

  • log to console 直接打印(类似python中的print)

  • ${33} 直接传数字

  • 可以自己写一个xxx.py,自定义函数,然后在robot文件中在Settings中引用后Library xxx.py,此时可直接调用该函数(此时函数名就是关键字)

Library xxx.py 寻找路径同pyhon中选择模块的方法(path)

  • 关键字文档

文档中import部分为导入文档时可使用的参数

# 例如导入SeleniumLibrary
Library    SeleniumLibrary    implicit_wait=10  # 修改等待时间

基础关键字

log就是python中的"print"
  • log关键字就是编程语言里的的print,可以打印任何想打印的内容
*** Test Cases ***
test case1
    log    robot framework
    log    python

RobotFramework学习02-基础关键字_第1张图片
log to console将信息打印到控制台(不会存储到log文件中)
test case1
    log to console    robot framework
    log to console    python
RobotFramework学习02-基础关键字_第2张图片
定义变量
  • 在Robot Framework中通过setvariable关键字定义变量
*** Test Cases ***
test case2
    ${var}  set variable  test python
    log  ${var}
RobotFramework学习02-基础关键字_第3张图片
定义及展示列表
  • 关键字Create List可以用来定义列表
  • 关键字log many打印列表元素
test case4
    ${hi}    Create List    hello    world
    log    ${hi}
    log many    @{hi}
RobotFramework学习02-基础关键字_第4张图片
连接对象
  • Catenate关键字可以链接多个对象
*** Test Cases ***
test case3
    ${hi}    Catenate    hello    world
    log    ${hi}
RobotFramework学习02-基础关键字_第5张图片
  • Catenate关键字加上 SEPARATOR=可以对多个连接的信息进行分割。
test case4
    ${hi}    Catenate    SEPARATOR=---    hello    world
    log    ${hi}
RobotFramework学习02-基础关键字_第6张图片
时间操作
  • get time关键字用来获取当前时间
test case4
    ${hi}    get time
    log to console    ${hi}
RobotFramework学习02-基础关键字_第7张图片
  • sleep关键字用来设置休眠一定时间
test case4
    ${t1}    get time
    sleep  5
    ${t2}    get time
RobotFramework学习02-基础关键字_第8张图片

基础关键字(高级用法)

if语句
  • 通过run keyword if关键字可以编写if分支语句

注1:ELSE IFELSE 前面的三个点(…)
注2:ELSE IFELSE 必须大写

test case4
    ${hi}  set variable  90
    run keyword if   ${hi}>=90   log to console   优秀
    ...   ELSE IF     ${hi}>=80   log to console    良好
    ...   ELSE IF    ${hi}>=60   log to console    及格
    ...   ELSE   log to console   不及格
RobotFramework学习02-基础关键字_第9张图片
for循环

在Robot Framework中编写循环通过:FOR
循环内的语句必须以\开头

  • 循环0-9
test case4
    :for  ${i}  in range   10
    \  log to console   ${i}
RobotFramework学习02-基础关键字_第10张图片
  • 遍历列表
test case4
    ${abc}  create list  1  2  3   a
    :for  ${i}  in  @{abc}
    \  log to console  ${i}
RobotFramework学习02-基础关键字_第11张图片
强大的Evaluate

通过Evaluate可以使用 Python 语言中所提供的方法。

  • 生成随机数
test case4
    ${d}  evaluate  random.randint(999,9999)   random
    log to console   ${d}

RobotFramework学习02-基础关键字_第12张图片
  • 转化类型
test case4
    ${d}  evaluate  int(4)

你可能感兴趣的:(RobotFramework学习02-基础关键字)