在BeautiflReport中实现手动截图

BeautifulReport(以下简称BR)是一个出色的基于unittest的Html报告生成库。
BR默认的截图方式是通过语法糖,在assert抛异常时自动截图。
https://github.com/TesterlifeRaymond/BeautifulReport
在BeautiflReport中实现手动截图_第1张图片
但有时候我们需要手动截图,甚至在用例顺利通过时截图,之前的装饰器就不太好用了。
所以要解决这个问题,可以手动写一个GetScreen方法,镶嵌到Html里。
具体实现如下:

def GetScreen(startTime,devices,action):
	#定义截图存放目录的路径
    screenpath = os.path.join(os.getcwd(), "Screen")
	#生成图片,此时图片还为空。action是字符串类型,存图片的说明信息
    png = screenpath +"\\"+ time.strftime('%Y%m%d_%H%M%S', time.localtime(startTime)) + "_" +  "_" + action+ ".png"
    #调用adb shell screencap方法截图,并存在手机的本地
    os.system("adb -s " + devices + " shell screencap -p /sdcard/screencap.png")
    fp = open(png, "a+", encoding="utf-8")
    fp.close()
    #将手机上的图片,转存到截图目录里。
    os.system("adb -s " + devices + " pull /sdcard/screencap.png " + png)
    #将图片发送到Html报告里,这里借用了BR生成报告的一个特性,它会将所有的print都带进报告里,这样使用预先写好的标签,可以将png嵌入Html。
    print("")
    return png

然后,在unittest的test类里,如图一样,调用GetScreen()方法即可。在BeautiflReport中实现手动截图_第2张图片
最后得到的效果如下:
在BeautiflReport中实现手动截图_第3张图片

你可能感兴趣的:(python,BeautifulReport)