Getting HTTPError: 400 Client Error: Bad Request for url: http://localhost:2707/dummy1/dummy2 When Executing Post call Input: I'm using robot framework on python and I would like to test the failed scenario, where I pass 4 arguments : Base URL, End Point, empty body , with Authorization Bearer Token for the Post request key word as shown below
Library RequestsLibrary
Library OperatingSystem
Library Collections
Library SeleniumLibrary
Library OperatingSystem
**Post API Request**
[Documentation] Post Request call
[Arguments] ${base_url} ${endpoint_uri} ${jsonBody}=${None} ${tokenGenerated}=${None}
Create Session mysession ${base_url} disable_warnings=1
${headers} Create Dictionary Content-Type=application/json Authorization=Bearer ${tokenGenerated}
${postResponse}= POST On Session mysession ${endpoint_uri} json=${jsonBody} headers=${headers}
${statusCode} = Convert to string ${postResponse.status_code}
log to console ${statusCode}
Should Be Equal As Strings ${postResponse.status_code} 400
***Actual Behavior:**
It fails at ${postResponse}= POST On Session mysession ${endpoint_uri} json=${jsonBody} headers=${headers}
variable ${postResponse} is having "400 Client Error: Bad Request for url: http://localhost:2707/dummy1/dummy2" and Test case is thus failing directly
**Expectation :** I would like to get status code as 400 from ${postResponse.status_code} and pass my test scenario*
**Libraries Using:**
requests 2.28.1
robotframework 6.0.1
robotframework-jsonlibrary 0.5
robotframework-requests 0.9.4
robotframework-seleniumlibrary 6.0.0
Can someone please help me to assist on to get status code from this
Actual Behavior: It fails at
${postResponse}= POST On Session mysession ${endpoint_uri} json=${jsonBody} headers=${headers}
variable ${postResponse} is having "400 Client Error: Bad Request for url: http://localhost:2707/dummy1/dummy2" and Test case is thus failing directly
Expectation : I would like to get status code as 400 from ${postResponse.status_code} and pass my test scenario
CodePudding user response:
You can use a BuiltIn Robot Framework keywords to approach this issue from couple different angles. Basically you'll need to run the call, return the value and ignore the initial error, because Robot is failing the test on the first raised exception by default.
Based on the issue description I would use Run Keyword and Expect Error. For example something like this
*** Test Cases ***
Post API Request**
[Documentation] Post Request call
[Arguments] ${base_url} ${endpoint_uri} ${jsonBody}=${None} ${tokenGenerated}=${None}
Create Session mysession ${base_url} disable_warnings=1
${headers} Create Dictionary Content-Type=application/json Authorization=Bearer ${tokenGenerated}
${postResponse}= Run Keyword and Expect Error STARTS:400 Client Error
... POST On Session mysession ${endpoint_uri} json=${jsonBody} headers=${headers}
This way the keyword will show passed only if the error you get matches what is after the STARTS:
. You'll also have access to the response object in ${postResponse}
afterwards if needed but you have already validated it when running the call.
CodePudding user response:
@Morkkis, we are able to pass the test case but when trying to fetch the response status code, it is giving me below error.
Code: ${statusCode} = Convert to String ${responseReceveied.status_code}
Error: .HTTPError: 400 Client Error: Bad Request for url: http://localhost:2707/dummy1/dummy2 Gateway_TC01_Implement gateway in .net core | FAIL | Resolving variable '${responseReceveied.status_code}' failed: AttributeError: 'str' object has no attribute 'status_code'