FAT框架里有截图的功能,我主要介绍,根据自己的需要,自主创建文件夹,用于存放用例的截图。
因为我的main函数是放根目录下的,所以截图也是放在根目录的TestResult文件夹下。
首先截图之前,先判断当天日期的文件夹是否存在,如果存在就跳过新建文件夹,直接截图。截图的名称是按照年月日时分秒来命名,基本上不会出现重复的情况。
import os
import time
from fastAutoTest.utils.logger import Log
import sys
class NewDirectory:
def __init__(self):
self.logger = Log().getLogger()
def newdir(self, realpath,needscreenshot=True):
date = time.strftime('%Y%m%d', time.localtime(time.time()))
datetime = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
dirPath = os.getcwd()
dirPath = dirPath.split("TestCase")[0] + "\\TestResult"
newPath = (dirPath + "\\%s" % date)
if not os.path.exists(newPath):
os.makedirs(newPath)
self.logger.info(' 文件夹新建成功! ')
else:
pass
self.logger.info(' 文件夹已存在!')
# 截图文件命名方式:当前用例名称+当前日期
if needscreenshot:
filename = realpath.split('Case\\')[1].split('.py')[0]
picname = os.path.join(newPath, filename + "_%s.png" % datetime)
self.logger.info('已截图 ')
return picname
else:
pass
以需要截图的 文件名称+年月日时分秒 命名。所以需要在运行的文件内获取文件名,然后将文件名传给截图的方法newdir里
realpath = os.path.realpath(__file__)
picname = newdir.NewDirectory().newdir(realpath)
self.wxDriver.d.screenshot(picname)
**有疑问的加V了解详情:zx1187463903**