首页 > 解决方案 > xmlPath 无法从 xml 中获取要列出的特定对象

问题描述

我最近开始使用 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);

我缺少什么或解决方案应该是什么样子的任何想法?非常感谢您的帮助。

标签: javaxmlrest-assured

解决方案


这是一个猜测,但也许你需要一个.list()after .category

PS您可能打算调用变量container...


推荐阅读