Python+unittest做UI自动化测试时用例执行失败自动截图,创建以时间命名的文件夹和图片

from selenium import webdriver
import os
import time

def test_currentDate():
    '''生成当前日期字符串'''
    currentDate = time.localtime()
    return '-'.join([str(currentDate.tm_year), str(currentDate.tm_mon), str(currentDate.tm_mday)])

def test_currentTime():
    '''生成当前时间字符串'''
    currentTime = time.localtime()
    return '-'.join([str(currentTime.tm_hour), str(currentTime.tm_min), str(currentTime.tm_sec)])

def test_createDir():
    '''创建当前日期文件夹'''
    base_dir = 'D:\\PyCharm\\script\\Test_CRM\\report\\image\\'+test_currentDate()
    #如果当前目录不存在就创建
    if not os.path.exists(base_dir):
        os.mkdir(base_dir)
    return base_dir

def test_takeScreenshot(driver, imgName):
    '''截图函数'''
    driver.get_screenshot_as_file(test_createDir()+'\\'+imgName+'.png')

if __name__ == '__main__':
    driver = webdriver.Chrome()
    driver.get('http://..........')
    time.sleep(3)
    test_takeScreenshot(driver, test_currentTime())
    driver.quit()

执行结束的结构图如下

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值