邮递员检查XML响应包括多个元素和值

时间:2019-12-19 11:53:34

标签: javascript xml api postman

以下是API响应的一部分,响应中可能包含大约10个UserGroup块。我需要检查响应是否包含某个UsergroupType(例如6),然后还要检查Users.User.Value是否为某个值,我已经尝试过:

 pm.test("Body includes UserGroupValue 1 ", function () {
    pm.response.to.have.body("<UserGroupValue>1.00</UserGroupValue>");
});

但是因为可能还有另一个具有相同值的元素,所以我不确定这是正确的UserGroup块。我想我需要检查多行并在测试中声明它们,但是我不确定该怎么做,除非有其他方法?预先感谢!

<UserGroup>
    <UserGroupType>1</UserGroupType>
    <UserGroupDescription>Users</UserGroupDescription>
    <UserGroupValue>1.00</UserGroupValue>
    <UserGroupCurrency>USD</UserGroupCurrency>
    <UserGroupISOCurrency>150</UserGroupISOCurrency>
    <UserGroupISOCurrencySymbol>$</UserGroupISOCurrencySymbol>
    <Users>
        <User>
            <UserType>67</UserType>
            <UserDescription>Test</UserDescription>
            <UserValue>1.00</UserValue>
            <UserCurrency>USD</UserCurrency>
            <UserISOCurrency>150</UserISOCurrency>
        </User>
        <User>
            <UserType>15</UserType>
            <UserDescription>Test2</UserDescription>
            <UserValue>1.00</UserValue>
            <UserCurrency>USD</UserCurrency>
            <UserISOCurrency>150</UserISOCurrency>
        </User>
    </Users>
</UserGroup>
<UserGroup>
    <UserGroupType>6</UserGroupType>
    <UserGroupDescription>Users2</UserGroupDescription>
    <UserGroupValue>1.00</UserGroupValue>
    <UserGroupCurrency>USD</UserGroupCurrency>
    <UserGroupISOCurrency>150</UserGroupISOCurrency>
    <UserGroupISOCurrencySymbol>$</UserGroupISOCurrencySymbol>
    <Users>
        <User>
            <UserType>78</UserType>
            <UserDescription>Test</UserDescription>
            <UserValue>1.00</UserValue>
            <UserCurrency>USD</UserCurrency>
            <UserISOCurrency>150</UserISOCurrency>
        </User>
        <User>
            <UserType>15</UserType>
            <UserDescription>Test2</UserDescription>
            <UserValue>1.00</UserValue>
            <UserCurrency>USD</UserCurrency>
            <UserISOCurrency>150</UserISOCurrency>
        </User>
    </Users>
</UserGroup>

1 个答案:

答案 0 :(得分:0)

可以尝试这样的操作,但是就返回XML数据的方式而言,它有其局限性:

let convertedData = xml2Json(pm.response.text())

pm.test("Check UserGroup data", function () {
    pm.expect(convertedData.UserGroup.UserGroupType).to.equal("1");
    pm.expect(convertedData.UserGroup.UserGroupValue).to.equal("1.00");
});

此处提供了有关使用XML响应的更多信息: https://www.youtube.com/watch?v=xC4QQCQDDDk