栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

多测师拱墅校区肖sir

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

多测师拱墅校区肖sir

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/882936.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号