Postman接口自動化測試之——數據關聯(正則提取XML響應值)

測試API接口:
http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getDatabaseInfo
請求數據要求參照API規範。

要求:將響應數據中手機號和歸屬地數據賦給全局變量(其他變量也可以)。
在這裏插入圖片描述
響應數據:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
            <getMobileCodeInfoResult>13644556677:黑龍江 綏化 黑龍江移動神州行卡</getMobileCodeInfoResult>
        </getMobileCodeInfoResponse>
    </soap:Body>
</soap:Envelope>

在Tests斷言添加如下代碼:

//獲取xml響應數據
var reg = pm.response.text();
pm.globals.set("xmldata", reg);

//正則提取手機號歸屬地結果
var phone = reg.match(/[\\<]getMobileCodeInfoResult>([\S\s]*?)[\\<][\\/]getMobileCodeInfoResult.*/)[1];
pm.globals.set("phone_address", phone);

//正則提取手機號
var phone = reg.match(/[\\<]getMobileCodeInfoResult>([\S\s]*?):.*/)[1];
pm.globals.set("phone_Num", phone);

在這裏插入圖片描述
執行接口,查看變量。
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章