简单的验证码登录接口实例,用于初步窥探接口自动化测试,短信验证码与语音验证码能够成功,每一步都增加了必要的print信息,在控制台可以进行校验。
代码为实际某应用登录案例,实际情况以接口信息改变相应参数,主要注意通过post方法得到的信息需要转格式。
import requests
import random
def message(type):
#获取验证码信息
# global codeID #声明全局变量
prelist = ["130", "131", "132", "133", "134", "135", "136", "137", "138", "139",
"147", "150", "151", "152", "153", "155", "156", "157", "158", "159",
"186", "187", "188", "189"]
phone_num0 = random.choice(prelist) + "".join(random.choice("0123456789") for i in range(8))
phone = {
"nationCode": "86",
"phone": phone_num0
}
if type == 2:
'''发送短信验证码'''
re = requests.post("http://test-app.rolormd.com/Login/User/SendPhoneCode", data=phone)
assert "success" in re.text
print("短信验证码发送成功: " + phone_num0,"\n",re.json())
elif type == 3:
'''发送语音验证码'''
re = requests.post("http://test-app.rolormd.com/Login/User/SendVoiceCode",data=phone)
assert "success" in re.text
print("语音验证码发送成功: " + phone_num0,"\n",re.json())
elif type == 1:
'''获取图形验证码'''
re = requests.get("http://test-app.rolormd.com/Login/User/GetCodeImage")
assert "success" in re.text
print("获取成功: " + phone_num0,"\n",re.json())
codeid = re.json()
codeID = codeid["response"]["codeID"]
return codeID
def check_message(codeID,code='000000',type='2'):
#验证码校验
message = {
"codeID": codeID,
"code": code,
"type": type
}
print(message)
re = requests.post("http://test-app.rolormd.com/Login/User/ValidateCode",data=message)
print(re.text)
if "success" in re.text:
print("验证码正确")
else:
print("您输入的验证码有误,请重新输入")
if __name__=='__main__':
codeID=message(3)
check_message(codeID)