简体   繁体   English

如何在机器人框架中读取XML响应元素?

[英]How to read the XML response element in robot framework?

I have run SOAP API request and get the response as below using robot framework. 我已经运行了SOAP API请求,并使用机器人框架获取了如下响应。

(reply){
   return = "PGP-98-Sq0awmdslfjsdssdlsifTvZUORTLe1fgVeUwaolR14QS"
}

I would like to retrieve the value PGP-98-Sq0awmdslfjsdssdlsifTvZUORTLe1fgVeUwaolR14QS from the response XML. 我想从响应XML检索值PGP-98-Sq0awmdslfjsdssdlsifTvZUORTLe1fgVeUwaolR14QS。 I have tried to get the value using the command 我试图使用命令获取值

${token}=    Set Variable    ${API_response_Data.return} 

But it throws an error 但这会引发错误

SyntaxError: unexpected EOF while parsing (<string>, line 1)

The actual response from the SOAP UI tools looks like below (This is just for reference). SOAP UI工具的实际响应如下所示(这仅供参考)。

<soapenv:Envelope xmlns:soapenv="something" xmlns:xsd="something">
   <soapenv:Body>
      <ns1:response xmlns:ns1="http://something.com">
         <return>PGP-98-Sq0awmdslfjsdssdlsifTvZUORTLe1fgVeUwaolR14QS</return>
      </ns1:response>
   </soapenv:Body>
</soapenv:Envelope>

I have used Parse XML keyword and I get the below error. 我使用了Parse XML关键字,但出现以下错误。 在此处输入图片说明

and when I use 当我使用

 ${Token}=    Get Element    ${API_response_Data}    .//*return
    Log    ${Token.text}

I get the below error. 我收到以下错误。 在此处输入图片说明

Please let me know how to extract the value from the return tag? 请让我知道如何从返回标记中提取值?

You could use robot framework's XML library to parse the response XML and then get the text of the specific element. 您可以使用机器人框架的XML库来解析响应XML,然后获取特定元素的文本。 For example: 例如:

Demo
    ${root}=    Parse XML   soap_res.xml
    ${return}=  Get Element     ${root}     .//*return
    Log     ${return.text}

The Get Element keywords returns an XML element object, you need its text attribute. Get Element关键字返回一个XML元素对象,您需要它的text属性。 The output is the following: 输出如下:

在此处输入图片说明

The SudsLibrary depends on the Suds module. SudsLibrary取决于Suds模块。 Sadly this module is no longer maintained and as time passes the limitations of this module are becoming clearer. 遗憾的是,该模块不再维护,并且随着时间的流逝,该模块的局限性越来越清晰。

In your case the object that is returned is difficult to use. 在您的情况下,返回的对象很难使用。 This is why I preferred to have the XML response returned and process it myself using the standard XML library. 这就是为什么我更愿意返回XML响应并使用标准XML库自己处理它的原因。

Before sending the request you can use the Set Return XML keyword to enable this. 在发送请求之前,您可以使用Set Return XML关键字启用此功能。 Then follow the example in the XML library for keyword Parse XML to obtain the actual value 然后按照XML库中的示例使用关键字Parse XML来获取实际值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM