SoapUI 模拟服务/模拟操作中的 XPATH 调度

XPATH Dispatch in SoapUI Mock service/ Mock operation

我是 SoapUI 的新手,正在尝试了解 XPATH 分派在模拟服务中用于模拟操作的用法。

这是我到目前为止所做的

  1. 为计算器创建了模拟服务
  2. 添加模拟运算减法

以下是操作请求示例

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cal="http://www.parasoft.com/wsdl/calculator/">
   <soapenv:Header/>
   <soapenv:Body>
      <cal:subtract>
         <cal:x>1</cal:x>
         <cal:y>1</cal:y>
      </cal:subtract>
   </soapenv:Body>
</soapenv:Envelope>

以下是针对相同问题的示例响应

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cal="http://www.parasoft.com/wsdl/calculator/">
   <soapenv:Header/>
   <soapenv:Body>
      <cal:subtractResponse>
         <cal:Result>?</cal:Result>
      </cal:subtractResponse>
   </soapenv:Body>
</soapenv:Envelope>

我能够理解其他调度,但不能理解 XPATH,因为以下是我在 XPATH 调度中输入的内容

declare namespace cal='http://www.parasoft.com/wsdl/calculator/';
declare namespace soapenv='http://schemas.xmlsoap.org/soap/envelope/';
//cal:subtract/cal:x

还观察到,如果我已经使用 SCRIPT 调度并从下拉列表切换到 XPATH,则脚本在 declaration/scripting box/area

中可见

问题如下:

  1. XPATH 和 SCRIPT 调度是否相同
  2. 如果不是,那么 XPATH 调度实际上如何工作以识别从所有形式的 MockResponses 列表中调度什么响应

请帮我解决这个问题。

PS: 我已经过了 http://www.soapui.org/soap-mocking/reference/mockoperation.html http://www.soapui.org/soap-mocking/mockoperations-and-responses.html

您在问题中提到的 soapUI 文档是获取信息的正确位置。但是,可用的信息并不完整。

找了一段时间,找到了详情。

最初,在 XpathScript 调度方法之间感到困惑。

这里是您要找的额外信息:

Is XPATH and SCRIPT dispatch same

答案是。两者不同

If not, how does the XPATH dispatch actually work to identify what response to dispatch out of all form MockResponses list

在文档中找到的 following 信息:

XQUERY - This is similar to the QUERY_MATCH but not quite as powerful; an XPath expression is applied to the incoming request and the resulting value is used for selecting which MockResponse to be returned. In our previous example of search results, we could set the XPath expression to select a search term and then create MockResponses named after each expected value. The advantage being that we don’t need to add new XPath statements for new search criteria, just another MockResponse.

假设您为 Mock 服务的 subtract 操作创建了多个响应说 PositiveResponseNegativeResponseZeroResponse .

以下是您可能希望在请求中应用并发送适当的 响应 的示例条件。当然,你需要多少就多少。

  1. PositiveResponse - 如果 x、y 值分别为 10、5。
  2. NegativeResponse - 如果 x、y 值分别为 5、10。
  3. ZeroResponse - 否则(如果满足上述 none,则这是强制性的)。

以下是您需要在为 XPATH 调度模式

提供的编辑器中编写的方式
declare namespace cal='http://www.parasoft.com/wsdl/calculator/';
if (//cal:subtract/cal:x[. = '10'] and //cal:subtract/cal:y[. = '5']) then
    'PositiveResponse'
    else 
    if (//cal:subtract/cal:x[. = '5'] and //cal:subtract/cal:y[. = '10']) then
    'NegativeResponse'
    else
    'ZeroResponse'

希望您现在了解并区分 SCRIPT 调度模式。

I think the confusion created because both SCRIPT and XPATH shows an editor of same type. But the content inside of it will be entirely different. Also you can easily see a message on top of the editor that log, context, mockRequest availability of variables if you select SCRIPT Dispatch mode and it vanishes when XPATH is choosen.

只是给出 SCIRPT 示例,以防您对此感兴趣:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder(mockRequest.requestContent)
def x = holder.getNodeValue("//*:x") as int
def y = holder.getNodeValue("//*:y") as int
context.result = x - y

A simple test can be (to differentiate between the two), copy the above script for xpath and try testing and soap fault is received saying does not know groovyUtils. This test will confirm that xpath and script are different.

这里您可能不需要创建多个响应,因为上面的代码可以处理动态输入值并在响应中设置 resultsubtract 操作的 MockReponse 可能如下所示,place holder as ${result}.

脚本的模拟响应:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cal="http://www.parasoft.com/wsdl/calculator/">
   <soapenv:Header/>
   <soapenv:Body>
      <cal:subtractResponse>
         <cal:Result>${result}</cal:Result>
      </cal:subtractResponse>
   </soapenv:Body>
</soapenv:Envelope>

希望这能说明问题。