快捷搜索:  汽车  科技

python接口自动化基础(手把手教python3接口自动化)

python接口自动化基础(手把手教python3接口自动化)import requests代码实现:2、从学校列表查询学校(学校名称: t_school)3、点查询。用fiddler抓包

「第十七章」 Python3 接口项目实战二(WEB项目)

备注:Python3 接口项目实战二承接Python3 接口项目实战一那篇文章的内容

17.5.2测试用例2-从学校列表查询学校

用例步骤:

1、登录教育局招生管理系统

2、从学校列表查询学校(学校名称: t_school)

3、点查询。

python接口自动化基础(手把手教python3接口自动化)(1)

用fiddler抓包

python接口自动化基础(手把手教python3接口自动化)(2)

python接口自动化基础(手把手教python3接口自动化)(3)

python接口自动化基础(手把手教python3接口自动化)(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_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)

运行结果:

python接口自动化基础(手把手教python3接口自动化)(5)

17.5.3 测试用例3-禁用学校

测试用例3:禁用学校。

python接口自动化基础(手把手教python3接口自动化)(6)

通过fiddler抓包。

python接口自动化基础(手把手教python3接口自动化)(7)

python接口自动化基础(手把手教python3接口自动化)(8)

python接口自动化基础(手把手教python3接口自动化)(9)

程序实现:

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)

运行结果:

python接口自动化基础(手把手教python3接口自动化)(10)

python接口自动化基础(手把手教python3接口自动化)(11)

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)

运行结果:

python接口自动化基础(手把手教python3接口自动化)(12)

备注:这里我就不写全部单个的用例了,用例情况类似,一个一个去实现,同时,这里我也暂时不做断言,后续讲单元测试框架再讲框架自动的断言。

猜您喜欢: