机械手框架:IF / ELSE-在另一个关键字中使用关键字

时间:2020-09-15 15:38:19

标签: robotframework

关于机器人框架,我是一个完整的初学者,如果这是一个愚蠢的问题,我深表歉意。

所以我在这里有一个用户定义的关键字:

GET call to an API
      [Arguments]  ${user_uuid}  ${country_uuid}  ${http_status_code}
      ${json_date_uuid} =  Set Variable  $.date
     #code here#  
    
      ${response} =  Trigger GET  ${HTTP_SERVER}  ${APP_UUID}  ${PRI_KEY}  ${get_endpoint}  ${secs_headers}  ${no_query_param}
      Log  Endpoint GET Result:
      Log  ${response.status_code}
      Log  ${response.content}

现在,我正在尝试进行验证,以检查response.status_code是否返回200或404。

如果是200,我希望它运行以下步骤:

Should Be Equal As Strings  ${response.status_code}  ${status_code}
${milestone_definition} =  Retrieve Json Value  ${response.content}  ${json_date_uuid}
List Should Contain Value  ${value}  12345

如果是404,我希望它运行以下命令:

Should Be Equal As Strings  ${response.status_code}  ${status_code}

因此,我尝试了以下操作,其中将步骤定义为 GET API调用关键字下的关键字,并使用IF / ELSE IF对其进行了调用,但无济于事:

GET call to an API
   [Arguments]  ${user_uuid}  ${country_uuid}  ${http_status_code}
   ${json_date_uuid} =  Set Variable  $.date
   #code here#
                
   ${response} =  Trigger GET  ${HTTP_SERVER}  ${APP_UUID}  ${PRI_KEY}  ${get_endpoint}  
   ${secs_headers}  ${no_query_param}
   Log  Endpoint GET Result:
   Log  ${response.status_code}
   Log  ${response.content}
            
   Run Keyword If  ${response.status_code} == 200  GET SEC Success Validation  ELSE IF ${response.status_code} == 404  GET SEC Failure Validation
                    
#Validation
GET SEC Success Validation
   Should Be Equal As Strings  ${response.status_code}  ${status_code}
   ${milestone_definition} =  Retrieve Json Value  ${response.content}  ${json_date_uuid}
   List Should Contain Value  ${value}  12345
                
GET SEC Failure Validation GET
   Should Be Equal As Strings  ${response.status_code}  ${status_code}

不幸的是,当我运行命令时,它返回以下错误:

关键字“ API_Keywords.GET SEC成功验证”预期为0 论点,得到2。

请问有关如何实现此目标的任何提示或指导?

3 个答案:

答案 0 :(得分:2)

您可以使用Run Keyword If创建一个简单的if, else if, else结构。关键字的文档甚至提供了这种用例的示例。

${var1} =     Run Keyword If  ${rc} == 0  Some keyword returning a value      
...   ELSE IF     0 < ${rc} < 42  Another keyword         
...   ELSE IF     ${rc} < 0   Another keyword with args   ${rc}   arg2
...   ELSE    Final keyword to handle abnormal cases  ${rc}

答案 1 :(得分:1)

您犯了一个错误

Run Keyword If  ${response.status_code} == 200  GET SEC Success Validation  ELSE IF 
${response.status_code} == 404  GET SEC Failure Validation

这应该做您想要的。 (在一行中,如果在“ IF”和“ $ {response.status_code}”之间加双倍空格,则将双倍空格传递到新行中)

有时ELSE / IF功能存在问题,因此可以使用以下替代方法:

Run Keyword If  ${response.status_code} == 200  GET SEC Success Validation
Run Keyword If  ${response.status_code} == 404  GET SEC Failure Validation

这应该保证正确运行您的代码 有关更多信息,请查看BuiltIn Librarie

如果您需要将变量从一个关键字传递到另一个关键字,则有两种方法:

  1. 使用全局变量(不推荐)

    Suite Setup Set Suite/Global/Local Variable name value

  2. 将所需的变量作为参数传递

Run Keyword If  ${response.status_code} == 200  GET SEC Success Validation  ${arg1.1}  ${arg1.2}

GET SEC Success Validation
  [Arguments]  ${arg1.1}  ${arg1.2}
Run Keyword If  ${response.status_code} == 404  GET SEC Failure Validation  ${arg2.1}  ${arg2.2}

GET SEC Failure Validation GET
  [Arguments]  ${arg2.1}  ${arg2.2}

我希望你能理解这一点。如果您还有其他问题或我误解了一些内容,请写命令

答案 2 :(得分:0)

不检查文档(我怀疑没有ELSE IF),这是我的建议:

Run Keyword If  ${response.status_code} == 200  GET SEC Success Validation  ELSE  Run Keyword If  ${response.status_code} == 404  GET SEC Failure Validation

Run Keyword If  ${response.status_code} == 200  GET SEC Success Validation
Run Keyword If  ${response.status_code} == 404  GET SEC Failure Validation

Run Keyword If  ${response.status_code} == 200  GET SEC Success Validation
Run Keyword Unless  ${response.status_code} == 200  Run Keyword If  ${response.status_code} == 404  GET SEC Failure Validation