简体   繁体   English

如何将XML字符串传递给UFT / QTP中的子API测试?

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

As a simple case, in an API-test Flow I am using another API-Test module to which I have to pass an XML string(a message). 作为一个简单的例子,在API测试流程中,我使用了另一个API测试模块,必须将XML字符串(消息)传递给该模块。 But once the sub module gets the XML string it throws following run error step regarding the whitespace before the xml tag! 但是,一旦子模块获得了XML字符串,它就会在xml标记之前引发有关空白的运行错误步骤! I even trim the input string in a custom code right before entrance to the sub-module but get the same error! 我什至在进入子模块之前就用自定义代码修剪输入字符串,但得到相同的错误!

Error Message: "Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it." 错误信息:“意外的XML声明XML声明必须是文件中的第一个节点,也没有空格字符被允许在它之前出现。”

Any hint is appreciated in advance :) 任何提示,请提前感激:)

Here is the thing, UFT API handles parameters as XML structures. 事情就是这样,UFT API将参数作为XML结构处理。 Since the parameter value is an XML structure as well, it should be passed as text (escaped) and not as XML. 由于参数值也是XML结构,因此应将其作为文本(转义)而不是XML传递。 So, instead of this: 因此,代替此:

<?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>

You should pass this: 您应该通过此:

&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;

Here you can find an online tool that can help you escape your XML: http://www.freeformatter.com/xml-escape.html 在这里,您可以找到一个在线工具,可以帮助您转义XML: http : //www.freeformatter.com/xml-escape.html

EDIT: 编辑:

A dynamic way to get around this would be to include the code that automatically escapes de XML value in the parameter before executing the step where you're calling your API test. 解决此问题的一种动态方法是在执行调用API测试的步骤之前,在参数中包含自动转义XML值的代码。 Example: 例:

    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);    
    }

Note: In my case, the input parameter containing the xml structure is "XMLString" 注意:就我而言,包含xml结构的输入参数是“ XMLString”

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

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