多测师拱墅校区肖sir_高级金牌讲师_接口测试pytohon+request

一、介绍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()

==================================================

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

金牌j软件测试培训师肖sir

作为一个名资深IT搬运工

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值