美文网首页
【python3-2】读取html报告返回值,作为接口传参调用

【python3-2】读取html报告返回值,作为接口传参调用

作者: _王子_ | 来源:发表于2019-03-02 15:23 被阅读0次

    获取读取报告数值

    把读取报告结果值作为参数变量,作为参数传给请求接口调用

    import urllib
    import http.cookiejar
    import requests
    import json
    import sys
    from urllib import request
    from bs4 import BeautifulSoup
    # 读取本地报告html文件
    path = 'D:\\ant\\ResultReport\\html\\接口测试报告201902260414.html'
    
    with open(path, 'r',encoding='utf-8') as f:
        Soup = BeautifulSoup(f.read(), 'lxml')
        titles = Soup.select('html > body > table > .Failure > td')
    lists = [] #生成数组
    for title in titles:
         lists.append(title.text)
    #打印对应参数数组的值
    print("api_total:",lists[0],",","api_pass_total:",lists[1],",","api_pass_rate:",lists[3],",","excute_time:",lists[7])
    
    url = " http://godeye.hqygou.com/api/receiveAutoTestResultTest"
    # postdata =urllib.parse.urlencode({
    header = {"Content-Type":"application/json"}
    raw={
         "api_key": "2b0e740f99f20906a54d04ebe9816d9b",
         "api_sign": "sTest123!@#",
         "project_id": 10007,
         "project_name": "SOA",
         "platform": "OTHER",
         "level": "Core",
         "case_total": 0,
         "case_pass_total": 0,
         "case_pass_rate": 0,
         "api_total": lists[0],
         "api_pass_total": lists[1], # 调用返回值作为参数
         "api_pass_rate": lists[3], # 调用返回值作为参数
         "execute_detail": [{
                "case_suit_id": "1",
                "case_suit_name": "soa-订单",
                "module": "order",
                "case_total": "0",
                "case_pass_total": "0",
                "case_pass_rate": "0",
                "api_total": lists[0], # 调用返回值作为参数
                "api_pass_total": lists[1], # 调用返回值作为参数
                "api_pass_rate": lists[3] # 调用返回值作为参数
            }],
         "execute_stage": "RegressionTest",
         "execute_scene": "BaselineRelease",
         "execute_type": "Normal",
         "autotest_type": "Interface",
         "autotest_source": "Jmeter",
         "excute_start_time": "2019-02-6 15:25:04",
         "excute_end_time": "2019-02-6 15:25:04",
         "excute_time": lists[7], # 调用返回值作为参数
         "report_url": "shop",
         "test_result": 1,
         "execute_result": 1,
         "desc": ""
    }
    # }).encode("utf-8")
    data = json.dumps(raw)
    data1 = bytes(data,"utf8")
    print(data1.decode('unicode_escape')) #转换unicode的中文编码
    req = urllib.request.Request(url,data1,header)
    # print(urllib.request.urlopen(req).read().decode('utf-8'))
    
    # #自动记住cookie
    cj = http.cookiejar.CookieJar()
    opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
    r = opener.open(req)
    # print(r.read().decode("utf-8")) #打印响应参数带unicode的中文编码
    print(r.read().decode('unicode_escape')) #转换unicode的中文编码
    

    执行脚本,返回结果
    转换前结果:


    执行脚本结果

    转换后结果


    执行脚本结果

    相关文章

      网友评论

          本文标题:【python3-2】读取html报告返回值,作为接口传参调用

          本文链接:https://www.haomeiwen.com/subject/uedeuqtx.html