繁体   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