简体   繁体   English

如何搜索具有相同名称和相同级别的XML节点值?

[英]How to search XML node values with the same name and at the same level?

I am trying to use Cucumber and REST Assured to create autoamted tests in my Gradle project. 我正在尝试使用Cucumber和REST保证在我的Gradle项目中创建自动测试。

Below is the XML Response Body: 以下是XML响应正文:

<ValidationResponse>
    <errors>
        <error>
            <field>id</field>
        </error>

        <error>
            <field>amount</field>
        </error>

    </errors>
</ValidationResponse>

I am trying to use the below REST Assured code to check that: 我正在尝试使用下面的REST保证代码来检查以下内容:

  • "id" appears in the first error field “ id”出现在第一个错误字段中
  • "amount" appears in the second error field “金额”出现在第二个错误字段中

     RestAssured.given() .auth() .preemptive() .basic(theUsername, thePassword) .contentType(theContentType) .header("Accept",theContentType) .body(theXMLBody) .when() .post(theURL) .then() .body("ValidationResponse.errors.error[0].field", equalTo("id")) .and() .body("ValidationResponse.errors.error[1].field", equalTo("amount")); 

The second field is failing because the code is just checking the first "field" it runs into. 第二个字段失败,因为代码只是检查它遇到的第一个“字段”。

Does anybody know what changes I need to make to my code so that it checks the second "field" also? 有人知道我需要对代码进行哪些更改,以便它也检查第二个“字段”吗?

Use below snippet to validate the response fields. 使用以下代码片段验证响应字段。

String responseXMLAsString = "<ValidationResponse>     <errors>         <error>             <field>id</field>         </error>          <error>             <field>amount</field>         </error>      </errors> </ValidationResponse>";
    XmlPath xmlPath = new XmlPath(responseXMLAsString);
    assertThat(xmlPath.get("ValidationResponse.errors.error.field"), contains("id","amount"));

OR 要么

RestAssured.given()
.auth()
.preemptive()
.basic(theUsername, thePassword)
.contentType(theContentType)
.header("Accept",theContentType)
.body(theXMLBody)
.when()
.post(theURL)
.then()
.body("ValidationResponse.errors.error.field", contains("id","amount"));

Where contains() is hamcrest matcher 其中contains()是hamcrest匹配器

import static org.hamcrest.collection.IsIterableContainingInOrder.contains;

暂无
暂无

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

相关问题 如何使用XLS转换复制XML节点并粘贴到同一级别 - How to copy an XML node and paste in same level using XLS transformations 重命名同一级别同名标签的xml标签 - Rename xml tag for tag with same name on same level 如何在Java中使用DOM在XML文档中的相同层次结构级别上添加两个或多个相同名称的元素 - How to add two or more elements of the same name at the same hierarchical level in an XML document using DOM in Java 如何使用JDOM以相同的名称编写和获取具有相同名称的所有xml元素 - how to write and get all the xml elements in same level with same name using JDOM 如何在父节点和子节点名称相同的情况下通过JAXB解组XML - How to Unmarshaller XML by JAXB where parent and child node as same name 将相同名称的xml节点添加到arrayList - add same name xml node to a arrayList Castor XML映射与子节点同名 - Castor XML Mapping same name with the child node 在xml文件中,如何根据同一级别节点中的另一个标签获取标签中的数据? - In a xml file,How can I get the data in a label depending on another label in the same level of node? 如果同一XML节点有多个层次,如何遍历JAXB类 - How to Loop through JAXB classes, if there are more than one level of same XML node 如果有许多同名的父节点,如何从xml的父节点中选择所有子节点? - How to select all child nodes from a parent node from xml if there are many parents node with same name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM