粗暴解决 HTMLTestRunner 加入截图展示功能

最近使用appium+python来做自动化,测试报告用的网上共享的HTMLTestRunner模块,但是里面貌似没有展示截图的功能,于是自己动手稍微修改了一下!
先上成果图,点击图片名字就能打开图片

总体思路为:在HTMLTestRunner的report模板中加入一列picture,然后将用来展示图片的html打印到每个case的日志中,再从日志中将这段文字截取出来放入report中(主要是为了简单粗暴的将每个case与相关截图正确对应起来~想不到其他办法啦!欢迎高手们给出更加简单快捷的办法~)
以下为我的操作方法,有点长~
一、在HTMLTestRunner.py里的原有的html表格中插入了一列picture(在

View下面加入一行Picture,在 下加入一行即可),用来展示脚本中的截图超链接
<tr id='header_row'>
    <td>Test Group/Test case</td>
    <td>Count</td>
    <td>Pass</td>
    <td>Fail</td>
    <td>Error</td>
    <td>View</td>
    <td>Picture</td>
</tr>
%(test_list)s
<tr id='total_row'>
    <td>Total</td>
    <td>%(count)s</td>
    <td>%(Pass)s</td>
    <td>%(fail)s</td>
    <td>%(error)s</td>
    <td>&nbsp;</td>
    <td><a href="" target="_blank"></a></td>
</tr>
</table>

