Mia
Mia

Reputation: 448

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?

Upvotes: 0

Views: 4256

Answers (2)

anudeep
anudeep

Reputation: 1

If we are sending the request using ${response} variable, then ${response.status_code} will return us the status code.

#Request sent using response variable
${response}=  Post Request  Post  test/login  headers=${headers}  data=${body}  
#Validating the status is either 200 or 201
Should Be True  '${response.status_code}'=='200' or '${response.status_code}'=='201'

Upvotes: 0

Todor Minakov
Todor Minakov

Reputation: 20077

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

Upvotes: 1

Related Questions