51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1216|回复: 1

[Python测试开发全栈班] 接口联调,获取用户信息失败

[复制链接]

该用户从未签到

发表于 2019-5-27 11:46:38 | 显示全部楼层 |阅读模式
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:54 | 显示全部楼层
这个是因为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,更新用户信息的时候有,可以继续先往下看看呵
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-4-19 09:01 , Processed in 0.071525 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表