一、介绍request库
(1)requests是用python语言编写的简单易用的http库,用来做接口测试的库;
(2)接口测试自动化库有哪些?
requests、urllib 、urllib2、urllib3 httplib 等(最受欢迎的是request)
(3)安装request库
方式一:
dos下pip:
命令:pip install requests
方式二:
在pycharm中的setting
(4)组建一个接口需要哪些参数?
a.URL
b. 当前接口的请求方式 get/post
c、接口请求的类型 (请求头)
d、接口的入参
案例:
import requests
url=“http://cms.duoceshi.cn/cms/manage/loginJump.do”
data={‘userAccount’:‘admin’,‘loginPwd’:123456}
h={‘Content-Type’:‘application/x-www-form-urlencoded’}
jk=requests.post(url=url,data=data,json=h)
print(jk.text) #登录内容
print(jk.headers) #请求头
print(jk.cookies) #cookies
print(jk.status_code) #状态码
print(jk.request) #请求方式
print(jk.url) #打印接口
============================================
二、request实战:
组建接口的三种破功能方式:
第一种:
requests.方法(参数)
案例1:get请求
requests.get(参数)
import requests
url=“http://cms.duoceshi.cn/cms/manage/loginJump.do?userAccount=admin&loginPwd=123456”
h={‘Content-Type’:‘application/x-www-form-urlencoded’}
jk=requests.get(url=url,json=h)
print(jk.text) #登录内容
案例2:post请求
requests.post(参数)
import requests
url=“http://cms.duoceshi.cn/cms/manage/loginJump.do”
data={‘userAccount’:‘admin’,‘loginPwd’:123456}
h={‘Content-Type’:‘application/x-www-form-urlencoded’}
jk=requests.post(url=url,data=data,json=h)
print(jk.text) #登录内容
第二种:
requests.requests(请求方法,参数)
案例1:
requests.requests(get,参数)
import requests
url=“http://cms.duoceshi.cn/cms/manage/loginJump.do?userAccount=admin&loginPwd=123456”
h={‘Content-Type’:‘application/x-www-form-urlencoded’}
jk=requests.request(‘get’,url=url,json=h)
print(jk.text) #登录内容
案例2:
requests.requests(post,参数)
import requests
url=“http://cms.duoceshi.cn/cms/manage/loginJump.do”
d={‘userAccount’:‘admin’,‘loginPwd’:123456}
h={‘Content-Type’:‘application/x-www-form-urlencoded’}
jk=requests.request(‘post’,url=url,data=d,json=h)
print(jk.text) #登录内容
第三种:
requests.Session( ) #使用session保持上下文管理,可以保持会话的状态
案例:
import requests #导入request库
s=requests.Session() #创建s对象来保持上下接口的关联
class Cms_api(object):
def init(self):
pass
def dl(self): #登录接口
url=“http://cms.duoceshi.cn/cms/manage/loginJump.do”
d={‘userAccount’:‘admin’,‘loginPwd’:123456}
h={‘Content-Type’:‘application/x-www-form-urlencoded’}
jk=s.post(url=url,data=d,json=h)
print(jk.text)
def lmcx(self):
url2=“http://cms.duoceshi.cn/cms/manage/findCategoryByPage.do”
d2={‘parentId’:‘’,‘categoryName’:‘’,‘page=1’:“”}
h2 = {‘Content-Type’: ‘application/x-www-form-urlencoded’}
jk2 = s.post( url=url2, data=d2, json=h2)
print(jk2.text)
if name == ‘main’:
d=Cms_api()
d.dl()
d.lmcx()
============================================================
断言:
(1)if 语句断言
A.先讲接口返回值转化成json格式;
B.在通过if语句判断
案例:
import requests #导入request库
s=requests.Session() #创建s对象来保持上下接口的关联
class Cms_api(object):
def init(self):
pass
def dl(self): #登录接口
url=“http://cms.duoceshi.cn/cms/manage/loginJump.do”
d={‘userAccount’:‘admin’,‘loginPwd’:123456}
h={‘Content-Type’:‘application/x-www-form-urlencoded’}
jk=s.post(url=url,data=d,json=h)
print(jk.text)
js=jk.json()
if js[‘msg’]==“登录成功”:
print(‘ok’)
else:
print(‘no’)
def lmcx(self):
url2=“http://cms.duoceshi.cn/cms/manage/findCategoryByPage.do”
d2={‘parentId’:‘’,‘categoryName’:‘’,‘page=1’:“”}
h2 = {‘Content-Type’: ‘application/x-www-form-urlencoded’}
jk2 = s.post( url=url2, data=d2, json=h2)
print(jk2.text)
if name == ‘main’:
d=Cms_api()
d.dl()
d.lmcx()
(2)通过assert 断言
assert也要转化json格式在进行断言
按:
import requests #导入request库
s=requests.Session() #创建s对象来保持上下接口的关联
class Cms_api(object):
def init(self):
pass
def dl(self): #登录接口
url=“http://cms.duoceshi.cn/cms/manage/loginJump.do”
d={‘userAccount’:‘admin’,‘loginPwd’:123456}
h={‘Content-Type’:‘application/x-www-form-urlencoded’}
jk=s.post(url=url,data=d,json=h)
# print(jk.text)
js=jk.json()
assert js[‘msg’]==“登录成功”
print(jk.text)
============================================================
关联接口:
(1)
省份接口和城市接口
案例1:
import requests
import re
class Cms(object):
def init(self):
pass
def sf(self):
surl=“http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportProvince”
h = {‘Content-Type’: ‘application/x-www-form-urlencoded’}
jk=requests.get(url=surl ,json=h)
print(jk.text)
def cs(self):
curl=“http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity”
cdata={‘byProvinceName’:‘浙江’}
ch = {‘Content-Type’: ‘application/x-www-form-urlencoded’}
cjk=requests.post(url=curl,data=cdata,json=ch)
print(cjk.text)
if name == ‘main’:
c=Cms()
c.sf()
c.cs()
(2)省份接口和城市接口关联关系
备注:通过re正则 匹配,findall (.+?)
import requests
import re
class Cms(object):
def init(self):
pass
def sf(self):
surl=“http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportProvince”
h = {‘Content-Type’: ‘application/x-www-form-urlencoded’}
jk=requests.get(url=surl ,json=h)
r=re.findall(“(.+?)”,jk.text)
return r
def cs(self):
s=self.sf()
curl=“http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity”
cdata={‘byProvinceName’[6]}
ch = {‘Content-Type’: ‘application/x-www-form-urlencoded’}
cjk=requests.post(url=curl,data=cdata,json=ch)
print(cjk.text)
if name == ‘main’:
c=Cms()
c.sf()
c.cs()
==================================================
依赖关系:
方法一:使用request.session 方法解决
案例:
mport requests #导入request库
s=requests.Session() #创建s对象来保持上下接口的关联
class Cms_api(object):
def init(self):
pass
def dl(self): #登录接口
url=“http://cms.duoceshi.cn/cms/manage/loginJump.do”
d={‘userAccount’:‘admin’,‘loginPwd’:123456}
h={‘Content-Type’:‘application/x-www-form-urlencoded’}
jk=s.post(url=url,data=d,json=h)
print(jk.text)
def lmcx(self):
url2=“http://cms.duoceshi.cn/cms/manage/findCategoryByPage.do”
d2={‘parentId’:‘’,‘categoryName’:‘’,‘page=1’:“”}
h2 = {‘Content-Type’: ‘application/x-www-form-urlencoded’}
jk2 = s.post( url=url2, data=d2, json=h2)
print(jk2.text)
if name == ‘main’:
d=Cms_api()
d.dl()
d.lmcx()
方法二:应用cookies 方法
import requests #导入request库
s=requests.Session() #创建s对象来保持上下接口的关联
class Cms_api(object):
def init(self):
pass
def dl(self): #登录接口
url=“http://cms.duoceshi.cn/cms/manage/loginJump.do”
d={‘userAccount’:‘admin’,‘loginPwd’:123456}
h={‘Content-Type’:‘application/x-www-form-urlencoded’}
jk=s.post(url=url,data=d,json=h)
# print(jk.text)
print(jk.cookies)
self.c=str(jk.cookies).split(’ ‘)
self.c = str(jk.cookies).split(’ ‘)[1]
print(self.c)
def lmcx(self):
url2=“http://cms.duoceshi.cn/cms/manage/findCategoryByPage.do”
d2={‘parentId’:’‘,‘categoryName’:’',‘page=1’:“”}
h2 = {‘Content-Type’: ‘application/x-www-form-urlencoded’,‘Cookie’:self.c}
jk2 = s.post( url=url2, data=d2, headers=h2)
print(jk2.text)
if name == ‘main’:
d=Cms_api()
d.dl()
d.lmcx()
==================================================



