阅读量:0
软件测试经理工作日常随记【8】-UI自动化_加密接口的传输
工具类
#utils_api.py class RequestUtils: @classmethod def send_request_splicing(cls, dicts, url): # 对应请求的入参及请求的函数 Logger.logger_in().info('-----------------{}接口开始执行-----------------'.format(url)) print('-----------------{}接口开始执行-----------------'.format(url)) def parse_url(data: dict): item = data.items() urls = "?" for i in item: (key, value) = i temp_str = key + "=" + value urls = urls + temp_str + "&" urls = urls[:len(urls) - 1] print('请求体为:{}'.format(urls)) Logger.logger_in().info('请求体为:{}'.format(urls)) return urls response = RequestUtils.session.get(url + parse_url(dicts)) Logger.logger_in().info('接口请求成功,响应值为:{}'.format(response.json())) print('接口请求成功,响应值为:{}'.format(response.json())) print(response.json()["data"][0]["A"]) return response
page类
#page_api.py class PageUrl: def __init__(self): self.session = requests.session() def wechat_public_account_api(self, url, params, assert_msg): # 对应请求的断言的函数 response = RequestUtils().send_request_splicing(url, params) print("实际response:" + str(response.json()) + ";预期响应:" + assert_msg) print("实际response.json()[‘data’][0][‘A’]:" + response.json()["data"][0]["A"]) assert response.json()["data"][0]["A"] == assert_msg return response
test类
import os import allure import time import sys import pytest import hashlib import urllib.parse from page_url.page_url import PageUrl from utils_app import DbMysql class TestUrl: def test_003_wechat_public(self): A = "valueA" B = "valueB" json1 = '{"key":"value"}' json = urllib.parse.quote(json1) # 用于对URL中的非ASCII字符或特殊字符进行编码,以便它们转为URL的一部分,比如查询参数。其中json串需转化为字符串 md5 = hashlib.md5() # 创建MD5哈希对象 sign_str = "A=" + A + "&B=" + B + "&json=" + json1 sign_bytes_utf8 = sign_str.encode() # 将字符串 sign_str 编码为字节串 md5.update(sign_bytes_utf8) # 使用update方法将字节串数据添加到哈希计算 sign_md5 = md5.hexdigest() # 使用hexdigest方法来获取当前哈希值的十六进制字符串表示。这个字符串就是sign_str的MD5哈希值 dicts = {'A': A, 'B': B, '_json': _json, 'sign': sign_md5} url = "http://***" time.sleep(2) self.page_url.wechat_public_account_api(dicts, url, RequestUtils.test_number2) time.sleep(0.3)