[英]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保证代码来检查以下内容:
"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.