我最近开始使用rest-assured来测试一些xmls。这是一个例子:
<activity>
<shopping>
<category type="groceries" label="chocolate" />
<category type="groceries" label="chocolate" />
<category type="present" label="chocolate" />
<container number="eight" color="green" />
</shopping>
<shopping>
<category type="groceries" label="chocolate" />
<category type="groceries" label="chocolate" />
<category type="present" label="chocolate" />
<container number="eight" color="green" />
</shopping>
<shopping>
<category type="groceries" label="chocolate" />
<category type="groceries" label="chocolate" />
<category type="present" label="chocolate" />
<container number="eight" color="green" />
</shopping>
</activity>
我想在这里实现的是,检查每次购物时是否有一个容器编号 =“八”,然后检查每个容器的颜色是否为“绿色”。这是代码:
assertThat(xmlPath.getList("activity.shopping.list().container.findAll{it.@number == 'eight'}.@color", String.class), everyItem(equalTo("green")));
这工作得很好,当我调试它时,我得到了我想要实现的目标:
List <String> lista = xmlPath.getList("activity.shopping.list().container.findAll{it.@number == 'eight'}.@color", String.class);
这是一个三元素列表,其中每个元素都等于“绿色”。
但是当我尝试检查每次购物时是否有一个类别 type = "present",然后检查这些类别中的每一个是否都有 label = "chocolate",我收到一个空列表:
List <String> lista2 = xmlPath.getList("activity.shopping.list().category.findAll{it.@type == 'present'}.@label", String.class);
有什么想法是我遗漏的,或者解决方案应该是什么样子的?非常感谢您的帮助。
最佳答案
这是一个猜测,但也许您需要在 .category
之后添加一个 .list()
?
附言您可能打算调用变量 container
...
关于java - xmlPath 无法从 xml 中获取要列出的特定对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58173955/