51Testing软件测试论坛
标题: 接口联调,获取用户信息失败 [打印本页]
作者: bbb327 时间: 2019-5-27 11:46
标题: 接口联调,获取用户信息失败
import requests
class flow_workupdate():
#1.用户注册
def regist(self):
url="http://localhost:8080/jwshoplogin/user/register.do"
useinfo={"username":"樊S2",
"password":"123456",
"phone":"18911271199",
"email":"87878111@qq.com",
"question":"我最爱的食物",
"answer":"奶茶"}
response=requests.post(url,data=useinfo).text
r=response.find("注册成功")
if r>1:
print("用户注册接口测试通过")
else:
print("用户注册接口测试不通过")
# 2.用户登录
def login(self):
url = "http://localhost:8080/jwshoplogin/user/login.do"
useinfo = {"username": "樊S2",
"password": "123456"}
response = requests.post(url, data=useinfo).text
print(response)
r = response.find("登录成功")
if r > 1:
print("登录接口测试通过")
else:
print("登录接口测试不通过")
#3.获取用户信息def infomation(self): url ="http://localhost:8080/jwshoplogin/user/get_information.do" response = requests.post(url).text print(response) r = response.find("data") if r > 1: print("获取用户信息接口测试通过") else: print("获取用户信息接口测试不通过")代码结果:用户注册接口测试不通过
{"status":0,"msg":"登录成功","data":{"id":33,"username":"樊S2","password":null,"email":"87878111@qq.com","phone":"18911271199","question":"我最爱的食物","answer":"奶茶","role":0,"createTime":1558085040000,"updateTime":1558085040000}}
登录接口测试通过
{"status":10,"msg":"未登录,需要强制登录status=10"}
获取用户信息接口测试不通过
问题是:为什么我之前登录了,会显示未登录呢?
在学课程
Python测试开发全栈核心课程 互联网测试工程师必修课
作者: 学掌门网校 时间: 2019-5-27 12:06
这个是因为get_information.do这个用脚本实现,需要传入登录时候自动生成的cookie值。代码可以参考 如下
def login_test(self):
url="http://localhost:8080/jwshoplogin/user/login.do"
userinfo={}
#正确的用户登录数据
userinfo={"username":"润玉2",
"password":"123456"
}
#发送用户登录请求并获取响应结果
response = requests.post(url, data=userinfo)
self.sessionID = dict(response.cookies)['JSESSIONID']
print(self.sessionID)
r=response.text.find("登录成功")
if r>0:
print("用户登录接口测试成功")
else:
print("用户登录接口测试失败")
# 3、获取用户信息
def get_information_test(self):
url = "http://localhost:8080/jwshoplogin/user/get_information.do"
# 发送用户登录请求并获取响应结果
header={"Content-Type":"application/x-www-form-urlencoded"}
session = {"JSESSIONID": self.sessionID}
response = requests.post(url, headers=header, cookies=session).text
print(response)
这个内容在接口框架部分讲update,更新用户信息的时候有,可以继续先往下看看呵
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) |
Powered by Discuz! X3.2 |