XPath 验证 Wiremock 的 matchesXPath 表达式

4

我正在使用Wiremocks创建测试用例,并生成响应模拟。

我有一个如下的XML请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xw="http://example.com">
    <soapenv:Header/>
    <soapenv:Body>
        <xw:gen>
            <xw:input>
                <xw:element1>0100</xw:element1>
                <xw:element2>741</xw:element2>
                <xw:element3>JAVA</xw:element3>
                <xw:element4>123</xw:element4>
            </xw:input>
            <xw:global>
                <xw:obj1>
                    <xw:attr1>john</xw:attr1>
                    <xw:attr2>doe</xw:attr2>
                </xw:obj1>
            </xw:global>
        </xw:gen>
    </soapenv:Body>
</soapenv:Envelope>

我只需要验证xw:input/xw:element1 = 0100xw:input/xw:element2 = 741,并且我需要确保xw:global节点有值。唯一的条件是xw:global不为空。(该节点可以是<xw:global></xw:global>)。
这是我的JSON模拟数据:
{
    "request" : {
        "url" : "/myweb/myrequest.asmx",
        "headers": {
            "SOAPAction": {
                "equalTo": "\"http://example.com/gen\""
            }
        },
        "bodyPatterns" : [ {
            "matchesXPath": "//xw:input[xw:element1=\"0100\" and xw:element2=\"741\" ]",
            "xPathNamespaces" : {
                "xw" : "http://example.com"
            }
        }]
    },
    "response" : {
        "status" : 200,
        "headers": {
            "Content-Type": "text/xml;charset=UTF-8"
        },
        "body" : "<Abody>"
    }
}

问题是:如何验证节点xw:global不为空或不为null?
我尝试了使用matchesXPath但没有成功: "matchesXPath": "//xw:input[xw:element1=\"0100\" and xw:element2=\"741\" ] and count(//xw:global) > 0" 谢谢。

你在存根定义中指定了命名空间吗?如果没有,那么你不能在XPath表达式中使用它们。 - Tom
是的。我指定了命名空间。谢谢。 - dani77
1个回答

2

我不熟悉WireMock,但您可以尝试以下XPath:

"//xw:gen[xw:input[xw:element1=\"0100\" and xw:element2=\"741\"]][xw:global/*]"

上面的XPath检查是否存在任何xw:gen,该元素必须同时满足以下两个条件:
  • [xw:input[xw:element1=\"0100\" and xw:element2=\"741\"]] :具有子元素xw:input,且该子元素满足您提到的标准
  • [xw:global/*] :具有子元素xw:global,并且该子元素至少包含一个其他名称的元素

嗨 @har07。在我的情况下,节点 xw:global 可以是 xw:global/,我使用了这个验证:[count(xw:globalObj)>0]。但是你的回复非常有用。谢谢! - dani77

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接