selenium截图

文件结构

1.DateUtil.py

# cncoding = 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())

2.FileUtil.py

# cncoding = 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)
    if not os.path.exists(dateDir):
        # 如果以今天日期命名的目录不存在则创建
        os.mkdir(dateDir)
        # 获取当前的时间字符串
        now = currentTime()
        # 构造以当前时间命名的目录的绝对路径
        timeDir = os.path.join(dateDir,now)
        print(timeDir)
        if not os.path.exists(timeDir):
            # 如果已当前时间命名的目录不存在则创建
            os.mkdir(timeDir)
        return timeDir
    else:
        os.path.join(currentPath, today)
        now = currentTime()
        # 构造以当前时间命名的目录的绝对路径
        timeDir = os.path.join(dateDir, now)
        # print(timeDir)
        if not os.path.exists(timeDir):
            # 如果已当前时间命名的目录不存在则创建
            os.mkdir(timeDir)
        return timeDir

if __name__ == "__main__":
    createDir()

3.SoGou.py

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

'''
创建存在异常截图的目录,并得到本次实例中存放图片目录的绝对路径,
并且作为全局变量,以供本次所有测试用例调用
'''
picDir = createDir()

def takeScreenshot(driver,savePaht,picName):
    # 封装截图方法
    # 构造屏幕截图路径及图片名
    # 因为windows默认编码是GBK,而传入的图片名为utf8编码
    # 所以这里需要进行转码,以便让图片名中的中文字符能正常显示
    print(savePaht,str(picName))
    picPath = os.path.join(savePaht, str(picName).encode('gbk').decode('utf-8').
                           strip()+ ".png")
    try:
        # 调用webDriver提供的get_screenshot_sa_file()方法,
        # 将截图的截图图片保存为本地文件
        driver.get_screenshot_as_file(picPath)
    except Exception as e:
        print(traceback,e)

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

    def tearDown(self):
        self.driver.quit()
    def testSoGoouSearch(self):
        url = "http://www.baidu.com"
        self.driver.get(url)
        try:
            self.driver.find_element_by_id("kw").
                send_keys(u'光荣之路自动化测试')
            self.driver.find_element_by_id('su').click()
            time.sleep(3)
            # 断言页面的代码中是否存在"实在认为"这4个关键字
            # 因为页面中没有这四个字,所以会触发except语句的执行,并触发截图操作
            self.assertTrue(u'事在人为' in self.driver.page_source,
                            "事在在人为关键字在页面源码中未找到")
        except AssertionError as e:
            takeScreenshot(self.driver,picDir,e)
        except Exception as e:
            print(traceback.print_exc())
            takeScreenshot(self.driver,picDir,e)


if __name__ == "__main__":
    unittest.main()
原文地址:https://www.cnblogs.com/zhmiao/p/10548109.html