执行以 XML 格式返回的 GET 请求。我将其转换为 JSON,如下所述。我正在寻找解析数组并将数组设置为环境。数组中每个特定对象的变量。
如何通过 Tuple.Answer 实现循环以从响应中获取每个对象的所有突出显示值?使用当前的测试脚本,我在 ActionIDs env 变量中得到以下信息:[“1965555”,“1965555”,“1965555”,“1965555”,“1965555”,“1965555”]
我要检索的数据路径:
BESAPI.Query.Result.Tuple.Answer[0]._
这是我的动作脚本:
var jsonData = xml2Json(responseBody);
var ActionIDs = ;
.each(jsonData.BESAPI.Query.Result.Tuple[0].Answer[0]., () => {
ActionIDs.push(jsonData.BESAPI.Query.Result.Tuple[1].Answer[0]._)
})
pm.environment.set(“ActionID”, ActionIDs.shift());
pm.environment.set(“ActionIDs”, JSON.stringify(ActionIDs));
pm.test(“Status code is 200”, function() {
console.log(pm.response.code);
pm.response.to.have.status(200);
});
回复:
<?xml version="1.0" encoding="UTF-8"?>
<BESAPI xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="BES.xsd">
<Query Resource="(id of it, name of it, state of it) of bes actions">
<Result>
<Tuple>
<Answer type="integer">1965354</Answer>
<Answer type="string">Test Policy 2: Test Policy Azure Test: (1/4)</Answer>
<Answer type="string">Open</Answer>
</Tuple>
<Tuple>
<Answer type="integer">1965555</Answer>
<Answer type="string">Test Policy 2: Test Policy Azure Test: (2/4)</Answer>
<Answer type="string">Open</Answer>
</Tuple>
<Tuple>
<Answer type="integer">1965761</Answer>
<Answer type="string">Test Policy 2: Test Policy Azure Test: (3/4)</Answer>
<Answer type="string">Open</Answer>
</Tuple>
<Tuple>
<Answer type="integer">1965962</Answer>
<Answer type="string">Test Policy 2: Test Policy Azure Test: (4/4)</Answer>
<Answer type="string">Open</Answer>
</Tuple>
<Tuple>
<Answer type="integer">1967153</Answer>
<Answer type="string">Image: Windows Images: (1/6)</Answer>
<Answer type="string">Open</Answer>
</Tuple>
<Tuple>
<Answer type="integer">1967354</Answer>
<Answer type="string">Image: Windows Images: (2/6)</Answer>
<Answer type="string">Open</Answer>
</Tuple>
<Tuple>
<Answer type="integer">1967555</Answer>
<Answer type="string">Image: Windows Images: (3/6)</Answer>
<Answer type="string">Open</Answer>
</Tuple>
<Tuple>
<Answer type="integer">1967756</Answer>
<Answer type="string">Image: Windows Images: (4/6)</Answer>
<Answer type="string">Open</Answer>
</Tuple>
<Tuple>
<Answer type="integer">1967957</Answer>
<Answer type="string">Image: Windows Images: (5/6)</Answer>
<Answer type="string">Open</Answer>
</Tuple>
<Tuple>
<Answer type="integer">1968158</Answer>
<Answer type="string">Image: Windows Images: (6/6)</Answer>
<Answer type="string">Open</Answer>
</Tuple>
</Result>
<Evaluation>
<Time>8.844ms</Time>
<Plurality>Plural</Plurality>
</Evaluation>
</Query>
</BESAPI>