获取HTTPError: 400客户端错误:错误请求url: http://localhost:2707/dummy1/



get HTTPError: 400 Client Error: Bad Request for url: http://localhost:2707/dummy1/dummy2执行Post调用时输入:我在python上使用机器人框架,我想测试失败的场景,在那里我传递了4个参数:基本URL,终点,空体,带有Post请求关键字的授权承载令牌,如下所示

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

实际行为:

处失败
${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

期望:我想从${postResponse获得状态码400。Status_code}并通过我的测试场景

您可以使用BuiltIn Robot Framework关键字从几个不同的角度来处理这个问题。基本上,您需要运行调用,返回值并忽略初始错误,因为Robot在默认情况下无法通过第一个引发的异常的测试。

根据问题描述,我会使用运行关键字和预期错误。例如像这样的

*** 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}

这样关键字将只显示已传递的如果你得到的错误与STARTS:之后的匹配。由于Run Keyword and Expect Error的行为,该方法将以字符串形式返回响应对象。

最新更新