RestAssured:无法使用 Xpath 解析所需值的响应

RestAssured: unable to parse response for required value using Xpath

我需要一点帮助来适应 Rest Assured。

我有一个请求,我正在构建一个字符串(这些测试必须暂时简化,因为测试人员将维护这些测试,因此使用更高级的概念,如 JAXB 是次要的)。

String request = myPayRequest.searchPaymentOptions(dataObject);

我传递的字符串实际上是一个肥皂信封,看起来像这样:

<?xml version="1.0" encoding="UTF-8" ?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"     
xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Body>
<searchPaymentOptions xmlns="http://website.stuff.com">
  <header xmlns="">
    <ns1:credentials organisation="stuff" password="password" username="foobar" xmlns:ns1="http://website.stuff.com"/>
    <ns2:invocationDetails system="FindAndBook" trackingId="qqdG6jVIqIkw459wSj0ymokh" type="NATIVE" xmlns:ns2="http://website.stuff.com"/>
  </header>
  <criteria xmlns="">
    <performFundingCheck>false</performFundingCheck>
    <preferredPayment>
      <productSupplier>
        <ns3:thingyCode xmlns:ns3="http://website.stuff.com">ABC</ns3:thingyCode>
      </productSupplier>
      <requiredFunds amount="35.63" currency="GBP"/>
    </preferredPaymentCriterions>
  </criteria>
</searchPaymentOptions>

响应:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
    <ns2:preferredPaymentSearch xmlns:ns2="http://website.com">
        <header>
            <issueAudit>
                <info>
                    <issues>
                        <issue host="website/10.2.333.46" issueCode="Host" issueId="b5b006c7-42b6-4d8c-8e07-c2f2e1634a9e" issueMessage="website/10.2.333.46" severity="INFO" timestamp="2017-03-23T13:04:53.106Z"/>
                        <issue host="website/10.2.333.46" issueCode="TrackingId" issueId="38f73e0d-5c42-415b-b88d-1aba098e1a59" issueMessage="qqdG6jVIqIkw459wSj0ymokh" severity="INFO" timestamp="2017-03-23T13:04:53.106Z"/>
                    </issues>
                </info>
                <warnings>
                    <issues/>
                </warnings>
                <errors>
                    <issues/>
                </errors>
            </issueAudit>
            <status>SUCCESS</status>
            <ver>1.0.0-SNAPSHOT</ver>
        </header>
        <results>
            <preferredPaymentResults>
                <preferredPaymentCriterion>
                    <productSupplier>
                        <ns2:actorCode>ABC</ns2:actorCode>
                    </productSupplier>
                    <requiredFunds amount="35.63" currency="GBP"/>
                </preferredPaymentCriterion>
                <preferredPaymentOption>
                    <preferredCardOption>
                        <cardForm>GENERATABLE</cardForm>
                        <cardType>VISA_CREDIT</cardType>
                        <provider>wibble</provider>
                    </preferredCardOption>
                </preferredPaymentOption>
            </preferredPaymentResults>
        </results>
    </ns2:preferredPaymentSearch>
</soap:Body>

响应应该并且确实包含带有以下片段的 SOAP 信封 <status>SUCCESS</status>

当我尝试以下操作时:

   String response = given().body(request)
            .when().post().andReturn().asString();

    expect().body(hasXPath("//soap:Body//*[name()='status']", equalTo("FAILURE")));

...那么即使 SUCCESS 是数据值,测试也会通过

同样,我尝试使用不同的语法,但这也给出了误报:

    given().config(newConfig().xmlConfig(xmlConfig().with().namespaceAware(true)));
    given().body(request).post();
    expect().body(hasXPath("//soap:Body//*[name()='status']", equalTo("SUCCESS")));

我哪里错了?我相信我实际上不需要将响应作为字符串来读取,所以示例 1 可能很臭。但是示例 2 也通过了,但应该会失败。

尝试以下格式:

expect().body("Envelope.Body.preferredPaymentSearch.header.status", equalTo("SUCCESS"))
    .given().body(request)
    .when().post()

如果您刚开始使用 REST-assured,看看您是否可以评估 Karate 因为它更适合测试 SOAP 和 XML.

免责声明:我是开发者。

这里是 link 到 documentation of the SOAP / XML support

原生支持 XPath 表达式,大多数情况下您可以按原样比较两个有效负载。