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

我正在尝试使用Cucumber和REST Assured在我的Gradle项目中创建自动测试。


以下是XML响应正文:


<ValidationResponse>

    <errors>

        <error>

            <field>id</field>

        </error>


        <error>

            <field>amount</field>

        </error>


    </errors>

</ValidationResponse>

我正在尝试使用下面的REST保证代码来检查以下内容:


“ id”出现在第一个错误字段中

“金额”出现在第二个错误字段中


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"));

第二个字段失败,因为代码只是检查它遇到的第一个“字段”。


有人知道我需要对代码进行哪些更改,以便它也检查第二个“字段”吗?


FFIVE
浏览 176回答 1
1回答

繁华开满天机

使用下面的代码片段来验证响应字段。String responseXMLAsString = "<ValidationResponse>&nbsp; &nbsp; &nbsp;<errors>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<error>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<field>id</field>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</error>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <error>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<field>amount</field>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</error>&nbsp; &nbsp; &nbsp; </errors> </ValidationResponse>";&nbsp; &nbsp; XmlPath xmlPath = new XmlPath(responseXMLAsString);&nbsp; &nbsp; assertThat(xmlPath.get("ValidationResponse.errors.error.field"), contains("id","amount"));或者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"));其中contains()是hamcrest匹配器import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java