我正在尝试从SOAP API中提取的XML中提取SessionId。
我已经阅读过Postman文档(已多次),但它对实现我的目标并不是最有帮助的。
在一些博客中建议将XML转换为JSON,然后从那里挑选令牌及其值,但这也无济于事。
我在测试中使用了以下内容:
var jsonObject = xml2Json(responseBody);
postman.setGlobalVariable("Session_Id", jsonObject.SessionID);
上面创建了变量“Session_Id”,但实际上没有给它赋值。我很难过。
我肯定是从API中检索数据,并且可以在Postman的“身体”响应中查看。
答案 0 :(得分:19)
要使用Postman从XML中提取变量,首先使用xml2Json转换器方法将XML转换为JSON:
var responseJson = xml2Json(responseBody);
(其中“responseBody”是你的xml主体。) 然后使用console.log方法输出您的JSON数据,如下所示:
console.log(responseJson);
请务必在Enabling Chrome Dev Tools in Postman
上遵循本教程在Test Runner中,运行测试,然后右键单击并在Runner中的任何位置“检查”。 Chrome的Dev Tools启动后,选择“控制台”选项卡。展开“对象”部分。
然后向下钻取(展开),直到您看到需要其数据的属性。 此后,通过将每个向下钻取级别附加到所需参数来设置变量:
postman.setGlobalVariable("Session_Id", responseJson.UserSessionToken.SessionID);
在这种情况下,“responseJson”是对象,“UserSessionToken”是深入分析中的下一个级别,而SessionId是我需要的参数。
答案 1 :(得分:7)
自Postman v7.15.0起,已接受的答案对我不起作用(在更新之前曾经如此)。您必须将路径段放在方括号中。
例如,在以下XML响应中:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<QuotesPrivateResponse>
<lease-service>
<duration-in-months>24</duration-in-months>
</lease-service>
</QuotesPrivateResponse>
检索值月持续时间:
var response = xml2Json(responseBody);
var duration = response["QuotesPrivateResponse"]["lease-service"]["duration-in-months"];
pm.environment.set("duration", duration);
答案 2 :(得分:3)
邮递员v7.20.1
我想添加我的答案,因为上面有一些细节需要我花点时间解决:
这是我要分析的XML响应的第一行:
------=_Part_694527_1470506002.1584708814081
Content-Type: application/xop+xml;charset=UTF-8;type="text/xml"
Content-Transfer-Encoding: 8bit
Content-ID:
<e3bd82ac-d88f-49d4-8088-e07ff1c8d407>
<?xml version="1.0" encoding="UTF-8" ?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header/>
<env:Body>
<ns2:GenericResponse xmlns:ns2="http://XXXXXXXX">
<ns2:Service IdcService="XXXXXXXX">
<ns2:Document>
<ns2:Field name="XXXXXXXX:isSetDefault">1</ns2:Field>
<ns2:Field name="XXXXXXXX">CHECKIN_UNIVERSAL</ns2:Field>
在我发现它是一个多部分之后,我最终完成了这个邮递员测试:
var response = pm.response.text();
var responseBody = response.substr(response.indexOf('<env:'));
pm.test("The body of the response is a valid XML", function () {
pm.expect(xml2Json(responseBody)).to.exist;
});
pm.test("UCM file upload checkin succesfull", function(){
var responseJson = xml2Json(responseBody);
var JsonFields = (responseJson['env:Envelope']['env:Body']['ns2:GenericResponse']['ns2:Service']['ns2:Document']['ns2:Field']);
JsonFields.forEach( field => {
if (field.name == 'StatusMessage'){
console.log("Field = " + field.name);
pm.expect(field.text().to.include("Successfully checked in"));
}
});
});