当前位置:网站首页>Requests the library deployment and common function
Requests the library deployment and common function
2022-08-05 05:03:00 【Si Xiaoyou】
1.Requests常用函数讲解
import json
import jsonpath
import requests
url = "http://www.baidu.com"
res = requests.get(url)
# Print what the response came back with Binary text content
# print(res.content)
# 文本内容
# print(res.text)
# 接口地址
# print(res.url)
# cookie
# print(res.cookies)
# Print the header content
# print(res.headers)
# 打印json
# print(res.json())
# 请求不带参数 请求带参数
# url = 'http://39.98.138.157:5000/api/gettomorrow'
# data = {"city":"1"}
# res = requests.get(url = url,params = data)
# print(res.text,type(res.text))
# print(res.json(),type(res.json()))
# Get the content that comes back in the response res.json good point 数据类型 字典类型
# res.textThe content of the response comes back 字符串的类型
# post请求
# url = "http://39.98.138.157:5000/api/login"
# data={"password": "123456","username": "admin"}
# print(type(data))
# # def post(url, data=None, json=None, **kwargs): 用json
# res = requests.post(url,json=data)
# print(res.json())
# 什么时候用data,什么时候用json呢? 看content-type数据类型,如果是json,要用json传
# 非要用dataCan it be passed,可以哦
# 接口文档 It is required to passcontent-type:application/json
# data A string is required to be passed
# jsonA dictionary type is required to be passed
# url = "http://39.98.138.157:5000/api/login"
# data='{"password": "123456","username": "admin"}'
# print(type(data))
# # def post(url, data=None, json=None, **kwargs): 用json
# # 加个请求头
# header = {"content-type":"application/json"}
# res = requests.post(url,data=data,headers = header)
# print(res.headers)
# print(res.json())
# When the interface documentation does not match the actual interface parameter type,就要改 改json
# The data type is defined when developing the write interface
# dataA string is required to be passed
# Manually convert the dictionary to a string
# url = "http://39.98.138.157:5000/api/login"
# data={"password": "123456","username": "admin"}
# # dictionary to string String to dictionary eval
# data1 = json.dumps(data)
# print(type(data1))
# header = {"content-type":"application/json"}
# res = requests.post(url,data=data1,headers = header)
# # print(res.headers)
# print(res.json())
# json提交 字典类型的数据
# String to dictionary
# url = "http://39.98.138.157:5000/api/login"
# data='{"password": "123456","username": "admin"}'
# # dictionary to string String to dictionary eval
# data1 = json.loads(data)
# print(type(data1))
# header = {"content-type":"application/json"}
# res = requests.post(url,json=data1,headers = header)
# # print(res.headers)
# print(res.json())
# 总结:传json数据
# 1.可以直接用json传参
# 2.如果你要用data传参 The data is changed to string type
# json传字典 data传字符串
# String to dictionary json.loads(data) eval
# dictionary to string json.dumps(data)
# json.load 和 json.dump
# json.load:用于读取文件中json数据
# json.dump:用于写入json文件中
# url = "http://39.98.138.157:5000/api/login"
# data={"password": "123456","username": "admin"}
# f = open('tt.txt','a')
# json.dump(data,f)
# The read file is a string
# f1 = open('tt.txt','r')
# print(f1.read(),type(f1.read()))
f1 = open('tt.txt','r')
ff = json.load(f1)
print(ff,type(ff))
# Login interface test
url = "http://39.98.138.157:5000/api/login"
data='{"password": "123456","username": "admin"}'
# dictionary to string String to dictionary eval
data1 = json.loads(data)
print(type(data1))
header = {
"content-type":"application/json"}
res = requests.post(url,json=data1,headers = header)
# print(res.headers)
print(res.json())
# If this use case returns yessuccess 用例成功 用例失败
# 预期结果
exmsg = 'success'
# 实际结果 msg的字段的值 字典取值
# 其他的方式?Get actual results 正则可以 json取值 json格式的数据
# 字典 数据类型 数据格式 通过json去取值
sjmsg = jsonpath.jsonpath(res.json(),'$.msg')[0]
# 数据,表达式
# sjmsg = res.json()['msg']
print(sjmsg)
# 判断
if exmsg == sjmsg:
print('用例成功')
else:
print('用例失败')
# 相等 啥事没有 不相等 报错 Tips are not friendly 有个友好的提示 用什么方式 There will be a friendly reminder
# try:
# assert exmsg == sjmsg
# print('用例成功')
# except Exception as e:
# print('用例失败')
2.JsonPath讲解
import jsonpath as jsonpath
data={
"store": {
"book": [
{
"category": "新闻学",
"author": "张三",
"title": "图书标题1",
"price": 8.95
},
{
"category": "金融学",
"author": "李四",
"title": "图书标题2",
"price": 12.00
},
{
"category": "计算机",
"author": "王五",
"title": "图书标题3",
"isbn": "0-553-21311-3",
"price": 9.99
},
{
"category": "医学",
"author": "赵六",
"title": "图书标题4",
"price": 22.99
}
],
"phone": {
"color": "red",
"price": 1999.00,
"author": "孙七"
},
"author": "周八",
"price": 1.00
},
"author": "吴九"
}
# # # 找出book的所有author ['张三', '李四', '王五', '赵六']
# jsonpath(数据,表达式)
print(jsonpath.jsonpath(data,'$.store.book[*].author'))
# # # under all nodesauthor As long as the author finds it.. ['吴九', '周八', '张三', '李四', '王五', '赵六', '孙七']
print(jsonpath.jsonpath(data,'$..author'))
# # store下的所有元素
print(jsonpath.jsonpath(data,'$.store'))
# # book的第3个元素
print(jsonpath.jsonpath(data,'$.store.book[2]'))
# # book的前面2个元素 切片 [开始值:结束值 不包含结束值]
print(jsonpath.jsonpath(data,'$.store.book[:2]'))
# # book的最后2个元素
print(jsonpath.jsonpath(data,'$.store.book[-2:]'))
# # book的第1个元素到第4个元素 不包含4的元素
print(jsonpath.jsonpath(data,'$.store.book[:4]'))
# # book中所有带有 isbn 的元素 [?(@.)]is the way to write a filter expression [?(@.isbn)]Filter other to find only the content inside the expression
print(jsonpath.jsonpath(data,'$.store.book[?(@.isbn)]'))
# 语法
# $ The entire root node object
# @ 当前节点
# .或[] 子节点
# * 任意子节点
# .. 任意后代节点
3.接口关联 实现登录 下单流程
import jsonpath
import pytest
import requests
class TestCase:
token = None
# 响应 tokennumber will change 响应回来的tokennumber to be extracted 保存在变量中.放在一个地方,都能拿到token
def test_login(self):
url = 'http://39.98.138.157:5000/api/login'
# 张三 token 2h
data = {
"password": "123456","username": "admin"}
res = requests.post(url,json=data)
print(res.json())
# 提取token 保存在变量中 类变量 其他方式 2.session中去取 3.返回值
sjmsg = jsonpath.jsonpath(res.json(),'$.msg')[0]
TestCase.token = jsonpath.jsonpath(res.json(),'$.token')[0]
assert 'success' == sjmsg
# Note that you need to use this when adding to the shopping cart to place an orderuserid和openid 这个怎么弄
def test_getUserinfo(self):
url = 'http://39.98.138.157:5000/api/getuserinfo'
# 这个token不能写死 when getting logged intoken号
header = {
"token":TestCase.token}
res = requests.get(url,headers = header)
print(res.json())
sjname = jsonpath.jsonpath(res.json(),'$..nikename')[0]
assert '风清扬' == sjname
# 选择商品
def test_shopping(self):
url = 'http://39.98.138.157:5000/api/getproductinfo?productid=8888'
res = requests.get(url)
print(res.json())
sjproductid = jsonpath.jsonpath(res.json(),'$..productid')[0]
assert 8888 == sjproductid
def test_cart(self):
pass
def test_order(self):
pass
if __name__ == '__main__':
pytest.main(['-sv','test_demo3.py'])
边栏推荐
猜你喜欢
随机推荐
基于Web的商城后台管理系统的设计与实现
University Physics---Particle Kinematics
雷克萨斯lm的安全性到底体现在哪里?一起来看看吧
【学习笔记之菜Dog学C】动态内存管理之经典笔试题
NPDP证书含金量高吗?跟PMP相比?
大学物理---质点运动学
Cron(Crontab)--use/tutorial/example
数字孪生技术在电力系统中的应用现状
Redis - 13. Development Specifications
LAB 信号量实现细节
狗仔队:表面编辑多视点图像处理
算法---一和零(Kotlin)
Talk about 20 common problems in data governance
软件管理rpm
虚证、实证如何鉴别?
mutillidae download and installation
uboot enable debug printing information
Flutter learning three-Flutter basic structure and principle
how to measure distance from point to face in creo
UVA10827