簡體   English   中英

如何將XML字符串傳遞給UFT / QTP中的子API測試?

[英]How to pass over an XML string to a sub API-test in UFT/QTP?

作為一個簡單的例子,在API測試流程中,我使用了另一個API測試模塊,必須將XML字符串(消息)傳遞給該模塊。 但是,一旦子模塊獲得了XML字符串,它就會在xml標記之前引發有關空白的運行錯誤步驟! 我什至在進入子模塊之前就用自定義代碼修剪輸入字符串,但得到相同的錯誤!

錯誤信息:“意外的XML聲明XML聲明必須是文件中的第一個節點,也沒有空格字符被允許在它之前出現。”

任何提示,請提前感激:)

事情就是這樣,UFT API將參數作為XML結構處理。 由於參數值也是XML結構,因此應將其作為文本(轉義)而不是XML傳遞。 因此,代替此:

<?xml version="1.0" encoding="UTF-8"?><shiporder orderid="889923" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="shiporder.xsd"><orderperson>John Smith</orderperson><shipto><name>Ola Nordmann</name><address>Langgt 23</address><city>4000 Stavanger</city><country>Norway</country></shipto><item><title>Empire Burlesque</title><note>Special Edition</note><quantity>1</quantity><price>10.90</price></item><item><title>Hide your heart</title><quantity>1</quantity><price>9.90</price></item></shiporder>

您應該通過此:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;shiporder orderid=&quot;889923&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;shiporder.xsd&quot;&gt;&lt;orderperson&gt;John Smith&lt;/orderperson&gt;&lt;shipto&gt;&lt;name&gt;Ola Nordmann&lt;/name&gt;&lt;address&gt;Langgt 23&lt;/address&gt;&lt;city&gt;4000 Stavanger&lt;/city&gt;&lt;country&gt;Norway&lt;/country&gt;&lt;/shipto&gt;&lt;item&gt;&lt;title&gt;Empire Burlesque&lt;/title&gt;&lt;note&gt;Special Edition&lt;/note&gt;&lt;quantity&gt;1&lt;/quantity&gt;&lt;price&gt;10.90&lt;/price&gt;&lt;/item&gt;&lt;item&gt;&lt;title&gt;Hide your heart&lt;/title&gt;&lt;quantity&gt;1&lt;/quantity&gt;&lt;price&gt;9.90&lt;/price&gt;&lt;/item&gt;&lt;/shiporder&gt;

在這里,您可以找到一個在線工具,可以幫助您轉義XML: http : //www.freeformatter.com/xml-escape.html

編輯:

解決此問題的一種動態方法是在執行調用API測試的步驟之前,在參數中包含自動轉義XML值的代碼。 例:

    public void CallSTTest4_OnBeforeExecuteStepEvent(object sender, STActivityBaseEventArgs args)
    {                       
        CallSTTest4.InputProperties.GetElementsByTagName("XMLString").Item(0).InnerXml = System.Security.SecurityElement.Escape(CallSTTest4.InputProperties.GetElementsByTagName("XMLString").Item(0).InnerXml);    
    }

注意:就我而言,包含xml結構的輸入參數是“ XMLString”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM