代码拉取完成,页面将自动刷新
import json
import random
from locust import HttpUser, TaskSet, task, SequentialTaskSet, constant
import uuid
class UserTasks(HttpUser):
wait_time = constant(1)
#执行时最先开始运行on_start方法
def on_start(self):
self.header = {"Content-Type": "application/json", "store-id": "34f05584-7610-421a-a366-b67df6000471","token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlcyI6WyJHVUVTVCJdLCJhZG1pbl9pZCI6IjlkY2Q5ZmI4LTA1MGItNGIzOS1hNWNmLTk0NDQxYzkyNzU3NiIsImFkbWluX3JvbGVzIjpbIkFETUlOOlNVUEVSQURNSU4iXSwiaXNzIjoibGF6eXNob3A6cGVybWlzc2lvbiIsImN1c3RvbWVyX2lkIjoiZThmMDM4OTAtNmVlMC00NzlhLWI1MTItMTVlOWY0NGVhODkwIiwiZXhwIjoxNjcwMTI1Mjg5LCJpYXQiOjE2Mzg1ODkxNjksInNvdXJjZV9pcCI6IjQyLjE4NC41LjExMCJ9.R1NDNpXkjUBLEY-hRylvku5KdLYReLUcOIPWvxxm2Xc","cookie":""}
# 列出需要测试的任务形式一
#根据店铺ID获得支付方式
# @task(1)
def redius(self):
url = '/payment/method/descriptors'
par = {"countryCode":"CN"}
with self.client.get(url, headers=self.header,catch_response=True,params=par) as res:
if res.status_code == 200 and res.text.find("SUCCESS") > -1:
resdata = res.json()
self.ids = resdata["data"]["paymentMethods"]["paymentMethods"][0]["id"]
self.createPaymentTransactionType = resdata["data"]["paymentMethods"]["paymentMethods"][0]["createPaymentTransactionType"]
self.idss = resdata["data"]["paymentMethods"]["paymentMethods"][1]["id"]
self.idsss = resdata["data"]["paymentMethods"]["paymentMethods"][2]["id"]
#发起cod支付
# @task(1)
def checkout(self):
self.redius()
url = '/payment'
body = {
"amount": "666",
"createPaymentTransactionType": self.createPaymentTransactionType,
"currency": "USD",
"merchantTransactionId":"{}".format(uuid.uuid1()),
"paymentMethodId": self.ids
}
with self.client.post(url, catch_response=True, headers=self.header,data=json.dumps(body)) as res:
if res.status_code == 200 and res.text.find("SUCCESS") > -1:
#res.json 是帮你把json字符串转为了字典,res.text 是原始的json字符串
resdata = res.json()
print(resdata)
else:
res.failure("失败")
print("失败")
#发起wordpay支付
#@task(1)
def wordpay(self):
self.redius()
url = '/payment'
body = {
"amount": 5.99,
"currency": "USD",
"encryptedData":"eyJhbGciOiJSU0ExXzUiLCJlbmMiOiJBMjU2R0NNIiwia2lkIjoiMSIsImNvbS53b3JsZHBheS5hcGlWZXJzaW9uIjoiMS4wIiwiY29tLndvcmxkcGF5LmxpYlZlcnNpb24iOiIxLjAuMiIsImNvbS53b3JsZHBheS5jaGFubmVsIjoiamF2YXNjcmlwdCJ9.LTp0wyeegyv4WFyqB4qeT5TbwuS90NQy-J8ubWtVBguHcnhXoiO_BWUfkpU75b19KedNvnXppwdwsAHNgydrZUtTQ4Oi9N1cXeUu0Uc_buluTAuFFWRTS07WOSYydORzxZFQqD80jrwNQJtPDsTQ42kW4ANLREaaKOb_hHPPmdsVuRPgsEUuXiJui4a9yAiCCOJ1uTRopYb3CU543KR3H3u0lGlt8l9PTGdvsob8FJ396hF3_BPtSxVRUzTrE2hwv0W5xjzv694o0EXTaOMg94DagfOLv_UHwaSBi7KjYwMW_YJ_G36gc7xUL-3qV0CDxy96XbMaoPxZAC4ZYCsfeA.-5NGbqUWDle21MYQ.lTiBgWM6zG0IQVoPpW8zdPQyFP7LQ5umGXACYkKO6QQo1HzU2dq-QEI1Tn2_bzqo8tUNMhGo7QsBHk9YeWuW0sfpQX9IDU2DfWC1aMDUcimrgG0oC6Ce8BoDCYiwroOzaxHajNCOCe2uag.5DSRe8nSdc6rtN7RQvPfJA",
"merchantTransactionId": "{}".format(uuid.uuid1()),
"paymentMethodId": self.idss,
"properties": {
"billingAddress": {
"address1": "address-1",
"address2": "address-2",
"city": "hrb",
"company": "",
"countryCode": "CN",
"firstName": "ocean",
"lastName": "li",
"phone": "15544444444",
"phoneAreaCode": "+86",
"postalCode": "150000",
"provinceCode": "HL"
},
"description": "WorldPay WPG Payment demo",
}
}
with self.client.post(url, catch_response=True, headers=self.header, data=json.dumps(body)) as res:
if res.status_code == 200 and res.text.find("SUCCESS") > -1:
# res.json 是帮你把json字符串转为了字典,res.text 是原始的json字符串
resdata = res.json()
print(resdata)
else:
print(res.text)
#发起paypel支付
@task(1)
def paypel(self):
self.redius()
url = '/payment'
body = {
"paymentMethodId": self.idsss,
"createPaymentTransactionType": "PURCHASE",
"merchantTransactionId": "{}".format(uuid.uuid1()),
"amount": 3.99,
"currency": "USD",
"properties": {
"cancelUrl": "http://localhost.com/cancel",
"returnUrl": "http://localhost.com/return",
"shippingAddress": {
"address1": "address-1",
"address2": "address-2",
"city": "hrb",
"company": "",
"countryCode": "C2",
"firstName": "ocean",
"lastName": "li",
"phone": "15544444444",
"phoneAreaCode": "+86",
"postalCode": "150000",
"provinceCode": "HL"
}
}
}
with self.client.post(url, catch_response=True, headers=self.header, data=json.dumps(body)) as res:
if res.status_code == 200 and res.text.find("SUCCESS") > -1:
# res.json 是帮你把json字符串转为了字典,res.text 是原始的json字符串
resdata = res.json()
print(resdata)
else:
print(res.text)
if __name__ == "__main__":
import os
os.system("locust -f zhifu.py --host=https://api.dev.lazyshop.com:4430")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。