当前位置 博文首页 > 文章内容

    学习httprunner记录02

    作者: 栏目:未分类 时间:2020-06-24 14:50:53

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    使用测试脚本完成文件的自动上传,以下脚本是使用requests实现的:

    使用的环境

    python3.6

    pipenv 虚拟化环境

    测试服务器是禅道,功能场景是提交bug添加附件(可以是图片、文本,压缩包等等)

     

     

    # coding:utf-8
    import requests
    
    host = 'http://192.168.75.175'
    
    h1 = {
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "X-Requested-With": "XMLHttpRequest",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36",
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    "Accept-Encoding": "gzip, deflate",
    "Referer": host + "/zentao/user-login.html",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,lb;q=0.7",
    "Connection": "keep-alive"
    }
    
    
    url = host + '/zentao/user-login.html'
    
    body = {
        'account': 'admin',
        'password': 'qazwsx123',
        'referer': host+ '/zentao/',
        'keepLogin': 0
    }
    
    s = requests.session()
    
    r = s.post(url, data=body, headers=h1)
    
    # print(r.content.decode('utf-8'))
    
    # cookies = requests.utils.dict_from_cookiejar(r.cookies)
    #
    # for key in cookies.keys():
    #     if key == 'zentaosid':
    #         c = cookies.get(key)
    #         print(c)
    # files = {
    #     'files': open(r'/Users/wdy/Downloads/py3env/query_IP.json', 'rb'),
    #     'Content-Disposition': 'form-data',
    #     'Content-Type': 'application/json',
    #     'filename': 'query_IP.json'
    #
    # }
    
    files = {
        'files': open(r'load.txt', 'rb'),
    }
    
    
    data = {
    
        "color": "",
        "title": "test file upload",
        "steps": "<p>[步骤]</p>\n<br />\n<p>[结果]</p>\n<br />\n<p>[期望]</p>\n<br />",
        "comment": "hello world",
        "labels[]": "load.txt",
        "files[]": "load.txt",
        "uid": "5ef2ace7661f4",
        "product": "1",
        "module": "0",
        "plan": "",
        "type": "codeerror",
        "severity": "3",
        "pri": "3",
        "status": "active",
        "assignedTo": "admin",
        "os": "osx",
        "keywords": "",
        "mailto[]": "",
        "project": "",
        "task": "",
        "openedBuild[]": "trunk",
        "resolvedBy": "",
        "resolvedDate": "",
        "resolvedBuild": "",
        "resolution": "",
        "duplicateBug": "0",
        "closedBy": "",
        "closedDate": ""
    
    }
    
    headers = {
    "Host":	"192.168.75.175",
    "Upgrade-Insecure-Requests": "1",
    "Origin": "http://192.168.75.175",
    #"Content-Type":	"multipart/form-data; boundary=----WebKitFormBoundaryvVlF9qeZZGCBZnBK",# 不需要设置,requests能够根据传递的参数自动识别
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "Referer":	"http://192.168.75.175/zentao/bug-edit-1.html",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,lb;q=0.7",
    "Pragma": "no-cache",
    "Cache-Control": "no-cache"
    }
    
    
    upload_url = "http://192.168.75.175/zentao/bug-edit-2.html"
    
    
    ur = s.post(upload_url, files=files, data=data, headers=headers) 
    print(ur.status_code)
    print(ur.content)
    

     

    以下脚本是使用httprunner3.x版本,先录制再手动修改完成,以便于和requests脚本相对比;

    根据httprunner官方文档,接口调用部分是借助requests实现的,所以脚本表现形式有所差异,

    但实质过程是一致的;

     

    运行httprunner过程中发现两个bug,暂时先不把脚本贴出来了