二、还是在HTMLTestRunner.py里插入一行%(html)s来放置对应脚本的截图

    REPORT_TEST_WITH_OUTPUT_TMPL = r"""
<tr id='%(tid)s' class='%(Class)s'>
    <td class='%(style)s'><div class='testcase'>%(desc)s</div></td>
    <td colspan='5' align='center'>

    <!--css div popup start-->
    <a class="popup_link" onfocus='this.blur();' href="javascript:showTestDetail('div_%(tid)s')" >
        %(status)s</a>

    <div id='div_%(tid)s' class="popup_window">
        <div style='text-align: right; color:red;cursor:pointer'>
        <a onfocus='this.blur();' onclick="document.getElementById('div_%(tid)s').style.display = 'none' " >
           [x]</a>
        </div>
        <pre>
        %(script)s
        </pre>
    </div>
    <!--css div popup end-->

    </td>
    %(html)s
</tr>

三、依然在HTMLTestRunner.py中,_generate_report_test()函数中,插入html的内容,因为本身初学者,对unittest和htmltestrunner都不怎么了解,观察htmltestrunner的报告发现它能展示脚本执行过程中打印的log文本,所以我强行把自己组装的用于展示图片html代码打印到执行日志中,然后通过关键字截取出来,放置在script后面的位置,也就是刚才新建的picture列(要是觉得最后report中打印的log中加入了这段html影响美观,可以在下面代码中script里删掉~我懒得写了~)

script = self.REPORT_TEST_OUTPUT_TMPL % dict(
    id = tid,
    output = saxutils.escape(uo+ue),
)

s = uo+ue
html = s[s.find('htmlbegin')+9:s.find('htmlend')]

row = tmpl % dict(
    tid = tid,
    Class = (n == 0 and 'hiddenRow' or 'none'),
    style = n == 2 and 'errorCase' or (n == 1 and 'failCase' or 'none'),
    desc = desc,
    script = script,
    html = html,
    status = self.STATUS[n],
)

四、在自己封装的手机截屏方法中加入分别将截图名称和截图存放路径插入对应的数组中(也可以使用二维数组),以下分别为打印日志,截屏,组装html的方法,在组装完成的html代码开头和结尾分别叫上htmlbegin和htmlend作为之后截取使用的关键字

# _*_ coding:utf-8 _*_
__author__ = 'gjj14453'
import logging
import os
import common.myDate as myDate


PATH = lambda p: os.path.abspath(
    os.path.join(os.path.dirname(__file__), p))


pics = []
picpath = []


##################################
#  日志
# 0: debug
# 1:info
# 2:warning
# -1:error
###################################
def mylogger(msg, flag=1):
    logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(filename)s %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename='mylog.log',
                    filemode='w')
    console = logging.StreamHandler()
    console.setLevel(logging.DEBUG)
    formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
    console.setFormatter(formatter)
    logging.getLogger('').addHandler(console)
    if flag == 0:
        logging.debug(msg)
    elif flag == 1:
        logging.info(msg)
    elif flag == 2:
        logging.warning(msg)
        screenshot()
    elif flag == -1:
        logging.error(msg)
        screenshot()
    logging.getLogger('').removeHandler(console)


#  截屏
def screenshot():
    dirname = PATH('D:\\MyConfiguration\\gjj14453\\PycharmProjects\\untitled\\list_out' + "/screenshot")
    os.popen("adb wait-for-device")
    os.popen("adb shell screencap -p /data/local/tmp/tmp.png")
    if not os.path.isdir(dirname):
        os.makedirs(dirname)
    pic = myDate.timestampname() + ".png"
    path = dirname + "/" + pic
    os.popen("adb pull /data/local/tmp/tmp.png " + PATH(path))
    os.popen("adb shell rm /data/local/tmp/tmp.png")
    mylogger("截图为:" + pic)
    pics.append(pic)
    picpath.append(path)


def creathtml(path, pic):
    html = ''
    if range(len(path)) > 0:
        for i in range(len(path)):
            if i == 0:
                html = '<a href=' + path[i] + ' target="_blank">' + pic[i] + '</a>'
            else:
                html = html + '<br /><a href=' + path[i] + ' target="_blank">' + pic[i] + '</a>'
    else:
        html = ''
    htmls = 'htmlbegin<td>' + html +'</td>htmlend'
    return htmls

五、之后在case中加入打印html到日志中的语句就OK啦


class Test(unittest.TestCase):

    def setUp(self):
        log.picpath = []
        log.pics = []
        pass

    def tearDown(self):
        pass

    def test(self):
        log.mylogger('aaa', -1)
        log.mylogger('bbb', -1)
        log.mylogger(log.creathtml(log.picpath, log.pics))



以上是大牛的原创, 好像是针对安卓测试的,自己修改了下,整合到了web端的自动化测试中了。
修改的地方包括:
1、移动log.py到htmlrunnertest的同级目录下
2、修改log中的截图函数,直接用PIL来实现的。
3、更新了截图的时机,在执行出错的时候,才去截图处理。
后台后的部分代码如下。
HTMLrunnertest.py
def addError(self, test, err):
        log.picpath = []
        log.pics = []
        log.mylogger('errorTTT', -1)
        log.mylogger(log.creathtml(log.picpath, log.pics),-1)
        self.error_count += 1


log.py
#  截屏
def screenshot():
    dirname = PATH('D:\\**')
    if not os.path.isdir(dirname):
        os.makedirs(dirname)
    
   pic = time.strftime("%y%m%d%H%M%S")+str(random.randint(1,100))+".png"

    im = ImageGrab.grab() 
    
    path = dirname + "\\" + pic
    im.save(path)
    mylogger("截图为:" + pic)
    pics.append(pic)
    picpath.append(path)
    print 'pic path is :',path









  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个用Python生成测试图片的程序,它可以生成一个黑色背景上的梯形形状: ```python import cv2 import numpy as np def generate_test_image(img_width, img_height, top_width_ratio, bottom_width_ratio, height_ratio): """ 生成测试图片,梯形形状 :param img_width: 图像宽度 :param img_height: 图像高度 :param top_width_ratio: 上底宽度比例 :param bottom_width_ratio: 下底宽度比例 :param height_ratio: 矩形高度比例 :return: 生成的图像 """ # 创建黑色背景 img = np.zeros((img_height, img_width, 3), np.uint8) # 计算四个顶点坐标 x1 = int(img_width * (0.5 - top_width_ratio / 2)) x2 = int(img_width * (0.5 + top_width_ratio / 2)) x3 = int(img_width * (0.5 + bottom_width_ratio / 2)) x4 = int(img_width * (0.5 - bottom_width_ratio / 2)) y1 = int(img_height * (1 - height_ratio)) y2 = img_height - 1 # 绘制梯形 pts = np.array([[x1, y1], [x2, y1], [x3, y2], [x4, y2]], np.int32) pts = pts.reshape((-1,1,2)) cv2.fillPoly(img, [pts], (255,255,255)) return img ``` 示例用法: ```python # 生成测试图片 img_width = 640 img_height = 480 top_width_ratio = 0.5 bottom_width_ratio = 1 height_ratio = 0.5 img = generate_test_image(img_width, img_height, top_width_ratio, bottom_width_ratio, height_ratio) # 显示生成的图像 cv2.imshow('Generated Image', img) cv2.waitKey(0) ``` 其,`img_width`, `img_height`, `top_width_ratio`, `bottom_width_ratio`, `height_ratio` 分别表示图像宽度、图像高度、上底宽度比例、下底宽度比例和矩形高度比例,根据实际情况进行调整即可。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值