1. 程式人生 > 實用技巧 >Robot FrameWork使用

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 不知道是什麼時間 #一條寫不完,換行寫,前面加...

  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
  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}
  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 用例主體