最近在写一个车险比价流程的接口自动化,各个接口之间涉及到一些参数的传递与复用,做以下总结:
数据源的产生:基本的车辆信息 险种选择
接口简化可概括为两个方面:
参数的传递选择:XML文件
选择整个流程用到的所有参数作为key生成xml文件,接口请求的时候根据所需要的参数选择合适的value;
生成xml
host = config.get_data('mysql').get('host')
port = config.get_data('mysql').get('port')
user = config.get_data('mysql').get('user')
passwd = config.get_data('mysql').get('passwd')
db = config.get_data('mysql').get('db')
helper = MysqlHelper(host=host,port=port,db= db,user=user,passwd=passwd)
dictdata = helper.get_xubao(province=province,city=liben,company=company,date=date)[0]
dictinsure = config.get_data('insured')
dictdata = dict(dictdata, **dictinsure)
impl = minidom.getDOMImplementation()
doc = impl.createDocument(None, None, None)
rootElement = doc.createElement('dates')
for key, value in dictdata.items():
# 创建子元素
childElement = doc.createElement('date')
# 为子元素添加id属性
childElement.setAttribute('name', str(key))
childElement.setAttribute('value', str(value))
# 将子元素追加到根元素中
rootElement.appendChild(childElement)
# 将拼接好的根元素追加到dom对象
doc.appendChild(rootElement)
# 打开test.xml文件 准备写入
filename = os.path.join(Path().get_data_path(),filename)
f = open(filename, 'w', encoding='UTF-8')
# 清空数据
f.seek(0)
f.truncate()
# 写入文件
doc.writexml(f, addindent=' ', newl='\n', encoding='UTF-8')
# 关闭
f.close()
接口参数读与写文件
url, params, method = self.get_duojia_data_info('test_get_car_model_no_info')
for key in params:
params[key] = self.fg.getValueByName(key)
basehttp = self.accesss(url, params, method)
basehttp.set_cookie(self.cookie)
r = basehttp.get_post()
#选择最接近的车型
diff = []
for i in range(len(r.json()['data'])):
diffvalue = abs(
r.json()['data'][i]['price'] / float(10000) - float(self.fg.getValueByName('price')))
diff.append(diffvalue)
index = diff.index(min(diff))
self.fg.setValueByName('price', value=str(r.json()['data'][index]['price'] / float(10000)))
self.fg.setValueByName('seat_num', value=str(r.json()['data'][index]['seat']))
self.fg.setValueByName('selected_car_model_detail',
value=json.dumps(r.json()['data'][index], sort_keys=True, indent=4,
separators=(',', ':'),
ensure_ascii=False))
参数化报价
#报价
@pytest.mark.parametrize("province,city,insurance_company,filename",all_data)
def test_record_price(get_duojia_data_info,province,city,insurance_company,filename):
pass