Robot Framework基础教程

安装

pip install robotframework robotframework-selenium2library

Pycharm 安装插件

Pycharm File->Settings->Plugins
搜索并安装插件

  • IntelliBot
  • Run Robot Framework Testcase
  • RunRobot Framework

基本格式

  • Settings
    • Library
    • Resource
    • Force Tags Defalut Tags(未定义任何标签时)
    • Test Setup Test TearDown
    • Test Template
    • Test Timeout
  • Variables
  • Keywords
  • Test Cases
    • [Tags]
    • [Template]
    • [Documention]
    • [Timeout]
    • [Return]
    • [Arguments]
    • [Setup]
    • [TearDown]

Suite(文件夹)
__init__文件

*** Setttings ***
** Test Setup **     ... 
** Test Teardown **  ...

library为第三方库或自定义库,resource为自定义关键字集合,variables为自定义变量集合

*** Keywords ***
loginwebsite
    [Arguments]  ${username}  ${password}
    Open Browser   http://...    chrome
    
    [Return]   ${lessons}

简单示例

*** Settings ***
Library     SeleniumLibrary


*** Test Cases ***

test rf
    log    hello robot framework

Baidu search case
    Open Browser    http://www.baidu.com    chrome
    Input text    id=kw    robot framework
    Click button    id=su
    Close Browser

Selenium Demo

baidu_search.robot

*** Settings ***
Documentation    Simple examle using SeleniumLibrary
Library    SeleniumLibrary


*** Variables ***
${URL}      http://www.baidu.com
${BROWSER}  Chrome

*** Keywords ***
Baidu Search
    [Arguments]    ${search_key}
    Input text     id:kw    ${search_key}
    click button   id:su
    Evaluate       time.sleep(2)    time
    ${title}       Get title
    [Return]       ${title}

*** Test Case ***
case1
    Open Browser    ${URL}    ${BROWSER}
    ${title}        Baidu Search    robot framework
    Should contain  ${title}    robot framework_百度搜索
    close Browser

case2
    Open Browser    ${URL}    ${BROWSER}
    ${title}        Baidu Search    selenium
    Should contain  ${title}    selenium_百度搜索
    close Browser


Headless Demo

*** Settings ***
Documentation     This example demonstrates how to use current library
Library    SeleniumLibrary

*** Test cases ***
Open Browser with Chrome options in headless mode
    ${options}  Evaluate  sys.modules['selenium.webdriver'].ChromeOptions()  sys, selenium.webdriver
    Call Method  ${options}  add_argument  --start-maximized
    Call Method  ${options}  add_argument  --headless
    Call Method  ${options}  add_argument  --disable-gpu
    #Call Method  ${options}  add_argument  --remote-debugging-port=${9222}
    Create Webdriver    Chrome   chrome_options=${options}
    Go To    https://www.baidu.com
    ${title}=    Get Title
    Log to console    ${title}

MySQL操作Demo

*** Settings ***
Library    DatabaseLibrary

*** Test Case ***
Test
    Connect To Database Using Custom Params    pymysql    database='spicespirit', user='root', password='spice', host='192.168.100.198', port=3306,charset='utf8'
    ${result}     Query     select * from u_user where phone='18010181267'
    log           ${result}
原文地址:https://www.cnblogs.com/superhin/p/12747548.html