【问题标题】:Robot Framework - Ride: Post Request unable to get response body机器人框架 - 骑行:发布请求无法获得响应正文
【发布时间】:2016-10-31 22:28:02
【问题描述】:

我编写了这个测试,我在后台系统中发布了一份合同。我用一个forloop创建了一个关键字,它从系统文件夹中提取xml。

为了使测试通过,我想检查响应中的值。当我使用 RF/Ride 执行此测试时,无法获取响应体,如果我在 Postman/SoapUI 中进行相同的测试,我可以看到响应体

当我询问 response.headers 时,我得到了预期的数据

Ride 中的测试:

*** Test Cases *** XML
    [Template]    Template post contract BO
    apitest1.xml
*** Keywords *** Template post contract BO
    [Arguments]    @{bestandlijst}
    : FOR    ${bestand}    IN    @{bestandlijst}
    \    &{headers}=    Create dictionary    Content-type=application/xml
    \    ${bestandophalen}=    Get Binary File    ${bestand}
    \    Create Session    Backoffice    https://url        
    \    ${response}=    Post Request    Backoffice    /isCOBOL(API_V1_ARCONTRACT)    headers=&{headers}    data=${bestandophalen}
    \    log    ${response.headers} -> this works
    \    log    ${response.body} -> this doesn't work

Postman 中的响应:

<?xml version="1.0" encoding="UTF-8"?>
<Retourbericht xmlns="http://url/schemas/things">
    <Statuscode>OK</Statuscode>
    <Statusmelding>Contract opgeslagen in de backoffice.</Statusmelding>
    <TransactionResponse>Onbekend.</TransactionResponse>
</Retourbericht>

骑行时出现错误:

FAIL : Resolving variable '${response.body}' failed: AttributeError: 'Response' object has no attribute 'body'
  1. 我的测试是否有错误导致此错误?
  2. 有没有人遇到过同样的问题,您的解决方案是什么?

【问题讨论】:

  • 你用的是什么库?是否记录了返回具有body 属性的对象?标准 python 请求库不返回具有body 属性的对象。
  • 我们使用 requests 库。嗯,这是一个很好的问题,我的同事也使用它,并且对他有效。好吧,他的 api 给出了 json 响应和我的 xml。我检查了图书馆,但找不到任何关于返回对象的信息

标签: robotframework


【解决方案1】:

如果你使用RequestsLibrary,它是建立在python requests 库之上的。该库返回一个响应对象,但该对象没有body 属性。这就是你得到 AttributeError 的原因。

如果您想要结构化数据,您可以使用${response.json()},如果您想要将响应的原始文本作为字符串,您可以使用${response.text}

【讨论】:

    猜你喜欢
    • 2016-06-09
    • 2022-08-12
    • 2020-10-03
    • 2020-04-20
    • 2017-07-29
    • 2020-10-27
    • 2020-10-24
    • 1970-01-01
    • 2023-04-01
    相关资源
    最近更新 更多