zoukankan      html  css  js  c++  java
  • UI自动化测试:页面截图的3种方法

    前言

    在进行无人值守的UI自动化测试,如果页面操作出现了问题,可以用截图的方式保留问题现场,同时佐证自己发现的问题。下面将介绍3种截图的方式:

    driver.get_screenshot_as_file()

    driver.save_screenshot()

    Pillow包

    前两种是selenium自带的api,最后一个需要单独安装第3方的包

    测试代码

    1、get_screenshot_as_file():

     

    复制代码
    复制代码 

    import time

    from datetime import datetime

    from selenium import webdriver

    import traceback

    def currentDate():

    '''生成当前日期字符串'''

    date = time.localtime()

    return '-'.join([str(date.tm_year), str(date.tm_mon),str(date.tm_mday)])

    def currentTime():

       '''生成当前时间字符串'''

    date = time.localtime()

    return '-'.join([str(date.tm_hour), str(date.tm_min),str(date.tm_sec)])

    def createDir():

        '''创建当前日期和当前时间目录'''

    path = os.path.dirname(os.path.abspath(__file__))

    dateDir = os.path.join(path,currentDate())

    #如果当前日期目录不存的话就创建

    if not os.path.exists(dateDir):

    os.mkdir(dateDir)

    timeDir= os.path.join(dateDir,currentTime())

    #如果当前时间目录不存的话就创建

    if not os.path.exists(timeDir):

    os.mkdir(timeDir)

    return timeDir

    def takeScreenshot(driver,savePath,pictureName):

    picturePath = os.path.join(savePath, pictureName+'.png')

    try:

    driver.get_screenshot_as_file(picturePath)

    except Exception as e:

    print(traceback.print_exc())

    # 获取浏览器驱动实例

    driver = webdriver.Chrome(executable_path='f:\chromedriver')

    #访问搜狗首页

    driver.get('http://www.sogou.com')

    time.sleep(3)

    #搜狗首页截图

    takeScreenshot(driver,createDir(),'sogou')

    time.sleep(1)

    #退出浏览器

    driver.quit()

    复制代码
    复制代码

     

    执行完成后在当前目录下可以看到如下结果:

     

    2、save_screenshot():

    此方法和上一种是一样的,只需要对takeScreenshot函数作如下修改即可:

    复制代码
    复制代码

    def takeScreenshot(driver,savePath,pictureName):

       picturePath = os.path.join(savePath, pictureName+'.png')

       try:

           #和get_screenshot_as_file方法类似

           driver.save_screenshot(picturePath)

       except Exception as e:

           print(traceback.print_exc())

    复制代码
    复制代码

     

    执行完成后在当前目录下可以看到如下结果:

    3、Pillow包:

    需要先在命令行下安装pillow包,命令如下:pip install Pillow,如果电脑中有多个python版本,而且想指定版本安装的话参考之前的文章指定python版本pip安装 

    复制代码
    复制代码

    #encoding=utf-8

    import os

    import time

    from datetime import datetime

    from selenium import webdriver

    import traceback

    from PIL import ImageGrab

    def currentDate():
        '''生成当前日期字符串'''
        date = time.localtime()
        return '-'.join([str(date.tm_year), str(date.tm_mon),str(date.tm_mday)])

    def currentTime():
        '''生成当前时间字符串'''
        date = time.localtime()
        return '-'.join([str(date.tm_hour), str(date.tm_min),str(date.tm_sec)])


    def createDir():

        '''创建当前日期和当前时间目录'''

        path = os.path.dirname(os.path.abspath(__file__))

        dateDir = os.path.join(path,currentDate())

        #如果当前日期目录不存的话就创建

        if not os.path.exists(dateDir):

           os.mkdir(dateDir)

        timeDir= os.path.join(dateDir,currentTime())

        #如果当前时间目录不存的话就创建

        if not os.path.exists(timeDir):

            os.mkdir(timeDir)

        return timeDir

    def takeScreenshot(driver,savePath,pictureName):

        picturePath = os.path.join(savePath, pictureName+'.png')

        try:

            im=ImageGrab.grab()

            im.save(picturePath,"jpeg")

        except Exception as e:

            print(traceback.print_exc())

    # 获取浏览器驱动实例

    driver = webdriver.Chrome(executable_path='f:\chromedriver')

    #访问搜狗首页

    driver.get('http://www.sogou.com')

    time.sleep(3)

    #搜狗首页截图

    takeScreenshot(driver,createDir(),'sogou')

    time.sleep(1)

    #退出浏览器

    driver.quit()

    复制代码
    复制代码

     

    执行完成后在当前目录下可以看到如下结果:

    注意
    前两种方式截的是浏览器的相关页面,第3种是整个电脑桌面;

    创建时间或日期目录时%Y-%m-%d_%H:%M:%S这种是错误的格式,所有的包括 '' : / ? * < > |   这些特殊字符windows文件名无都无法使用,也无法保存,起名时需要注意,可以换成这种%Y-%m-%d_%H_%M_%S

     


    ————————————————
    版权声明:本文为CSDN博主「小小小小人ksh」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/kongsuhongbaby/article/details/87743133

     

  • 相关阅读:
    一些智力题
    17分钟过桥问题
    快排的非递归实现
    单链表逆置
    实现所有括号的合法匹配
    2013阿里笔试题
    Hadoop学习笔记—14.ZooKeeper环境搭建
    Hadoop学习笔记—15.HBase框架学习(基础知识篇)
    Hadoop学习笔记—13.分布式集群中节点的动态添加与下架
    Hadoop学习笔记—11.MapReduce中的排序和分组
  • 原文地址:https://www.cnblogs.com/kuaileya/p/12054926.html
Copyright © 2011-2022 走看看