1
votes

I am a complete beginner when it comes to robot framework, my apologies if this is a silly question.

So I have a user defined keyword here:

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}

Now I am trying to do a validation to check the response.status_code to see if it is returning 200 or 404.

If it is 200, I want it to run the following steps:

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

If it is 404, I want it to run the following:

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

So I have tried the following where I defined the steps as keywords under the GET call to an API keyword and call them using IF/ELSE IF but to no avail:

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}

Unfortunately it returns the following error when I run the command:

Keyword 'API_Keywords.GET SEC Success Validation' expected 0 arguments, got 2.

Any tips or guidances on how I can achieve this please?

3

3 Answers

2
votes

You can create a simple if, else if, else structure using the Run Keyword If. The documentation of the keyword even has an example for such use-case.

${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}

Starting from Robot Framework 4.0 the new IF/ELSE IF/ELSE syntax can be used.

IF    ${rc} == 0
    ${var1}=    Some keyword returning a value
ELSE IF    0 < ${rc} < 42
    ${var1}=    Another keyword
ELSE IF    ${rc} < 0
    ${var1}=    Another keyword with args   ${rc}   arg2
ELSE
    ${var1}=    Final keyword to handle abnormal cases  ${rc}
END
1
votes

You made a mistake

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

This schould do what you want. (in one line with double space between "IF" and "${response.status_code}"-sry double spaces pass it into a new line)

Sometimes there are Problems with the ELSE/IF Funktion so alternative you can use:

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

This should guarantee to run your code proper For more Information check the BuiltIn Librarie

If you need to pass variables from one Keyword to another there are two ways:

  1. Use global Variables (not recommended )

    Suite Setup Set Suite/Global/Local Variable name value

  2. Pass the needed Variables as Arguments

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}

I hope u understand this. Write a Command if u have more questions or i missunderstood something

0
votes

Without checking the documentation (I suspect that there is no ELSE IF), here is my proposal:

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

or

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

or

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