关键字' Zerocon'预计0个参数,得到6

时间:2017-03-16 17:07:49

标签: robotframework keyword

这是一个机器人脚本。

这是设置。

    *** Settings ***
    Suite Setup    GetUrl
    Test Setup     Create HTTP Context    ${conturl[0]["Host"]}
    Library    JsonLibb.py
    Library    RequestsLibrary
    Library    HttpLibrary.HTTP

它是测试用例。

    *** Test Cases ***
    Msg
        Log    ${con}
        Run Keyword If    ${con}==1    Zerocon    Onecon
            ELSE If    ${con}==2    Zerocon    Onecon    Twocon
            ELSE    Log    'Error'

它是关键字。

    *** Keywords ***
    GetUrl  
        ${conturl}    Get URL Json
        Set Global Variable    ${conturl}
        ${conturl_lenth}=    Get Length    ${conturl}
        Set Global Variable    ${conturl_lenth}
        :FOR    ${url_lenth}    IN RANGE    ${conturl_lenth} 
        \    Set Global Variable    ${url_lenth}  
        \    Log    ${url_lenth}

        ${content}    Get HEADER Json
        Set Global Variable    ${content}
        ${content_lenth}=    Get Length    ${content}
        Set Global Variable    ${content_lenth}
        :FOR    ${con}    IN RANGE    ${content_lenth}
        \    Set Global Variable    ${con}

        ${contbody}    Get BODY Json
        Set Global Variable    ${contbody}
        ${contbody_lenth}=    Get Length    ${contbody}
        Set Global Variable    ${contbody_lenth}
        :FOR    ${body_lenth}    IN RANGE    ${contbody_lenth}
        \    Set Global Variable      ${body_lenth}

    Zerocon
        Set Request Header    Cookie    ${content[${0}]["COOKIE"]}           
        Set Request Header    X-Requested-With    ${content[${0}]["X-Requested-With"]}
        Run Keyword If    '${content[0]["method"]}'=='POST'    POSTRequest
               ELSE If    '${content[0]["method"]}'=='GET'    GETRequest
               ELSE    Log    'ErrorZero'

        Onecon
        Set Request Header    Cookie    ${content[${1}]["COOKIE"]}           
        Set Request Header    X-Requested-With    ${content[${1}]["X-Requested-With"]}
        Run Keyword If    '${content[1]["method"]}'=='POST'    POSTRequest
               ELSE If    '${content[1]["method"]}'=='GET'    GETRequest
               ELSE    Log    'ErrorOne'

    Twocon
        Set Request Header    Cookie    ${content[${2}]["COOKIE"]}           
        Set Request Header    X-Requested-With    ${content[${2}]["X-Requested-With"]}
        Run Keyword If    '${content[2]["method"]}'=='POST'    POSTRequest
               ELSE If    '${content[2]["method"]}'=='GET'    GETRequest
               ELSE    Log    'ErrorTwo'

    POSTRequest
        Run Keyword If    '${url_lenth}'=='1'    HttpLibrary.HTTP.POST    ${conturl[0]["Uri"]}
               Set Request Body      ${contbody[0]}
               Response Status Code Should Equal    200
               Response Body Should Contain    "code":0,"msg":"","data":[]
               Log Response Body

               HttpLibrary.HTTP.POST    ${conturl[1]["Uri"]}
               Set Request Body      ${contbody[1]}
               Response Status Code Should Equal    200
               Response Body Should Contain    "code":0,"msg":"","data":[]
               Log Response Body

               ElSE If    '${url_lenth}'=='2'    HttpLibrary.HTTP.POST    ${conturl[0]["Uri"]}
               Set Request Body      ${contbody[0]}
               Response Status Code Should Equal    200
               Response Body Should Contain    "code":0,"msg":"","data":[]
               Log Response Body

               HttpLibrary.HTTP.POST    ${conturl[1]["Uri"]}
               Set Request Body      ${contbody[1]}
               Response Status Code Should Equal    200
               Response Body Should Contain    "code":0,"msg":"","data":[]
               Log Response Body

               HttpLibrary.HTTP.POST    ${conturl[2]["Uri"]}
               Set Request Body      ${contbody[2]}
               Response Status Code Should Equal    200
               Response Body Should Contain    "code":0,"msg":"","data":[]
               Log Response Body

               ELSE    Log    "Error"   

    GETRequest
        Run Keyword If    '${url_lenth}'=='1'    HttpLibrary.HTTP.GET    ${conturl[0]["Uri"]}
               Response Status Code Should Equal    200
               Response Body Should Contain    "code":0,"msg":"","data":[]
               Log Response Body

               HttpLibrary.HTTP.GET    ${conturl[1]["Uri"]}
               Response Status Code Should Equal    200
               Response Body Should Contain    "code":0,"msg":"","data":[]
               Log Response Body

               ElSE If    '${url_lenth}'=='2'    HttpLibrary.HTTP.GET    ${conturl[0]["Uri"]}
               Response Status Code Should Equal    200
               Response Body Should Contain    "code":0,"msg":"","data":[]
               Log Response Body

               HttpLibrary.HTTP.GET    ${conturl[1]["Uri"]}
               Response Status Code Should Equal    200
               Response Body Should Contain    "code":0,"msg":"","data":[]
               Log Response Body

               HttpLibrary.HTTP.GET    ${conturl[2]["Uri"]}
               Response Status Code Should Equal    200
               Response Body Should Contain    "code":0,"msg":"","data":[]
               Log Response Body

               ELSE    Log    "Error" 

这就是错误。

       ===========Output============

       Documentation:   
       Runs the given keyword with the given arguments, if condition is true.
       Start / End / Elapsed:   20170317 00:42:03.544 / 20170317 00:42:03.545 / 00:00:00.001
       00:00:00.001KEYWORD Zerocon Onecon, ELSE If, ${con}==2, Zerocon, Onecon, Twocon
        Start / End / Elapsed:  20170317 00:42:03.544 / 20170317 00:42:03.545 / 00:00:00.001
       00:42:03.545 FAIL    Keyword 'Zerocon' expected 0 arguments, got 6.

2 个答案:

答案 0 :(得分:1)

第一个问题是ELSE If必须是ELSE IF。从文档(重点添加):

  

从Robot版本2.7.4开始,此关键字还支持可选的ELSE和ELSE IF分支。这两个都在* args中定义,必须使用格式正确的ELSE或ELSE IF

你所遇到的另一个问题,就是错误说的是,Zerocon不会采取任何参数,但你要传递参数。你传递的是关键字名称,但我不知道你期望发生什么。我不知道您是否想要运行这些关键字,或将这些关键字作为参数传递给Zerocon

答案 1 :(得分:-1)

它是测试用例。

    *** Test Cases ***
    Msg
        Log    ${con}
        Run Keyword If    ${con}==1    Zerocon    Onecon
            ELSE If    ${con}==2    Zerocon    Onecon    Twocon
            ELSE    Log    'Error'