从接口自动化测试框架设计到开发(七)--cookie处理

模拟登陆获取cookie

# -*- coding: utf-8 -*-
# @Author: jiujiu
# @Date:   2020-03-09 17:05:13
# @Last Modified time: 2020-03-09 17:37:03
import requests
import json
url = "http://10.1.30.118:3200/provider/user/user/login"
data = {
    'loginName':'15519560000',
    'loginModel':'0',
    'password':'12345678',
    'loginType':'01',
    'timeStamp':'1582616038764'
    }
res = requests.post(url,data).json()
json.dumps(res,sort_keys=True,indent=10)
print(res['data']['token'])

运行结果

 简单的通过登录获取token,再通过添加token get到数据

# -*- coding: utf-8 -*-
# @Author: jiujiu
# @Date:   2020-03-09 17:05:13
# @Last Modified time: 2020-03-11 15:38:42
import requests
import json
def login():
        url = "http://10.1.30.119:9005/user/login"
        data = {
            'loginName': '012345',
            'password': '123456',
            'loginType': '01',
            'loginModel': '0',
            'anti_cache': '1583909753278'
            }
        res = requests.post(url,data=data).json()
            # json.dumps(res,sort_keys=False,indent=1,ensure_ascii=False)
            # token = res['data']['token']
#token = requests.utils.dict_from_cookiejar(token)
        # print(res)
        token = res['data']['token']
        # print(token)
        return token
login()

def get_data():
    print({'token':login()})
    url = "http://10.1.30.119:9005/query/selectDriverInfo?current=1&size=20&total=18&car_type=%E5%A4%A7%E5%9E%8B%E5%AE%A2%E8%BD%A6&sex=&time=&screenFiled=&deal_time_s=&deal_time_e=&anti_cache=1583909895350"
    header = {
    'token':login()
    }
    res = requests.get(url=url,headers=header)
    print(res.text)
if __name__ == '__main__':
    get_data()

运行结果:

 

posted @ 2020-03-12 14:27  大九~  阅读(359)  评论(0编辑  收藏  举报