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()

执行结束的结构图如下

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

你可能感兴趣的:(UI自动化)