def goods_list(): url = "http://211.103.136.242:8064/categories/115/skus/" data = {"page": "1", "page_size": "10", "ordering": "create_time"} requset = Request() r = requset.get(url, json=data) print(r) return r
def info(): url = "http://211.103.136.242:8064/user/" token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI6Ijk1MjY3MzYzOEBxcS5jb20iLCJleHAiOjE1ODMyMTg3MzksInVzZXJuYW1lIjoicHl0aG9uIiwidXNlcl9pZCI6MX0.7Mh9eeMIhstMNrQQH6qLmwwL0gM_HY5GcsZkvWRsGas" header = {'Authorization': 'JWT ' + token} requset = Request() r = requset.get(url, headers=header) print(r) return r
def run_api(self,url,method,params=None,header=None,cookie=None): reqest = Request() params = json_data(params) if str(method).lower() == "get": res = reqest.get(url, json=params, headers=header, cookies=cookie) elif str(method).lower() == "post": res = reqest.post(url, json=params, headers=header, cookies=cookie) else: log.error("I like you") # print(res) return res
def run_api(url,method,params=None,header=None,cookie=None): # 接口请求 reqest = Request() #验证parrams 是否有内容 params = json_parse(params) if str(method).lower() == "get": res = reqest.get(url, json=params, headers=header, cookies=cookie) elif str(method).lower() == "post": res = reqest.post(url, json=params, headers=header, cookies=cookie) else: log.error("I like you") # print(res) return res
def test_run(self,case): # 3、重构函数内容 data_key = ExcelConfig.DataConfig # run_list第1个用例,用例key获取values url = ConfigYaml().get_conf_url() + case[data_key.url] print(url) case_id = case[data_key.case_id] case_model = case[data_key.case_model] case_name = case[data_key.case_name] pre_exec = case[data_key.pre_exec] method = case[data_key.method] params_type = case[data_key.params_type] params = case[data_key.params] expect_result = case[data_key.expect_result] headers = case[data_key.headers] cookies = case[data_key.cookies] status_code = case[data_key.status_code] db_verify = case[data_key.db_verify] # 2).接口请求 request = Request() # params 转义json # 验证params有没有内容 if len(str(params).strip()) is not 0: params = json.loads(params) # headers 转义json # 验证headers有没有内容 if len(str(headers).strip()) is not 0: headers = json.loads(headers) # cookies 转义json # 验证cookies有没有内容 if len(str(cookies).strip()) is not 0: cookies = json.loads(cookies) # method post/get if str(method).lower() == "get": res = request.get(url, data=params, headers=headers, cookies=cookies) elif str(method).lower() == "post": res = request.post(url, data=params, headers=headers, cookies=cookies) else: res = log.error("错误请求method: %s" % method) print(res) # allure # sheet名称 feature 一级标签 allure.dynamic.feature(sheet_name) # 模块 story 二级标签 allure.dynamic.story(case_model) # 用例ID+接口名称 title allure.dynamic.title(case_id + case_name) # 请求URL 请求类型 期望结果 实际结果描述 desc = "<font color='red'>请求URL: </font> {}<Br/>" \ "<font color='red'>请求类型: </font>{}<Br/>" \ "<font color='red'>期望结果: </font>{}<Br/>" \ "<font color='red'>实际结果: </font>{}".format(url, method, expect_result, res) allure.dynamic.description(desc) # 断言验证 # 状态码,返回结果内容,数据库相关的结果的验证 # 状态码 assert_util = AssertUtil() assert_util.assert_code(int(res["code"]), int(status_code)) # 返回结果内容 assert_util.assert_in_body(str(res["body"]), str(expect_result))
def test_zxx(): # url = "http://mp-meiduo-python.itheima.net/login/" conf_yaml = ConfigYalm() url_path = conf_yaml.get_conf_url() url = url_path + "login/" print(url) # r_get = requests.get(url) # 使用封装的方法 request = Request() r_get = request.get(url) res = r_get.get("body") # 登陆时要传入token 使用正则表达式获取token token = re.findall('csrfmiddlewaretoken\" value=\"(.+?)\"', res) data = {"csrfmiddlewaretoken": token[0], "username": "******", "pwd": "admin", "remembered": "on" } # 登陆成功后禁止重定向 allow_redirects=False # r = requests.post(url, data=data, allow_redirects=False) # allow_redirects = False r = request.post(url, data=data, allow_redirects=False) # 获取登陆成功之后响应头中的cookie信息中的username和sessionid header = r.get("headers").get("Set-Cookie") username = re.findall('username=(.+?);', header) sessionid = re.findall('sessionid=(.+?);', header) cookie = "username="******"; sessionid="+sessionid[0] print("----------") print("登陆:cookie"+cookie) # 个人中心 # url_info = "http://mp-meiduo-python.itheima.net/info/" url_info = url_path + "info/" print(url_info) headers = { "Content-Type": "application/x-www-form-urlencoded", "Cookie": cookie } # r_info = requests.get(url_info, headers=headers) r_info = request.get(url_info, headers=headers) print("个人中心") print(r_info) # 添加购物车 # url_carts = "http://mp-meiduo-python.itheima.net/carts/" url_carts = url_path + "carts/" headers_carts = { "Content-Type": "application/json;charset=UTF-8", "Cookie": cookie } data_carts = {'sku_id': '18', 'count': '1'} # request payload形式传参使用json.dumps(data_carts) # res_carts = requests.post(url_carts, data=json.dumps(data_carts), headers=headers_carts) # print(res_carts.text) res_carts = request.post(url_carts, data=json.dumps(data_carts), headers=headers_carts) print("添加购物车") print(res_carts) # 购物车选择商品 # url_carts_sel = "http://mp-meiduo-python.itheima.net/carts/" url_carts_sel = url_path + "carts/" data_carts_sel = {'count': '10', 'selected': 'ture', 'sku_id': '18'} headers_carts_sel = { "Content-Type": "application/json;charset=utf-8", "Cookie": cookie } # res_carts_sel = requests.put(url_carts_sel, json=data_carts_sel, headers=headers_carts_sel) # print(res_carts_sel.json()) res_carts_sel = request.put(url_carts_sel, json=data_carts_sel, headers=headers_carts_sel) print("购物车选择商品") print(res_carts_sel) # 下订单 # url_orders = "http://mp-meiduo-python.itheima.net/orders/commit/" url_orders = url_path + "orders/commit/" headers_orders = { "Content-Type": "application/json;charset=utf-8", "Cookie": cookie } data_orders = {'address_id': '5', 'pay_method': 2} # res_orders = # requests.post(url_orders, data=json.dumps(data_orders), headers=headers_orders, allow_redirects=False) # print(res_orders.json()) res_orders = request.post(url_orders, data=json.dumps(data_orders), headers=headers_orders, allow_redirects=False) print("下订单") print(res_orders)