「第十七章」 Python3 接口项目实战二(WEB项目)
备注:Python3 接口项目实战二承接Python3 接口项目实战一那篇文章的内容
17.5.2测试用例2-从学校列表查询学校
用例步骤:
1、登录教育局招生管理系统
2、从学校列表查询学校(学校名称: t_school)
3、点查询。
![ccbddf76441c3ec14fbfe319ca3f7ede.png](https://i-blog.csdnimg.cn/blog_migrate/94361ec8771de12993b6dfc351067d7a.jpeg)
用fiddler抓包
![5c5df71d98ee7c0563ff135e7594ce74.png](https://i-blog.csdnimg.cn/blog_migrate/0fafe40ec96317327bd7e99ce42dd6b7.jpeg)
![84b4991da11a7328852454429827afdf.png](https://i-blog.csdnimg.cn/blog_migrate/e1e22bf6f42766b76d5be3511fd204d7.jpeg)
![2e6b5ba34c60ef9415f0e17d5fdec01f.png](https://i-blog.csdnimg.cn/blog_migrate/ea65d54cba2cac049ba34f7f55e6efee.jpeg)
代码实现:
import requests
import json
from urllib import parse
import requests
# 第一步:登录教育局招生管理系统
url_login="http://127.0.0.1:8090/recruit.students/login/in?account=admin&pwd=660B8D2D5359FF6F94F8D3345698F88C"
#把请求头信息进行处理,去掉一些没用的,保留一些有用头信息
headers = {
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
"Referer": "http://127.0.0.1:8090/recruit.students/login/view",
}
# 发送get请求
response = requests.get(url_login ,headers = headers)
# 第一步:登录教育局招生管理系统
url_login="http://127.0.0.1:8090/recruit.students/login/in?account=admin&pwd=660B8D2D5359FF6F94F8D3345698F88C"
#把请求头信息进行处理,去掉一些没用的,保留一些有用头信息·
headers1 = {
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
"Referer": "http://127.0.0.1:8090/recruit.students/login/view",
}
# 发送get请求
r1 = requests.get(url_login,headers = headers1)
#print(r1.text)
# 查询学校
url_find_school="http://127.0.0.1:8090/recruit.students/school/manage/schoolInfoList"
#把请求头信息进行处理,去掉一些没用的,保留一些有用头信息·
headers2 = {""
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:62.0) Gecko/20100101 Firefox/62.0",
"Accept": "application/json, text/javascript, */*; q=0.01",
"Referer": "http://127.0.0.1:8090/recruit.students/school/manage/index",
"X-Requested-With": "XMLHttpRequest",
"Cookie": "JSESSIONID=09CD90A3357DEBD4F3B0F2CF3B387DCA",
"Connection": "keep-alive",
}
formdata = {
"schoolName":"t_school",
"schoolType":"0",
"page":"1",
"pageSize":"15",
}
# 通过urlencode()转码
postdata = parse.urlencode(formdata)
#打印转码后的数据
print(postdata)
# 创建session对象,可以保存登录Cookie值。
ssion = requests.session()
# 发送附带用户名和密码的请求,并获取登录后的Cookie值,保存在ssion里。
r2 = ssion.post(url_find_school,headers = headers2,data=postdata)
html = r2.text
print(html)
# 查看响应码
print(r2.status_code)
运行结果:
![6a59ce67486e971edad9771073121845.png](https://i-blog.csdnimg.cn/blog_migrate/3f7b1c50a04dc87d8433ed6ebf877a8f.jpeg)
17.5.3 测试用例3-禁用学校
测试用例3:禁用学校。
![98ebce90881debef02d9cfa6bdf0d908.png](https://i-blog.csdnimg.cn/blog_migrate/65f6bc94d5e6f5a724dd0ca01ad398af.jpeg)
通过fiddler抓包。
![7a9bd6d195caaa42e9b2b4180f47f648.png](https://i-blog.csdnimg.cn/blog_migrate/8ef44f96ab629b6ac316bb75ae41ee9c.jpeg)
![552c759c489f6064b1ed4c4a1e5c8d7a.png](https://i-blog.csdnimg.cn/blog_migrate/117ce559ccc8888286d1de5e6746f074.jpeg)
![876bfbc25e9ab092ab3b39a48b87533e.png](https://i-blog.csdnimg.cn/blog_migrate/0937c29d93a55a39bf26b4215c3fbb9f.jpeg)
程序实现:
import requests
import json
from urllib import parse
import requests
# 第一步:登录教育局招生管理系统
url_login="http://127.0.0.1:8090/recruit.students/login/in?account=admin&pwd=660B8D2D5359FF6F94F8D3345698F88C"
#把请求头信息进行处理,去掉一些没用的,保留一些有用头信息
headers = {
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99
Safari/537.36",
"Referer": "http://127.0.0.1:8090/recruit.students/login/view",
}
# 发送get请求
response = requests.get(url_login ,headers = headers)
# 第一步:登录教育局招生管理系统
url_login="http://127.0.0.1:8090/recruit.students/login/in?account=admin&pwd=660B8D2D5359FF6F94F8D3345698F88C"
#把请求头信息进行处理,去掉一些没用的,保留一些有用头信息·
headers1 = {
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99
Safari/537.36",
"Referer": "http://127.0.0.1:8090/recruit.students/login/view",
}
# 发送get请求
r1 = requests.get(url_login,headers = headers1)
#print(r1.text)
# 查询学校
url_DisableSchool="http://127.0.0.1:8090/recruit.students/school/manage/enableOrDisableSchool"
#把请求头信息进行处理,去掉一些没用的,保留一些有用头信息·
headers2 = {""
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:62.0) Gecko/20100101 Firefox/62.0",
"Referer": "http://127.0.0.1:8090/recruit.students/school/manage/index",
"Content-Type": "application/json",
"X-Requested-With": "XMLHttpRequest",
"Content-Length": "46",
"Cookie": "JSESSIONID=09CD90A3357DEBD4F3B0F2CF3B387DCA",
"Connection": "keep-alive",
}
# 接口的数据类型是json格式
formdata = {"id":"820890","disable":0,"schoolId":"251"}
# 通过urlencode()转码
postdata = parse.urlencode(formdata)
#打印转码后的数据
print(postdata)
# 创建session对象,可以保存登录Cookie值。
ssion = requests.session()
# 发送附带用户名和密码的请求,并获取登录后的Cookie值,保存在ssion里。
r2 = ssion.post(url_DisableSchool,headers = headers2,data=postdata)
html = r2.text
print(html)
# 查看响应码
print(r2.status_code)
运行结果:
![830b1d87500e5bf489569afb9e41b1c6.png](https://i-blog.csdnimg.cn/blog_migrate/070af95a4432e1867e6722fdfb1ea9ed.jpeg)
![cc09c63d276a826507daef615b65b986.png](https://i-blog.csdnimg.cn/blog_migrate/bf03d5eec4aaedf0d9d5621de08e7f5a.jpeg)
17.5.4测试用例4-启用学校
程序实现:
import requests
import json
from urllib import parse
import requests
# 第一步:登录教育局招生管理系统
url_login="http://127.0.0.1:8090/recruit.students/login/in?account=admin&pwd=660B8D2D5359FF6F94F8D3345698F88C"
#把请求头信息进行处理,去掉一些没用的,保留一些有用头信息
headers = {
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
"Referer": "http://127.0.0.1:8090/recruit.students/login/view",
}
# 发送get请求
response = requests.get(url_login ,headers = headers)
# 第一步:登录教育局招生管理系统
url_login="http://127.0.0.1:8090/recruit.students/login/in?account=admin&pwd=660B8D2D5359FF6F94F8D3345698F88C"
#把请求头信息进行处理,去掉一些没用的,保留一些有用头信息·
headers1 = {
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
"Referer": "http://127.0.0.1:8090/recruit.students/login/view",
}
# 发送get请求
r1 = requests.get(url_login,headers = headers1)
#print(r1.text)
# 启用学校
url_DisableSchool="http://127.0.0.1:8090/recruit.students/school/manage/enableOrDisableSchool"
#把请求头信息进行处理,去掉一些没用的,保留一些有用头信息·
headers2 = {""
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:62.0) Gecko/20100101 Firefox/62.0",
"Referer": "http://127.0.0.1:8090/recruit.students/school/manage/index",
"Content-Type": "application/json",
"X-Requested-With": "XMLHttpRequest",
"Cookie": "JSESSIONID=365D9E30F67F88F7301B32AA83C14011",
"Connection": "keep-alive",
}
# 接口的数据类型是json格式
formdata = {"id":"820890","disable":1,"schoolId":"251"}
# 通过urlencode()转码
postdata = parse.urlencode(formdata)
#打印转码后的数据
print(postdata)
# 创建session对象,可以保存登录Cookie值。
ssion = requests.session()
# 发送附带用户名和密码的请求,并获取登录后的Cookie值,保存在ssion里。
r2 = ssion.post(url_DisableSchool,headers = headers2,data=postdata)
html = r2.text
print(html)
# 查看响应码
print(r2.status_code)
运行结果:
![1b855924c81ead1d54c8d203af7a5367.png](https://i-blog.csdnimg.cn/blog_migrate/865872c75f62b0b0bd046fcb68cce25b.jpeg)
备注:这里我就不写全部单个的用例了,用例情况类似,一个一个去实现,同时,这里我也暂时不做断言,后续讲单元测试框架再讲框架自动的断言。