• Robot FrameWork使用


     

    1. 变量

      1. 变量申明语法: ${变量名} set variable

        例:

        ${str1}  set variable  23       #申明变量,变量是string类型
      2. 变量转换:

        ${num1} convert to number ${str1} #将string类型转换为数字

        ${num3}     convert to integer      ${str3}     #将变量转换为整数

        是否能转换为数字或其他类型,需看变量的值是否符合

         

    2. 常用关键字

       

      • 打印log到控制台:


      log to console 这是用例2
      • log 打印到log中

      • log many 打印到log中1 log中2 log中3

      • 断言

        • should be true python表达式

          should be true      5>2     #结果为false,不往下执行;结果为true,继续往下执行
          should be true     $res>2 # $res为变量
        • should contain 参数1 参数2 参数3 参数4 参数5

          判断 参数1是否包含参数2,包含则为true,否则为false

          should contain      eat       a   f  #判断参数1包含参数2;结果为false,不往下执行;结果为true,继续往下执行
      • evaluate 运行python表达式

        • 用python赋值给变量

          ${res}  evaluate    [i for i in range (3)]  #结果为[0,1,2]
        • 运行python表达式

          evaluate   driver.get('https://baidu.com/')

         

      •  

       

    3. 条件判断

      1. if判断语法:run keyword if python表达式 log to console 条件成立

        run keyword if      type($resp1)=='string'     log to console      条件成立
      2. if-else:

        run keyword if      '2021' in $resp1      log to console      今年是2021年
        ...   ELSE IF     '-10-' in $resp1       log to console     现在是10月 #ELSE IF中间空一格
        ...   ELSE       log to console     不知道是什么时间       #一条写不完,换行写,前面加...

         

      3.  

    4. for循环

      1. for-break

        FOR      ${i}    IN  RANGE     9999
          #获取用户输入值
          ${score}   get value from user     请输入你的分数     #引用Library Dialogs
          run keyword if     'exit'==$score     exit for loop
          check score ${score}
        END
      2.  

    5. Variables

      1. 参数定义为列表

        @{list}  auto    sdfsdfsdf      #这样定义的是列表
        取上面变量的方法: ${list}[0] ${list}[1]
      2. 参数定义为字典

        &{dict}    name=auto    pwd=sdfsdfsdf   #这样定义是字典
        取用上面值的方法: ${dict}[name] ${dict}[pwd]

        实例

        *** Settings ***
        Library SeleniumLibrary
        *** Variables ***
        ${login_url}   http://localhost/mgr/login/login.html
        @{list} auto   sdfsdfsdf     #这样定义的是列表
        &{dict}   name=xiaohong   pwd=123456   #这样定义是字典
        *** Test Cases ***
        test1
          login1
        test2
          login2
        *** Keywords ***
        login1
          open browser ${login_url}     chrome
          set selenium implicit wait 10
          clear element text id=username
          input text id=username     ${list}[0]
          clear element text id=password
          input text id=password     ${list}[1]
          click element   css=.btn-success
          sleep 3
          close browser
        login2
          open browser ${login_url}     chrome
          set selenium implicit wait 10
          clear element text id=username
          input text id=username     ${dict}[name]
          clear element text id=password
          input text id=password     ${dict}[pwd]
          click element   css=.btn-success
          sleep 3
          close browser
      3. 使用python定义的变量文件

        *** Settings ***
        Variables conf.py #python的变量文件
        *** Test Cases ***
        test1
          log to console ${url}
          log to console ${name}[2]
          log to console ${ddic}[name]
          log to console ${ddic}[age]
        #conf.py文件内容
        url='http://www.baidu.com'
        name = ['a','eat','meet','apple']
        ddic = {'name':'Mary','age':4}
      4.  

    6. 命令行运行

      1. 指定子套件用--suite -s 指定测试用例--test -t

      2. 指定变量路径 --variablefile -V

      3. 指定pythonPath -P

    7. 初始化和清除

      在RF中,所有的setup与teardown操作都只能由一个关键字语句构成

      • setup/teardown

        • setup测试一个用例(或套件)前要做的是事情

        • 语法:

          [Setup]  log to console    执行用例初始化动作~~~~~~~~~
          [Teardown] log to console   执行用例清除动作===========
          log to console   执行用例主体啦
          • 任何情况下setup和teardown都会执行,即使用例主体执行失败

            [Setup]  log to console    执行用例初始化动作~~~~~~~~~
            [Teardown] log to console   执行用例清除动作===========
            log to console   执行用例主体啦
            should be true 1>7
          • 如果setup失败,用例主体不会执行

          • setup和teardown不用成对出现,可以单独用

    • 测试套件 与 测试用例中的初始化与清除

      • suite 中的

        • 将相同的数据环境抽离出来放在套件级别,可以提高用例执行效率,而且方便维护。

        • 进入和退出这个suite执行用例前后必须且只分别执行一次

        • 一般套件定义在Settings中

        • 测试套件目录的,是在目录下新建 __init.txt__ 或者__ init.robot__

      • testcase

        • 如果suite内的用例本身没有 setup/teardown 才执行

        • 遵循就近原则

          *** Settings ***
          Suite Setup       log to console   执行套件初始化@@@@@@
          Suite Teardown     log to console   执行套件清除……
          Test Setup     log to console   执行测试用例默认初始化~~~~~~~
          Test Teardown   log to console   执行测试用例默认清除&&&&&&&
          *** Test Cases ***
          test1
            log to console test1执行用例主体
          test2
            [Setup] log to console   初始化
            [Teardown] log to console   执行用例清除动作
            log to console   执行用例主体
          test3
            [Setup] log to console   用例初始化
            log to console     用例主体

       

  • 相关阅读:
    Django之模板
    Web框架的原理
    mysql 索引
    pymysql模块的使用
    MySQl创建用户和授权
    php去除h5标签
    phpcms_完整版
    快速上手Linux 玩转典型应用_慕课网笔记
    php 微擎
    二维码接口
  • 原文地址:https://www.cnblogs.com/xiaoyujade/p/13865225.html
Copyright © 2020-2023  润新知