报错_截图

DateUtil.py

# encoding = utf-8
import time
from datetime import datetime
'''
本文件用于获取当前日期及时间,用于生成保存截图文件目录名
'''

def currentDate():
    date = time.localtime()
    # 构造今天日期字符串
    today = str(date.tm_year) + "-" + str(date.tm_mon) + "-" + str(date.tm_mday)
    return today

def currentTime():
    timeStr = datetime.now()
    # 构造当前时间字符串
    now = timeStr.strftime('%H - %M -%S')
    return now

if __name__ == "main":
    print(currentDate())
    print(currentTime())

FileUtil.py

# encoding = utf - 8
from DateUtil import currentTime,currentDate
import os

'''
本文件用于存放截图
'''

def createDir():
    # 获得当前文件所在目录的绝对路径
    currentPath = os.path.dirname(os.path.abspath(__file__))
    # 获取今天的日期字符串
    today = currentDate()
    # 构造以日期命名的目录的绝对路径
    dateDir = os.path.join(currentPath,today)
    print(dateDir)
    if not os.path.exists(dateDir):
        os.mkdir(dateDir)
    # 获取当前时间字符串
    now = currentTime()
    # 构造以当前时间命名的目录的绝对路径
    timeDir = os.path.join(dateDir,now)

    if not os.path.exists(timeDir):
        os.mkdir(timeDir)
        return timeDir

if __name__ == '__main__':
    createDir()

Scree.py

# encoding = utf-8
from selenium import webdriver
import unittest,time,os
from FileUtil import createDir
import traceback

picDir = createDir()

def takeScreenshot(driver,savepath,picname):
    picpath = os.path.join(savepath,str(picname)+ ".png")

    try:
        print(picname)
        driver.get_screenshot_as_file(picpath)
    except Exception as e:
        print(traceback.print_exc())


class TestFailCaptureScreen(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()

    def testSoGouSearch(self):
        url = "http://www.sogou.com"
        self.driver.get(url)
        try:
            self.driver.find_element_by_id("query").send_keys("光荣之路自动化测试")
            self.driver.find_element_by_id("stb").click()
            time.sleep(3)
            self.assertTrue("事在人为" in self.driver.page_source,"事在人为没有找到")
        except AssertionError as e:
            takeScreenshot(self.driver,picDir,e)
        except Exception as e:
            takeScreenshot(self.driver,picDir,e)
        except traceback as e:
            takeScreenshot(self.driver, picDir, e)

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

你可能感兴趣的:(报错_截图)