zoukankan      html  css  js  c++  java
  • 封装selenium自动化框架中的截图功能

      对selenium自带的截图功能进行封装:

      以下为封装的代码,自定义一个.py文件即可,图片路径自己设置一个。

     1 #coding:utf-8
     2 
     3 class Screen(object):
     4     '''
     5         封装的截图类,webdriver自带的get_screenshot_as_file()
     6         在使用过程中,注意driver参数的传递
     7     '''
     8     def __init__(self, driver):
     9         '''
    10             写一个构造函数,有一个参数driver
    11         '''
    12         self.driver = driver
    13 
    14 
    15     def save_screen(self):
    16         '''
    17             截图并保存在根目录下的Screenshots文件夹下,并传输参数caseName
    18         '''
    19         self.log = TestLog().getlog()
    20         img_time = time.strftime("%Y_%m_%d_%H_%M_%S_")
    21         img_path = "D:\python\workspace\pythontest\screenshot\"
    22         img_name = img_path + img_time + 'screen.png'
    23         self.img_info = "截图路径:" + img_name
    24         try:
    25             self.driver.get_screenshot_as_file(self.img_info)
    26             self.log.info(self.img_info)
    27         except Exception as e:
    28             self.log.info("截图异常->", e)
    29             format(e)
    View Code

       我认为这其中主要的难点就是driver参数的传递。

      前提必须引用selenium中的webdriver,赋值driver = webdriver.Firefox(),然后把driver参数传递到封装的模块中。

  • 相关阅读:
    redis 数据库总结
    drf 序列化类总结
    drf 视图类经典总结
    celery 简介
    虚拟环境搭建pip换源
    git 与 svn,简介差别
    redis 数据库简介
    auth 模块
    python的注释与用户交互 基本数据类型
    python入门
  • 原文地址:https://www.cnblogs.com/zhuque/p/8320822.html
Copyright © 2011-2022 走看看