首页 > 代码库 > SOAPUI中文教程---转移属性

SOAPUI中文教程---转移属性

Property Transfer TestSteps用于在TestSteps和它们包含的TestCase,TestSuite和Project之间传递属性。它们在许多情况下非常有用,特别是当涉及包含XML的属性时,例如
  1. 从XML消息中提取值,例如来自SOAP Response的sessionID
  2. 将值写入XML消息,例如保存的sessionID或认证数据
  3. 在属性之间传输复杂的XML内容

1. The Property-Transfer Window

创建或双击现有的Property-Transfer TestStep将打开以下窗口:

 

技术分享

左侧的列表显示了此TestStep中配置的传输,添加和管理所需的数量。选择传输时(如上图截图中的“TransferUser”),右侧的区域允许您根据需要进行配置;
  • 顶部的源区域指定源属性和可选的XPath用于传输
  • 目标区域指定目标属性和可选的XPath进行传输
  • 下面的区域将不同的配置选项设置在传输级别上。

底部的传输日志显示窗口打开时的最新执行传输及其实际传输的值。 

可用的配置选项可以改变传输的内容,并将其各自的定义为:

 

  • Fail transfer on error - 尝试执行转移时发生任何错误时,转移步骤将失败
  • Transfer text content - 只会传送文字内容。不会保留结构元素
  • Transfer to all - 匹配的值将被转移到所有的目标位置,以防多个匹配
  • Entitize transferred values - 这些值将具有某些字符,例如&符号(“&”)替换为相应的字符实体值(“&”)
  • Set null on missing source - 如果源资源丢失,目标将设置为null。这意味着如果目标已经有一个值,它将丢失
  • Ignore empty/missing values - 这意味着如果源的匹配值为空或丢失,则它将被忽略,并且目标值保持不变
  • Use XQuery - 当尝试提取源值时SoapUI将假定源是XQuery而不是默认的XPath
  • Transfer child nodes - SoapUI不会尝试从匹配的源节点进行任何文本提取,而是选择匹配节点的子节点,从而保留子树XML结构

2. 转移执行

When the PropertyTransfer TestStep is executed during a TestCase run, each transfer in the Property Transfer is performed by selecting the property specified by the transfers source step, property and optional XPath expression and copying their value(s) to the destination steps specified property using an optional XPath expression. If XPath expressions are specified, soapUI will try to replace the target node with the source node if they are of the same type. If not (for example when assigning text() to an @attribute), soapUI will do its best to copy the value as possible.

Source and target XPath expressions must both point to existing nodes in their respective properties, the source property obviously requires the node so it can be selected, the target property requires the node so it can be found and overwritten.

If any of the transfers fail due to missing matches of any of the XPath expression, an error is printed and the step will either fail or go on, depending on of the "fail on error" option has been selected for that transfer. TestCase execution is only aborted if the TestCases‘ "Fail on error" option has been set as described for under TestCase Options.

3. An example: transferring a session ID from a response to a following request

当在TestCase运行期间执行PropertyTransfer TestStep时,通过选择传输源步骤,属性和可选XPath表达式指定的属性来执行属性传输中的每次传输,并使用以下命令将其值复制到目标步骤指定的属性可选的XPath表达式。如果指定了XPath表达式,如果它们的类型相同,soapUI将尝试用源节点替换目标节点。如果不同(例如,将text()分配给@attribute),soapUI将尽可能复制该值。

 

技术分享

技术分享

技术分享

  1. 添加并执行登录请求到一个空的TestCase,所以你需要有一个有效的响应来处理;

     
  2. 请求后创建Property-Transfer TestStep,并向其中添加属性传输;

     
  3. 将源属性配置为“Response”,然后在源属性工具栏右侧选择“向导”按钮。这将打开一个方便的向导,用于选择要传输的所需源节点(在标准soapUI中,您将必须手动创建生成的XPath语句)。


     
  4. 选择节点,然后按OK; soapUI为您生成相应的XPath语句;

技术分享

现在,所有的源都设置完成,您的下一步是配置属性转移的目标,即应写入的位置。我们有两个选择;
  1. 将其写入可以由需要使用sessionID的所有后续测试步骤引用的临时属性。如果我们要在几个地方使用sessionID或者TestSteps,这是最好的解决方案
  2. 将其直接传输到应该使用该值的目标TestStep。如果您只想在一个地方使用该值,这将更容易


3.1. 写入临时属性

让我们将属性保存到我们用“SessionID”命名的TestCase属性中;

技术分享

现在当执行时,SessionID被转移到指定的TestCase属性,并且可以稍后在通过标准属性扩展(或另一个属性转移)的任何请求中使用,例如在我们可以使用的注销请求中

<logout>
<sessionID>${#TestCase#SessionID}</sessionID>
</logout>

当请求发送时,将使用已保存的SessionID。

3.2. 直接写入所需的目标TestStep

将目标设置为注销请求,并再次使用XPath向导(或XPath技能)创建目标XPath语句;

技术分享


技术分享

现在执行此操作时,源目标值将从响应中直接提取并写入请求消息。

SOAPUI中文教程---转移属性