背景:小编最近写了一个接口自动化的步骤,主要分为以下几个部分
1.随机账号登录微信商城
2.提交订单 生成订单单号
3.订单进行余额支付或者在线支付
4.后台订单分拣
对于后台订单分拣,小编发现,他只是传了一个Id就对商品进行分拣了,然后小编一脸懵逼,这我怎么知道我刚才下的订单,每个商品的这个id是多少呢?经过思考小编觉得,应该是数据库存储了这个唯一的标识的Id(也就是商品订单表中的储存id),而如何获取这个id,只有根据提交订单时接口返回的订单号,然后根据这个订单号,在数据库中的订单表进行查询到Order_id,然后根据order_id在订单商品表中在查询商品对应关系的储存id(也就是分拣打印传递的哪个id,当然这里如果一个订单下了多个商品的话,查询的结果是多个商品储存标识id的,当然这里就靠自己对list以及元组的操作能力了,因为每次你只能一个id的对后台进行提交,所以我们还需要使用到循环
分析到了这里,所以在代码中,我们必须对数据库进行查询,在将这个查询的结果作为参数,也就会request的请求参数,从而才能让这个接口自动化流程能够跑通。
操作数据库中的代码如下:
#ecoding=utf8 import pymysql #创建连接 conn = pymysql.connect(host='填自己的', port=3306, user='supertest', passwd='SuperTest666_', db='vip_supertest', charset='utf8') #创建游标 cursor = conn.cursor() # 执行SQL,并返回收影响行数 dd='DD18062600008' #params=dd effect_row = cursor.execute("SELECT id FROM tb_order_commodity WHERE order_id=(SELECT id FROM tb_order WHERE order_no=\'%s\')"%(dd)) #print(cursor.fetchall()) a=cursor.fetchall() print(a[0][0]) print(a[1][0]) print(a[2][0]) print(a[3][0]) print(a[4][0]) print(a[5][0]) # 关闭cursor对象 cursor.close() #关闭数据库链接 # conn.close()
总结;
1.在这里,一定要注意sql的参数化,如果多个的时候 可以用使用params=[value,vlaue]
2.需要替代的值 需要使用%s来进行代替,并且用引号扩起来,'在数据库查询中 需要在这里用\进行转义
3.另外参数跟sql需要用%进行隔开
接口完成的自动化代码(在这里曾经多次操作数据库 也就是为了查询数据库得到的结果 作为参数输入 或者作为输出日志)
关键代码(当然数据库跟host我已经改掉了 所以有坏想法的人 你懂得)
#coding=utf8 import pymysql import sys reload(sys) sys.setdefaultencoding('utf8') import requests import json import random import datetime from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) #config host="填你自己的" loginuser1={"username":"15112345678","password":"1"} loginuser2={"username":"15112345679","password":"1"} loginuser3={"username":"15112345670","password":"1"} loginuser4={"username":"13874822433","password":"1"} loginuser5={"username":"13874222438","password":"1"} loginuser6={"username":"16666666666","password":"1"} loginuser7={"username":"18502020505","password":"1"} loginuser8={"username":"15806060202","password":"1"} loginuser9={"username":"17688888888","password":"1"} loginuser10={"username":"17645674567","password":"1"} loginuser11={"username":"13548753520","password":"123456"} loginuser12={"username":"12378945611","password":"1"} #设置发货日期 默认为后二天 需要修改可以更改days today = datetime.date.today() day = today + datetime.timedelta(days=2) #随机选择登录user loginuser=random.choice([loginuser1,loginuser2,loginuser3,loginuser4,loginuser5,loginuser6,loginuser7,loginuser8,loginuser9,loginuser10,loginuser11,loginuser12]) #登录 r=requests.session() login = r.post(host+"/commonApi/wapData/checkLogin",data=loginuser,verify=False) #判断是否登录成功 if((login.json()["status"]) == 1): print("运行日志:账户"+loginuser["username"]+"登录成功") else: print("运行日志:账户登录失败,"+login.json()['message']) #微信商城提交订单 orderdata={'commodity_list':'[{"id":"15071","name":"退款专用加工单单品辅助","price":"9.99","notice":"","unit":"瓶","summary":"","unit_sell":"瓶","unit_num":"1.00","parent_id":"15071","is_time_price":"0","is_collected":"N","order_quantity":"0","price_desc":"9.99","show_price":true,"shopping_amount":0,"convert_price":"9.99","convert_price_desc":"9.99","convert_unit":"瓶","logo":["https://sdpsass.b0.upaiyun.com/supertest/upload_pic/com_thumb_20180619112826cfbf98a95b28785a02a22.png"],"amount":1.33,"$$hashKey":"0NL","checked":true,"commodity_id":"15071"},{"id":"15069","name":"退款专用干果不按基础","price":"4.88","notice":"","unit":"斤","summary":"","unit_sell":"斤","unit_num":"1.00","parent_id":"15069","is_time_price":"0","is_collected":"N","order_quantity":"0","price_desc":"4.88","show_price":true,"shopping_amount":0,"convert_price":"4.88","convert_price_desc":"4.88","convert_unit":"斤","logo":["https://sdpsass.b0.upaiyun.com/supertest/upload_pic/com_thumb_20180619112700f88c44cf5b28780444132.png"],"amount":1.11,"$$hashKey":"0NP","checked":true,"commodity_id":"15069"},{"id":"15070","name":"退款专用干果不按基础","price":"48.80","notice":"","unit":"袋","summary":"","unit_sell":"袋","unit_num":"10.00","parent_id":"15069","is_time_price":"0","is_collected":"N","order_quantity":"0","price_desc":"48.80","show_price":true,"shopping_amount":0,"convert_price":"4.88","convert_price_desc":"4.88","convert_unit":"斤","logo":["https://sdpsass.b0.upaiyun.com/supertest/upload_pic/com_thumb_20180619112700f88c44cf5b28780444132.png"],"amount":1.22,"$$hashKey":"0NQ","checked":true,"commodity_id":"15070"},{"id":"15067","name":"退款专用水果按基础","price":"5.66","notice":"","unit":"斤","summary":"","unit_sell":"斤","unit_num":"1.00","parent_id":"15067","is_time_price":"0","is_collected":"N","order_quantity":"1","price_desc":"5.66","show_price":true,"shopping_amount":0,"convert_price":"5.66","convert_price_desc":"5.66","convert_unit":"斤","logo":["https://sdpsass.b0.upaiyun.com/supertest/upload_pic/com_thumb_20180619112606a9873ab95b2877ce525ee.png"],"amount":1.44,"$$hashKey":"0NV","checked":true,"commodity_id":"15067"},{"id":"15068","name":"退款专用水果按基础","price":"56.60","notice":"","unit":"袋","summary":"","unit_sell":"斤","unit_num":"10.00","parent_id":"15067","is_time_price":"0","is_collected":"N","order_quantity":"0","price_desc":"56.60","show_price":true,"shopping_amount":0,"convert_price":"5.66","convert_price_desc":"5.66","convert_unit":"斤","logo":["https://sdpsass.b0.upaiyun.com/supertest/upload_pic/com_thumb_20180619112606a9873ab95b2877ce525ee.png"],"amount":1.55,"$$hashKey":"0NW","checked":true,"commodity_id":"15068"},{"id":"15066","name":"退款专用大米单品","price":"8.88","notice":"","unit":"斤","summary":"描述啊","unit_sell":"斤","unit_num":"1.00","parent_id":"15066","is_time_price":"0","is_collected":"N","order_quantity":"1","price_desc":"8.88","show_price":true,"shopping_amount":0,"convert_price":"8.88","convert_price_desc":"8.88","convert_unit":"斤","logo":["https://sdpsass.b0.upaiyun.com/supertest/upload_pic/com_thumb_20180619112518820d69275b28779ec78ad.png"],"amount":1.66,"$$hashKey":"0O1","checked":true,"commodity_id":"15066"}]', 'op_source':'7',#提交方式7是小程序 2是微信支付 'op_action':'1', 'delivery_date':day } creatorder=r.post(host+"/commonApi/wapData/createOrder",data=orderdata,verify=False) #orderjson=json.dumps(creatorder.json(),ensure_ascii=False,indent=4) #判断是否提交订单成功 if((creatorder.json()["status"]) == 1): print("运行日志:" + creatorder.json()["message"] +" 发货日期为"+str(day)+ ",订单号为" + creatorder.json()["data"]["order_no"]+",下单总价为:"+str(creatorder.json()["data"]["price"])+"元") else: print("运行日志:提交订单失败," + creatorder.json()["message"]) #微信商城订单货到付款方式 payorder={"order_type":"=0", "order_no":creatorder.json()["data"]["order_no"]} payorder=r.post(host+"/commonApi/pay/index",data=payorder,verify=False) #判断是否支付成功 if((payorder.json()["status"])==1): print("运行日志:订单支付"+payorder.json()["message"]) else: print("运行日志:订单支付失败,"+payorder.json()["message"]) #连接数据库,获取分拣id #创建连接 conn = pymysql.connect(host='填你自己的', port=3306, user='supertest', passwd='SuperTest666_', db='vip_supertest', charset='utf8') #创建游标 cursor = conn.cursor() # 执行SQL,并返回收影响行数 dd=str(creatorder.json()["data"]["order_no"]) #user=loginuser["username"] #params=dd effect_row = cursor.execute("SELECT id,commodity_id FROM tb_order_commodity WHERE order_id=(SELECT id FROM tb_order WHERE order_no=\'%s\')"%(dd)) #effect_row = cursor.execute("SELECT id FROM tb_user WHERE tel=\'%s\')"%(user)) #print(cursor.fetchall()) id_commodity_id=cursor.fetchall() # 关闭cursor对象 cursor.close() #关闭数据库链接 # conn.close() #分拣 for i in range(6): # 连接数据库,根据commodity_id查询出商品名称和商品单位 conn = pymysql.connect(host='填你自己的', port=3306, user='supertest', passwd='SuperTest666_', db='vip_supertest', charset='utf8') # 创建游标 cursor = conn.cursor() # 执行SQL,并返回收影响行数 commodity_id = str(id_commodity_id[i][1]) effect_row = cursor.execute( "SELECT NAME,unit FROM tb_commodity WHERE id=\'%s\'"% commodity_id) name_unit=cursor.fetchall() # 关闭cursor对象 cursor.close() # 关闭数据库链接 # conn.close() pickparams={'id':id_commodity_id[i][0], 'weight':random.randint(1, 20)} cookie={'PHPSEGSUPER':'6p09lavid6ap0e1859at872ns2'} saveweight=r.get(host+"/superAdmin/sortSuper/saveWeight",params=pickparams,cookies=cookie,verify=False) print("运行日志:"+"商品名称:"+name_unit[0][0] +",单位("+name_unit[0][1]+")"+",分拣状态:"+saveweight.json()["message"]+",分拣数量为:"+str(pickparams["weight"])) #第二次创建连接 获取客户Id conn = pymysql.connect(host='填你自己的', port=3306, user='supertest', passwd='SuperTest666_', db='vip_supertest', charset='utf8') cursor = conn.cursor() user=str(loginuser["username"]) #根据登录的username(对应数据库的account_tel)查询用户id effect_row = cursor.execute("SELECT id FROM tb_user WHERE account_tel='%s'"%user) userid=cursor.fetchall() # 关闭cursor对象 cursor.close() #关闭数据库链接 # conn.close() #订单发货出库 sendOutByUserparams = {'uid': userid[0][0], 'deliveryDate': day} cookie = {'PHPSEGSUPER': '6p09lavid6ap0e1859at872ns2'} sendOutByUser = r.get(host + "/store/sendOut/sendOutByUser", params=sendOutByUserparams, cookies=cookie, verify=False) print("运行日志:订单号:"+creatorder.json()["data"]["order_no"]+",发货日期,"+str(day)+";发货状态,"+sendOutByUser.json()["message"])