python+selenium截图操作样例

# -*- coding:utf-8 -*-
# explain : 截图操作
import os, time


class Picture:
    """
         截图操作
         分为正常的操作截图truepicture
         和异常的操作截图errorpicture
    """
    def __init__(self, driver, path):
        """
        :type driver: selenium.webdriver.remote.webdriver.WebDriver
        :param driver WebDriver对象
        :param path 截图存放位置
        """
        self.__driver = driver
        self.__path = path

    def __picturelocation(self):
        # path = Primary.readini.getvalue("picture", "picturelocation")
        path = self.__path
        if not os.path.exists(path):  # 判断图片位置是否存在,不存在则先创建
            os.makedirs(path)
        picdatetime = time.strftime("%Y-%m-%d", time.localtime())  # 文件名为日期
        # 正确图片目录
        turepath = os.path.join(path, picdatetime + '/turepicture')  # 正确图片位置
        if not os.path.exists(turepath):
            os.makedirs(turepath)
        # 错误图片目录
        errorpath = os.path.join(path, picdatetime + '/errorpicture') # 错误图片位置
        if not os.path.exists(errorpath):
            os.makedirs(errorpath)
        return [turepath, errorpath]

    def truepicture(self, picusername):
        """
        正常操作的截图
        文件的保存位置在配置文件的turepicture
        :param picusername:  图片的文件名称
        """
        turepath = self.__picturelocation()[0]
        # 通过路径来拼接图片存放路径
        self.__driver.get_screenshot_as_file(turepath + os.sep + picusername +
                                             time.strftime("%Y-%m-%d%H%M", time.localtime()) + '.png')

    def errorpicture(self, picusername):
        """
        出现错误的截图操作
        文件的保存位置在配置文件的errorpicture
        :param picusername:  图片的文件名称
        """
        # 通过路径来拼接图片存放路径
        errorpath = self.__picturelocation()[1]
        # 截图
        self.__driver.get_screenshot_as_file(errorpath + os.sep + picusername +
                                             time.strftime("%Y-%m-%d%H%M", time.localtime()) + '.png')

UI自动化截图操作简单样例

猜你喜欢

转载自blog.51cto.com/11463754/2483053