Search code examples
pythonrobotframework

Check status code is 200 or 202 in robot framework


I have robot framework code which should check the status code is 200 or 202 if method is post so I am trying this code

  Run keyword if  '${Method}'== 'POST'    RequestsChecker.Check Response Status   
  ${response}  202 || 200
  or
  Run keyword if  '${Method}'== 'POST'    RequestsChecker.Check Response Status   
  ${response}  202 or 200

Error: ValueError: invalid literal for int() with base 10: '202 || 200' and ValueError: invalid literal for int() with base 10: '202 or 200'

can anyone guide how can i do this status code check with or in robot?


Solution

  • That keyword accepts a single RC - which can be int or a string, but the very first thing it does is to cast it to int. So it cannot work with "202 || 200", "202 or 200", or any similar combination - it has never been designed to.

    But you can accomplish that by two calls to it, expecting one of them to succeed

    ${status 200}=    Run keyword if  '${Method}' == 'POST'    Run Keyword And Return Status    RequestsChecker.Check Response Status   ${response}  200
    ${status 202}=    Run keyword if  '${Method}' == 'POST'    Run Keyword And Return Status    RequestsChecker.Check Response Status   ${response}  202
    
    # now fail if the method is the one, and the RC was not in the expected
    Run keyword if  '${Method}' == 'POST' and not (${status 200} or ${status 202})    Fail   The status code is not 200 or 202