【问题标题】:Postman check XML response includes multiple elements and valuesPostman 检查 XML 响应包含多个元素和值
【发布时间】:2019-12-19 21:17:15
【问题描述】:

以下是来自 API 的响应的一部分,响应中可能有大约 10 个用户组块。我需要检查响应是否包含某个 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>

【问题讨论】:

    标签: javascript xml api postman


    【解决方案1】:

    可以尝试这样的事情,但它有其局限性,就返回 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

    【讨论】:

    • 感谢您的回复。我得到检查用户组数据 | TypeError:无法读取未定义的属性“UserGroupType”
    • 是否需要提供 UserGroupType 的完整路径?
    • 你能用你尝试过的东西更新/编辑原始问题并扩展任何细节吗?通过 cmets 回答是不同的 :)
    • 好的,抱歉 Danny,我收到错误消息:“检查用户组数据 | TypeError:无法读取未定义的属性 'UserGroupType'”我不确定是否因为我需要添加该元素的完整路径,所以我现在添加了完整路径,例如 1 所以我的测试现在显示为:pm.expect(convertedData.x.y.z.UserGroup.UserGroupType)。 to.equal("1");现在我得到错误:检查用户组数据 | AssertionError: 预期未定义等于 1
    • 嘿,我从来没有使用过console.log(),但是在使用它之后我可以看到我需要在哪里,现在我的测试正在进行中,所以我要竖起大拇指!!!我需要使用一个数组,因为这些块具有相同的元素名称,感谢您对这个 Danny 的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    相关资源
    最近更新 更多