博客
关于我
生成以时间命名的目录(以存放异常截图或日志文件)
阅读量:483 次
发布时间:2019-03-06

本文共 2242 字,大约阅读时间需要 7 分钟。

 

 

工具类:FileUtil.py

本模块主要用于获取当前的日期以及时间,用于生成保存截图文件目录名等场景。

1 import time, os 2 from datetime import datetime 3  4  5 # 输出当前时间格式:年-月-日 6 def currentDate(): 7     date = time.localtime() 8     # 输出:time.struct_time(tm_year=2018, tm_mon=1, tm_mday=21, tm_hour=23, tm_min=27, tm_sec=43, tm_wday=6, tm_yday=21, tm_isdst=0) 9     # 构造今天的日期字符串10     today = str(date.tm_year) + "-" + str(date.tm_mon) + "-" + str(date.tm_mday)11     return today12 13 14 # 输出当前时间格式:时-分-秒15 def currentTime():16     timeStr = datetime.now()17     now = timeStr.strftime("%H-%M-%S")18     return now19 20 21 # 创建目录:年月日为父目录,时分秒为子目录22 def createDir():23     # 获得当前文件所在目录的绝对路径24     currentPath = os.path.dirname(os.path.abspath(__file__))25     today = currentDate()26     dateDir = os.path.join(currentPath, today)27     print("日期目录:%s" % dateDir)28     if not os.path.exists(dateDir):29         # 如果以今天日期命名的目录不存在则创建30         os.mkdir(dateDir)31     now = currentTime()32     timeDir = os.path.join(dateDir, now)33     print("时间目录:%s" % timeDir)34     if not os.path.exists(timeDir):35         # 如果以今天日期命名的目录不存在则创建36         os.mkdir(timeDir)       37    38     return timeDir39 40 41 if __name__ == "__main__":42     print(createDir())

 

截屏工具类:ScreenShot.py

封装异常截图。

1 from selenium import webdriver 2 import DateUtil 3 import os 4 import traceback 5 import time 6  7  8 # 封装截屏方法 9 def take_screen_shot(driver, savePath, picName):10     # 构造截屏路径及图片名11     picPath = os.path.join(savePath, picName+".png")12     try:13         driver.get_screenshot_as_file(picPath)14         print("截图成功:%s" % picName+".png")15     except Exception:16         print("截图失败:%s" % traceback.print_exc())17 18 19 # 测试示例20 if __name__ == "__main__":21     picDir = DateUtil.createDir()22 23     def test():24         try:25             # 序号用来作为文件名结尾,防止文件名重复26             num = 027             driver = webdriver.Chrome()28             driver.get("http://www.baidu.com")29             assert "hiphop" in driver.page_source30         except AssertionError as e:31             num += 132             take_screen_shot(driver, picDir, "AssertionError"+str(num))33         except Exception as e:34             num += 135             take_screen_shot(driver, picDir, "Exception"+str(num))36 37     test()

 

转载地址:http://gybyz.baihongyu.com/

你可能感兴趣的文章
MTK Android 如何获取系统权限
查看>>
MySQL - 4种基本索引、聚簇索引和非聚索引、索引失效情况、SQL 优化
查看>>
MySQL - ERROR 1406
查看>>
mysql - 视图
查看>>
MySQL - 解读MySQL事务与锁机制
查看>>
MTTR、MTBF、MTTF的大白话理解
查看>>
mt_rand
查看>>
mysql -存储过程
查看>>
mysql /*! 50100 ... */ 条件编译
查看>>
mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
查看>>
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>