Javascript 邮递员检查XML响应包含多个元素和值
下面是API响应的一部分,响应中可能有大约10个用户组块。我需要检查响应是否包含某个UsergroupType,例如6,然后还要检查Users.User.Value是否为某个值,我尝试了:Javascript 邮递员检查XML响应包含多个元素和值,javascript,xml,api,postman,Javascript,Xml,Api,Postman,下面是API响应的一部分,响应中可能有大约10个用户组块。我需要检查响应是否包含某个UsergroupType,例如6,然后还要检查Users.User.Value是否为某个值,我尝试了: pm.test("Body includes UserGroupValue 1 ", function () { pm.response.to.have.body("<UserGroupValue>1.00</UserGroupValue>"); }); pm.test(“
pm.test("Body includes UserGroupValue 1 ", function () {
pm.response.to.have.body("<UserGroupValue>1.00</UserGroupValue>");
});
pm.test(“主体包括UserGroupValue 1”,函数(){
pm.response.to.have.body(“1.00”);
});
但是因为可能还有另一个元素具有相同的值,我不能确定它是正确的用户组块。我想我需要检查多行代码,并在测试中声明这些代码,但我不知道该怎么做,除非有其他方法?提前谢谢
<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.
使用者
1
美元
150
$
67
试验
1
美元
150
15
测试2
1
美元
150
6.
用户2
1
美元
150
$
78
试验
1
美元
150
15
测试2
1
美元
150
您可以尝试类似的方法,但它在返回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响应的更多信息:
感谢您的回复。我得到检查用户组数据|类型错误:无法读取未定义的属性“UserGroupType”?我需要提供UserGroupType的完整路径?你能用你尝试过的东西更新/编辑原始问题并展开任何细节吗?请,通过注释回答不同:)好的,抱歉Danny,我得到错误:“检查用户组数据| TypeError:无法读取未定义的属性'UserGroupType'”我不确定是否因为我需要向该元素添加完整路径,所以我现在添加了完整路径,例如1,所以我的测试现在读取:pm.expect(convertedData.x.y.z.UserGroup.UserGroupType).to.equal(“1”);现在我得到了错误:检查用户组数据| AssertionError:预期未定义为等于1嘿,我从未使用过console.log(),但使用它后,我可以看到我需要的位置,现在我的测试正在进行中,所以我竖起大拇指!!!我需要使用数组,因为块具有相同的元素名称,感谢您在这方面的帮助。