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

Postman接口測試過程中,我們時常需要將某一接口的返回數據,提取作爲參數,供其他接口使用。這時我們可以通過斷言tests解析響應數據,提取所需字符串。

測試API接口:
https://www.juhe.cn/docs/api/id/39
請求參數要求參照API規範。
要求:將響應數據中sk的“wind_direction”的值賦給全局變量respjson

響應數據:

{
    "resultcode": "200",
    "reason": "successed!",
    "result": {
        "sk": {
            "temp": "22",
            "wind_direction": "西北風",
            "wind_strength": "4級",
            "humidity": "49%",
            "time": "13:55"
        },
        "today": {
            "temperature": "15℃~23℃",
            "weather": "多雲",
            "weather_id": {
                "fa": "01",
                "fb": "01"
            },
            "wind": "北風3-5級",
            "week": "星期五",
            "city": "蘇州",
            "date_y": "2019年10月18日",
            "dressing_index": "舒適",
            "dressing_advice": "建議着長袖T恤、襯衫加單褲等服裝。年老體弱者宜着針織長袖襯衫、馬甲和長褲。",
            "uv_index": "弱",
            "comfort_index": "",
            "wash_index": "較適宜",
            "travel_index": "較適宜",
            "exercise_index": "較適宜",
            "drying_index": ""
        },
        "future": {
            "day_20191018": {
                "temperature": "15℃~23℃",
                "weather": "多雲",
                "weather_id": {
                    "fa": "01",
                    "fb": "01"
                },
                "wind": "北風3-5級",
                "week": "星期五",
                "date": "20191018"
            },
            "day_20191019": {
                "temperature": "14℃~23℃",
                "weather": "晴",
                "weather_id": {
                    "fa": "00",
                    "fb": "00"
                },
                "wind": "北風微風",
                "week": "星期六",
                "date": "20191019"
            },
            "day_20191020": {
                "temperature": "15℃~23℃",
                "weather": "晴轉多雲",
                "weather_id": {
                    "fa": "00",
                    "fb": "01"
                },
                "wind": "東風微風",
                "week": "星期日",
                "date": "20191020"
            },
            "day_20191021": {
                "temperature": "16℃~24℃",
                "weather": "多雲",
                "weather_id": {
                    "fa": "01",
                    "fb": "01"
                },
                "wind": "東北風微風",
                "week": "星期一",
                "date": "20191021"
            },
            "day_20191022": {
                "temperature": "17℃~23℃",
                "weather": "多雲",
                "weather_id": {
                    "fa": "01",
                    "fb": "01"
                },
                "wind": "東風微風",
                "week": "星期二",
                "date": "20191022"
            },
            "day_20191023": {
                "temperature": "14℃~23℃",
                "weather": "晴",
                "weather_id": {
                    "fa": "00",
                    "fb": "00"
                },
                "wind": "北風微風",
                "week": "星期三",
                "date": "20191023"
            },
            "day_20191024": {
                "temperature": "16℃~24℃",
                "weather": "多雲",
                "weather_id": {
                    "fa": "01",
                    "fb": "01"
                },
                "wind": "東北風微風",
                "week": "星期四",
                "date": "20191024"
            }
        }
    },
    "error_code": 0
}

在斷言(tests)中添加提取代碼

//獲取JSON格式的響應數據
var jsonData=JSON.parse(responseBody);

//提取響應數據中sk.wind_direction的值賦給全局變量respjson
pm.globals.set("respjson", jsonData.result.sk.wind_direction);

在這裏插入圖片描述
執行接口,查看變量。
在這裏插入圖片描述
可以將該變量做爲其他接口的請求參數。
在這裏插入圖片描述

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