【问题标题】:Keyword 'Zerocon' expected 0 arguments, got 6关键字“Zerocon”预期 0 个参数,得到 6 个
【发布时间】:2017-08-08 00:12:01
【问题描述】:

这是一个机器人脚本。

这是设置。

    *** 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.

【问题讨论】:

  • 创建尽可能小的代码示例以允许其他人重新创建问题通常是好的。上述问题包含太多代码,无法运行以重新创建问题。此外,代码没有解释它应该做什么。请缩短示例并说明什么不起作用以及您已经尝试过什么。

标签: robotframework keyword


【解决方案1】:

第一个问题是ELSE If必须是ELSE IF。来自文档(强调添加):

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

正如错误所说,您遇到的另一个问题是Zerocon 不接受任何参数,但您正在向它传递参数。您传递的似乎是关键字名称,但我不知道您期望发生什么。我不知道您是想运行这些关键字,还是将它们作为参数传递给Zerocon

【讨论】:

  • 我只想在${con}的条件下运行关键字
【解决方案2】:

这是测试用例。

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

【讨论】:

  • 这似乎不是问题的答案。
猜你喜欢
  • 1970-01-01
  • 2020-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
  • 2019-11-23
  • 2021-06-12
相关资源
最近更新 更多