美文网首页
全面学习robotframework框架三:内置库的学习

全面学习robotframework框架三:内置库的学习

作者: 远航天下 | 来源:发表于2018-11-28 22:55 被阅读0次
    Robotframework支持库说明:

    根据Robotframework官网可以看到:

    标准库支持
    标准库
    扩展库(第三方库)支持
    扩展库
    其他库支持
    其他库
    其实RobotFramework之所以强大,是因为他支持自定义库的开发
    综上所述可以看出来,为满足我们日常的测试需求,可以使用如下库:

    web 自动化测试:SeleniumLibrary,Selenium2Library,Selenium2Library for Java、watir-robot 等。
    Windows GUI 测试:AutoItLibrary。
    移动测试:Android library、iOS library、AppiumLibrary 等。
    数据库测试:Database Library (Java)、Database Library (Python)、MongoDB library 等。
    文件对比测试:Diff Library。
    HTTP 测试:HTTP library (livetest)、HTTP library (Requests)等

    一个例子说明常用关键字的使用
    *** Settings***
    Documentation                   一个例子说明RF常用关键字的使用
    Force Tags                      测试套件标签
    Default Tags                    RF常用关键字
    Suite Setup                     Suite Start
    Suite Teardown                  Suite End
    Library                         Screenshot
    
    *** Variables ***
    ${START_EORD}                   测试开始
    ${END_WORD}                     测试结束
    
    *** Test Cases ***
    test_01
        [Documentation]             Robotframework中打印使用 “log”
        [Tags]                      test 1.0
        [Setup]                     Test Start
        log                         holle Robotframework
        [Teardown]                  Test Down
    
    test_02
        [Documentation]             测试定义变量
        [Tags]                      test 1.0
        Set Variable         
    
    test_03
        [Documentation]             测试连接多个信息
        [Tags]                      test 1.0
        Catenate variables
    
    test_04
        [Documentation]             测试创建列表变量
        [Tags]                      test 1.0
        Create List Data
    
    test_05
        [Documentation]             测试RF时间操作
        [Tags]                      test 1.0
        [Setup]                     Test Start
        Time Operation     
        [Teardown]                  Test Down
      
    test_06
        [Documentation]             测试if关键字
        [Tags]                      test 1.0
        [Setup]                     Test Start
        IF Keyword
        [Teardown]                  Test Down
    
    test_07    
        [Documentation]             测试FOR关键字
        [Tags]                      test 1.0
        [Setup]                     Test Start
        FOR Keyword
        [Teardown]                  Test Down
    
    test_08
        [Documentation]             Evaluate关键字测试
        [Tags]                      test 1.0
        Evaluate Keyword
    
    
    
    *** Keywords ***
    Suite Start
        [Documentation]             开始运行测试套件
        log                         ${START_EORD}
    
    Suite End
        [Documentation]             测试套件运行结束
        log                         ${END_WORD}
    
    Test Start
        [Documentation]             测试开始前执行的动作
        log                         测试开始前执行的动作
    
    Test Down
        [Documentation]             测试用例执行完毕后执行的动作
        log                         测试用例执行完毕后执行的动作
    
    Set Variable
        [Documentation]             FR定义变量
        ${a}    BuiltIn.Set Variable    1234
        log    ${a}
    
    Catenate variables
        [Documentation]             连接多个信息
        ${content1}    Catenate     Holle    World!!!
        log    ${content1}
        ${content2}    Catenate    SEPARATOR=^^^    你好    中国
        log    ${content2}
    
    Create List Data
        [Documentation]             创建列表变量
        ${list_data1}    Create List    1    2    3    4
        log    ${list_data1}
        @{list_data2}    Create List    a    b    c    d
        BuiltIn.log Many    @{list_data2}
    
    Time Operation
        [Documentation]             RF时间操作
        ${time}    Get Time                     # 获取当前时间,年-月-日 时:分:秒
        log    ${time}
        ${secs}    Get Time    epoch            # 获取当前时间的描述
        log    ${secs}
        ${year}    Get Time    return year      # 获取当前的年份
        log    ${year}
        ${yyyy}    ${mm}    ${dd}=    Get Time    year,month,day    # 获得年月日的分别变量值
        log    ${yyyy}
        log    ${mm}    
        log    ${dd}
        @{time}    Get Time    year month day hour min secs       # 获得年月日时分秒的列表参数
        BuiltIn.Log Many    @{time}
        ${s}    Get Time    seconds     # 获取秒数
        log    ${s}
        ${y}    Get Time    year    # 获取年数
        log     ${y}
        log     等到时间3秒
        Sleep    3
    
    IF Keyword
        [Documentation]             if关键字  
        ${age}    BuiltIn.Set Variable    10   
        run Keyword if    ${age}>=90    log    老年人
        ...               ELSE IF    ${age}>=50    log    中年人 
        ...               ELSE IF    ${age}>=20    log    青年人
        ...               ELSE    log    小孩子
    
    FOR Keyword
        [Documentation]             for关键字
        :FOR    ${i}    INRANGE    10        # 打印0-9的数
        \     log    ${i}
        :FOR    ${ii}    in range    1    11   # 打印1-10的数
        \        log    ${ii}
        :FOR    ${iii}    in range   1    11    2    # 答应1,3,5,7,9
        \        log    ${iii}
        @{my_list}    Create List    q    w    e    r    t    # 遍历列表
        :FOR    ${iiii}    in    @{my_list}
        \        log    ${iiii}
        @{abc}    Create List    1    2    3    4    # 循环中判断
        :FOR    ${f}    in    @{abc}
        \        Exit for loop if    '${f}'=='2'
        log    ${f}
    
    Evaluate Keyword
        [Documentation]             强大的Evaluate关键字
        ${d}    Evaluate    int(2)
        log    ${d}
        ${len}    Evaluate    type(${d})
        log    ${len}    
        Take Screenshot    # 截屏
        ${dd}    Evaluate    random.randint(1, 10000)    random     # 格式为: 变量名  Evaluate(关键字)  函数   需要import的包(类型python导入包)
        log    ${dd}
        Evaluate    os.system('python3 C:/MYTEST/VScode_Spider/Test/test_格式化日期输出.py')    os
    
    
    需要注意的是Resource 和 Template的使用

    pratice_resource_and_template.robot文件

    *** Settings ***
    Documentation                   练习resouse及template关键字
    ...                             template关键字,对每一个测试用例,或者一个测试文件中的所有用例
    ...                             重复执行同一个关键字多次(使用不同数据),也可以只针对测试用例,或者每个测试文件只执行一次
    ...                             模板关键字可以接受普通位置的参数、命名参数
    ...                             关键字名中可以使用参数,不可以使用变量定义模板关键字
    ...                             用例中的[Temlate]会覆盖Settingz中的Temlate设置
    Force Tags                      robot-1.1         
    Default Tags                    owner-damao
    Resource                        ../VScode_Spider/RobotFramework/Keywords_file.robot    # 此处因为关键字文件和用例文件在同一个目录,所以可以碎略前面的目录部分
    Variables                       Variables_file.py     # 同上,在在同一个目录下,可以省略文件前面的路径
    
    *** Variables ***
    ${data1}                        1234
    ${data2}                        qwer
    ${data3}                        @#$%
    
    *** Test Cases ***
    test_01
        [Documentation]             测试用例一
        [Tags]                      test 1.1
        Test Keyword    ${data1}
    
    test_02
        [Documentation]             测试用例二
        [Tags]                      test 1.1
        [Template]    Test Keyword      # 避免多次编写关键字,只需要编写对应的参数
        ${data2}
        # ${data4}                        # ${data4} 为不存在参数变量,其会测试失败,但不影响其他参数的测试结果
        ${data3}
    
    test_03
        [Documentation]             加载Resource文件中的关键字资源
        [Tags]                      test 1.1
        [Template]    Print Something         # 该关键字为Resource加载过来的关键字资源,此处可直接使用
        我爱你中国!!!
        ${VARIABLE}
    
    *** Keywords ***
    Test Keyword
        [Documentation]             测试关键字
        [Arguments]    ${var}
        log    ${var}
    
    

    Keywords_file.robot文件

    
    *** Keywords ***
    Print Something
        [Documentation]
        [Arguments]    ${qqq}
        log    ${qqq}
    
    

    Variables_file.py文件

    # 创建是Scalar类型的variable
    
    VARIABLE = "An example string" 
    INTEGER =  hash(42)
    STRINGS = ["one", "two", "kolme", "four"]
    
    
    
    想要学习的同学需要自己理解其中的奥秘,在此就不做过多的阐述了

    相关文章

      网友评论

          本文标题:全面学习robotframework框架三:内置库的学习

          本文链接:https://www.haomeiwen.com/subject/jswaqqtx.html