multipart/form-data环境准备
需要安装requests-toolbelt库
适用场景:content-type是multipart/form-data
MultipartEncoder中的fields不重复可以用字典,重复就用list
import requests import re from requests_toolbelt import MultipartEncoder from lxml import etree import pymysql # 数据准备 # DELETE from hello_teacher WHERE teacher_name = 'test0002'; import pymysql # 打开数据库连接 db = pymysql.connect(host='47.104.190.48', port=3306, user='root', passwd='root', db='django') # 使用 cursor() 方法创建一个游标对象cur cur = db.cursor() # 使用 execute() 方法执行 SQL 查询 sql_update = "DELETE from hello_teacher WHERE teacher_name = 'test0002';" cur.execute(sql_update) # 一定要提交 db.commit() db.close() url = "http://47.104.190.48:8000/login/" s = requests.session() r = s.get(url) # re 知道前面和后面,取中间值 token = re.findall('name="csrfmiddlewaretoken" value="(.+?)"', r.text) print(token[0]) body = { "csrfmiddlewaretoken": token[0], "username": "test", "password": "test" } r2 = s.post(url, data=body) if "登录成功" in r2.text: print("登录成功!") else: print("密码不对,自己申请账号密码") # 登录成功之后的请求 url3 = "http://47.104.190.48:8000/xadmin/hello/teacher/add/" r_token = s.get(url3) token2 = re.findall('name="csrfmiddlewaretoken" value="(.+?)"', r_token.text) print(token2[0]) body = MultipartEncoder( #通过MultipartEncoder进行转码,放到元祖里面请求 fields=[ ("csrfmiddlewaretoken", token2[0]), ("csrfmiddlewaretoken", token2[0]), ("teacher_name", "test0002"), ("tel", "112211"), ("mail", "1111@qq.com"), ("_save", ""), ] ) r3 = s.post(url3, data=body, headers={'Content-Type': body.content_type}) #body.content_type 自动获取上面body中的content_type属性 # 判断是否成功 # 写一个get请求,人工打开页面,检查列表中最新的数据 。SQL查询也可以 url4 = "http://47.104.190.48:8000/xadmin/hello/teacher/" r4 = s.get(url4) # xpath定位 通过lxml和requests-html都可以定位 x = '//*[@id="changelist-form"]/div[1]/table/tbody/tr[1]/td[2]/a' demo = etree.HTML(r4.text) nodes = demo.xpath(x) print(nodes[0].text) assert "test0002" == nodes[0].text # 断言 # 保证测试数据不干扰,测试前先清理,测试后删除
封装示例
数据库封装
import pymysql # 数据准备 # DELETE from hello_teacher WHERE teacher_name = 'test0002'; def delete_sql(sql_delete): # 打开数据库连接 db = pymysql.connect(host='47.104.190.48', port=3306, user='root', passwd='root', db='django') # 使用 cursor() 方法创建一个游标对象cur cur = db.cursor() # 使用 execute() 方法执行 SQL 查询 cur.execute(sql_delete) # 一定要提交 db.commit() db.close()
添加方法封装
import requests import re from requests_toolbelt import MultipartEncoder from lxml import etree def teacher_add(s): # 登录成功之后的请求 url3 = "http://47.104.190.48:8000/xadmin/hello/teacher/add/" r_token = s.get(url3) token2 = re.findall('name="csrfmiddlewaretoken" value="(.+?)"', r_token.text) print("获取添加页面的token值") print(token2[0]) body = MultipartEncoder( fields=[ #用list存储 ("csrfmiddlewaretoken", token2[0]), ("csrfmiddlewaretoken", token2[0]), ("teacher_name", "test0002"), ("tel", "112211"), ("mail", "1111@qq.com"), ("_save", ""), ] ) r3 = s.post(url3, data=body, headers={'Content-Type': body.content_type}) # 判断是否成功,headers={'Content-Type': body.content_type} 从body中自动获取Content-Type数据类型 # SQL查询也可以 def get_teacher_add_text(s): '''查询列表页面 第一个数据''' url4 = "http://47.104.190.48:8000/xadmin/hello/teacher/" r4 = s.get(url4) # xpath定位 x = '//*[@id="changelist-form"]/div[1]/table/tbody/tr[1]/td[2]/a' demo = etree.HTML(r4.text) nodes = demo.xpath(x) print("获取列表页面的第一个数据:%s"%nodes[0].text) return nodes[0].text # sql 查询数据库, name 数据条数 x # 新增一组数据 # sql查询数据库,name 数据条数 y # 断言 x+1 == y
unittest封装
import unittest import requests from xadmin_api.login_xadmin_api import login from xadmin_api.teacher_add_api import teacher_add,get_teacher_add_text from common.excuet_sql import delete_sql class TestTeacherAdd(unittest.TestCase): def setUp(self): self.s = requests.session() # 数据准备,清理重复数据 sql = "DELETE from hello_teacher WHERE teacher_name = 'test0002';" delete_sql(sql) def test_01(self): login(self.s) # 登陆 2s teacher_add(self.s) # 添加 result = get_teacher_add_text(self.s) print(result) # 实际结果 self.assertTrue(result == "test0002") if __name__ == '__main__': unittest.main()
图片上传
import requests import re from requests_toolbelt import MultipartEncoder from lxml import etree url = "http://47.104.190.48:8000/login/" s = requests.session() r = s.get(url) # re 知道前面和后面,取中间值 token = re.findall('name="csrfmiddlewaretoken" value="(.+?)"', r.text) print(token[0]) body = { "csrfmiddlewaretoken": token[0], "username": "test", "password": "test" } r2 = s.post(url, data=body) if "登录成功" in r2.text: print("登录成功!") else: print("密码不对,自己申请账号密码") # 登录成功之后的请求 url3 = "http://47.104.190.48:8000/xadmin/hello/fileimage/add/" r_token = s.get(url3) token2 = re.findall('name="csrfmiddlewaretoken" value="(.+?)"', r_token.text) print(token2[0]) body = MultipartEncoder( fields=[ ("csrfmiddlewaretoken", token2[0]), ("csrfmiddlewaretoken", token2[0]), ("title", "图片222"), ("image", ("test.png", open("E:\\test.png", "rb"), "image/png")),#用元组传值,图片名称,上传地址,图片类型 ("fiels", ""), ("_save", ""), ] ) r3 = s.post(url3, data=body, headers={'Content-Type': body.content_type})
文件上传
import requests import re from requests_toolbelt import MultipartEncoder from lxml import etree url = "http://47.104.190.48:8000/login/" s = requests.session() r = s.get(url) # re 知道前面和后面,取中间值 token = re.findall('name="csrfmiddlewaretoken" value="(.+?)"', r.text) print(token[0]) body = { "csrfmiddlewaretoken": token[0], "username": "test", "password": "test" } r2 = s.post(url, data=body) if "登录成功" in r2.text: print("登录成功!") else: print("密码不对,自己申请账号密码") # 登录成功之后的请求 url3 = "http://47.104.190.48:8000/xadmin/hello/fileimage/add/" r_token = s.get(url3) token2 = re.findall('name="csrfmiddlewaretoken" value="(.+?)"', r_token.text) print(token2[0]) body = MultipartEncoder( fields=[ ("csrfmiddlewaretoken", token2[0]), ("csrfmiddlewaretoken", token2[0]), ("title", "文件4444444"), ("image", ("test.png", open("E:\\test.png", "rb"), "image/png")), # 传图片 ("fiels", ("test.png", open("E:\\test.png", "rb"), "image/png")), #上传文件 ("_save", ""), ], ) r3 = s.post(url3, data=body, headers={'Content-Type': body.content_type})
1111