带有请求库的 Roboframework CDATA xml
Roboframework CDATA xml with request library
我正在尝试使用请求库自动测试 soap/xml 消息。如果尝试过其他库,但我 运行 遇到了问题(主要是缺乏使用 URL 访问 WSDL 的可能性)。我在 sennse 中成功使用了请求库,我设法发送了一条消息并取回了响应消息。到目前为止一切顺利,但我在日志记录中得到的响应有一个问题。响应中的一个标记有一个 <[!CDATA[]] 部分,它在响应中的格式不正确。这使得检索响应的内容非常麻烦,如果不是不可能的话。我试着看看解析成 xml 是否可行,但我得到的结果是一样的。我希望有人有好的建议以正确的格式返回响应。请参阅下面我的机器人脚本以及我在机器人中获得的预期和实际响应(所有内容都经过编辑,不共享任何敏感数据)。
机器人脚本:
*** Settings ***
Library RequestsLibrary
Library Collections
Library SeleniumLibrary
Library String
Library OperatingSystem
Library XML use_lxml=${TRUE}
Library SoapLibrary
Resource ../Resources/Inputs/cardscan240xml.robot
*** Variables ***
omitted
*** testcases ***
create session CardScan ${base_url}
#Create Request Headers
${request_header}= create dictionary SOAPAction="document/http://xxxx.com/CustomUI:xxxxxxxxxxMember" Content-Type=text;charset=utf-8
#Send the XML request body
${SAVE_Response}= Post Request CardScan ${channel_url} headers=${request_header} data=${CARDSCAN}
#Log Response and Status Code Details
log to console ${SAVE_Response.status_code}
log ${SAVE_Response.headers}
log ${SAVE_Response.text}
这是我在机器人框架中的获取方式
我应该得到的响应(这是我从 soapui 得到的。):
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns:xxxxxxxxxxxLoyaltyMember_Output xmlns:ns="http://xxxx.com/xxxUI">
<ns:MessageType>0250</ns:MessageType>
<ns:MessageXML><![CDATA[<?xml version="1.0" encoding="UTF-8"?><PrivateData><LoyaltyMessageNumber2><Customer xxxxID="5563" xxxID="1" xxxID="1254" xxxType="2" BusinessDate="2005/02/16"><LoyaltyInfo FirstName="Chris" CardID="2600787879882" HomeStore="" CardStatus="00" ServerDate="2021/07/07" CardIDType="P" LastName="xxxxkoren"><EligibleOffers></EligibleOffers><Segments><xxxxID="4"></Seg><xxxx="4"></Seg></Segments><Accounts><xxxxID="1000" Value="181"></Acc></Accounts><Email>c.xxxxxxxxx22@eu.xxxxxx.com</Email><EmailValidFlag>Y</EmailValidFlag><EReceiptFlag></EReceiptFlag></LoyaltyInfo></Customer></LoyaltyMessageNumber2><xxxxxxxCustomerInfo FreeCustomerInfo3="" FreeCustomerInfo4="" FreeCustomerInfo1="" FreeCustomerInfo5="" FreeCustomerInfo2=""></xxxxxCustomerInfo><xxxxxxInvoiceData Address2="" Address5="" CustomerName="Chris Tester" VatNr="" CustomerNumber="111111111" Address1="" Address4="" CompanyName="" Address3=""></xxxxxInvoiceData></PrivateData>]]></ns:MessageXML>
<ns:ResponseCode>00</ns:ResponseCode>
</ns:xxxxxxxxLoyaltyMember_Output>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
这是我在 Robot 框架中获取它的方式:
POST Response : url=https://xxxxxxvsbl.nl.xxxxxx.net:8113/xxi_anxx_xxu/xxxxxxt.swe?SWEExtSource=AnonWebService&SWEExtCmd=Execute
status=200, reason=
body=<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-
ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><SOAP-ENV:Body>
<ns:xxxxxInboundLoyaltyMember_Output xmlns:ns="http://xxxxx.com/xxxxxUI">
<ns:xxxxxxType>0250</ns:xxxxxType><ns:MessageXML><?xml version="1.0"
encoding="UTF-8"?><PrivateData><xxxxxxNumber2><Customer
xxxxxID="124" xxxxxD="1" xxxxxID="124"xxxxxType="2"
BusinessDate="2005/02/16"><LoyaltyInfo FirstName="Chris"
CardID="211111111879882" HomeStore="" CardStatus="00"
ServerDate="2021/07/07" CardIDType="P"LastName="xxxxxelkoren"><EligibleOffers></EligibleOffers><Segments>&
lt;Seg ID="4"></Seg><Seg ID="4"></Seg></Segments><Accounts><Acc ID="1000" Value="181"></Acc></Accounts><Email>c.xxxxxkoren22@eu.xxxxxx.com</Email><EmailValidFlag>Y</EmailValidFlag><EReceiptFlag></EReceiptFlag></LoyaltyInfo></Customer></xxxxxxMessageNumber2><xxxxxxCustomerInfo FreeCustomerInfo3="" FreeCustomerInfo4="" FreeCustomerInfo1="" FreeCustomerInfo5="" FreeCustomerInfo2=""></xxxxxxCustomerInfo><xxxxxxxInvoiceData Address2="" Address5="" CustomerName="Chris Tester" VatNr="" CustomerNumber="2600787879882" Address1="" Address4="" CompanyName="" Address3=""></xxxxxxxxInvoiceData></PrivateData>
</ns:MessageXML>
<ns:ResponseCode>00</ns:ResponseCode>
</ns:xxxxxxInboundLoyaltyMember_Output>
</SOAP-ENV:Body></SOAP-ENV:Envelope>
抱歉,我很难让机器人响应变得非常可读,但我确实希望很明显常规标签不会造成任何问题。但是 cdata 部分中的 CDATA 标记和双引号不可读。我希望有人有解决这个问题的建议。
用xml库,自动删除CDATA并编码对应部分,cf https://www.freeformatter.com/xml-escape.html
' is replaced with '
" is replaced with "
& is replaced with &
< is replaced with <
> is replaced with >
如果你想让它更漂亮,你可以把 xml 当作一个字符串,然后用 :
替换这些标签
.replace("<","<").replace(">",">").replace("&","&") etc...
否则,我认为存在一些方法:
https://wiki.python.org/moin/EscapingXml
我正在尝试使用请求库自动测试 soap/xml 消息。如果尝试过其他库,但我 运行 遇到了问题(主要是缺乏使用 URL 访问 WSDL 的可能性)。我在 sennse 中成功使用了请求库,我设法发送了一条消息并取回了响应消息。到目前为止一切顺利,但我在日志记录中得到的响应有一个问题。响应中的一个标记有一个 <[!CDATA[]] 部分,它在响应中的格式不正确。这使得检索响应的内容非常麻烦,如果不是不可能的话。我试着看看解析成 xml 是否可行,但我得到的结果是一样的。我希望有人有好的建议以正确的格式返回响应。请参阅下面我的机器人脚本以及我在机器人中获得的预期和实际响应(所有内容都经过编辑,不共享任何敏感数据)。
机器人脚本:
*** Settings ***
Library RequestsLibrary
Library Collections
Library SeleniumLibrary
Library String
Library OperatingSystem
Library XML use_lxml=${TRUE}
Library SoapLibrary
Resource ../Resources/Inputs/cardscan240xml.robot
*** Variables ***
omitted
*** testcases ***
create session CardScan ${base_url}
#Create Request Headers
${request_header}= create dictionary SOAPAction="document/http://xxxx.com/CustomUI:xxxxxxxxxxMember" Content-Type=text;charset=utf-8
#Send the XML request body
${SAVE_Response}= Post Request CardScan ${channel_url} headers=${request_header} data=${CARDSCAN}
#Log Response and Status Code Details
log to console ${SAVE_Response.status_code}
log ${SAVE_Response.headers}
log ${SAVE_Response.text}
这是我在机器人框架中的获取方式
我应该得到的响应(这是我从 soapui 得到的。):
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns:xxxxxxxxxxxLoyaltyMember_Output xmlns:ns="http://xxxx.com/xxxUI">
<ns:MessageType>0250</ns:MessageType>
<ns:MessageXML><![CDATA[<?xml version="1.0" encoding="UTF-8"?><PrivateData><LoyaltyMessageNumber2><Customer xxxxID="5563" xxxID="1" xxxID="1254" xxxType="2" BusinessDate="2005/02/16"><LoyaltyInfo FirstName="Chris" CardID="2600787879882" HomeStore="" CardStatus="00" ServerDate="2021/07/07" CardIDType="P" LastName="xxxxkoren"><EligibleOffers></EligibleOffers><Segments><xxxxID="4"></Seg><xxxx="4"></Seg></Segments><Accounts><xxxxID="1000" Value="181"></Acc></Accounts><Email>c.xxxxxxxxx22@eu.xxxxxx.com</Email><EmailValidFlag>Y</EmailValidFlag><EReceiptFlag></EReceiptFlag></LoyaltyInfo></Customer></LoyaltyMessageNumber2><xxxxxxxCustomerInfo FreeCustomerInfo3="" FreeCustomerInfo4="" FreeCustomerInfo1="" FreeCustomerInfo5="" FreeCustomerInfo2=""></xxxxxCustomerInfo><xxxxxxInvoiceData Address2="" Address5="" CustomerName="Chris Tester" VatNr="" CustomerNumber="111111111" Address1="" Address4="" CompanyName="" Address3=""></xxxxxInvoiceData></PrivateData>]]></ns:MessageXML>
<ns:ResponseCode>00</ns:ResponseCode>
</ns:xxxxxxxxLoyaltyMember_Output>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
这是我在 Robot 框架中获取它的方式:
POST Response : url=https://xxxxxxvsbl.nl.xxxxxx.net:8113/xxi_anxx_xxu/xxxxxxt.swe?SWEExtSource=AnonWebService&SWEExtCmd=Execute
status=200, reason=
body=<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-
ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><SOAP-ENV:Body>
<ns:xxxxxInboundLoyaltyMember_Output xmlns:ns="http://xxxxx.com/xxxxxUI">
<ns:xxxxxxType>0250</ns:xxxxxType><ns:MessageXML><?xml version="1.0"
encoding="UTF-8"?><PrivateData><xxxxxxNumber2><Customer
xxxxxID="124" xxxxxD="1" xxxxxID="124"xxxxxType="2"
BusinessDate="2005/02/16"><LoyaltyInfo FirstName="Chris"
CardID="211111111879882" HomeStore="" CardStatus="00"
ServerDate="2021/07/07" CardIDType="P"LastName="xxxxxelkoren"><EligibleOffers></EligibleOffers><Segments>&
lt;Seg ID="4"></Seg><Seg ID="4"></Seg></Segments><Accounts><Acc ID="1000" Value="181"></Acc></Accounts><Email>c.xxxxxkoren22@eu.xxxxxx.com</Email><EmailValidFlag>Y</EmailValidFlag><EReceiptFlag></EReceiptFlag></LoyaltyInfo></Customer></xxxxxxMessageNumber2><xxxxxxCustomerInfo FreeCustomerInfo3="" FreeCustomerInfo4="" FreeCustomerInfo1="" FreeCustomerInfo5="" FreeCustomerInfo2=""></xxxxxxCustomerInfo><xxxxxxxInvoiceData Address2="" Address5="" CustomerName="Chris Tester" VatNr="" CustomerNumber="2600787879882" Address1="" Address4="" CompanyName="" Address3=""></xxxxxxxxInvoiceData></PrivateData>
</ns:MessageXML>
<ns:ResponseCode>00</ns:ResponseCode>
</ns:xxxxxxInboundLoyaltyMember_Output>
</SOAP-ENV:Body></SOAP-ENV:Envelope>
抱歉,我很难让机器人响应变得非常可读,但我确实希望很明显常规标签不会造成任何问题。但是 cdata 部分中的 CDATA 标记和双引号不可读。我希望有人有解决这个问题的建议。
用xml库,自动删除CDATA并编码对应部分,cf https://www.freeformatter.com/xml-escape.html
' is replaced with '
" is replaced with "
& is replaced with &
< is replaced with <
> is replaced with >
如果你想让它更漂亮,你可以把 xml 当作一个字符串,然后用 :
替换这些标签.replace("<","<").replace(">",">").replace("&","&") etc...
否则,我认为存在一些方法: https://wiki.python.org/moin/EscapingXml