文件结构

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()

selenium截图的更多相关文章

  1. Java&Selenium截图方法封装

    Java&Selenium截图方法封装 package util; import org.apache.commons.io.FileUtils; import org.openqa.sele ...

  2. selenium截图对比校验方法

    /**对比图片进行校验是否成功**/package com.allin.pc;import java.awt.image.BufferedImage;import java.awt.image.Dat ...

  3. selenium 截图加上时间戳

    思路: 1  新建screenshot文件夹,不存在则创建该目录 2  在screenshot文件夹下新建当日日期文件夹,比如20190110:不存在则创建该目录 3  截图保存到当日文件夹,且截图文 ...

  4. selenium 截图 添加时间戳

    在自动化程序中运行的代码报错信息或者是相关日志有可能并无法直观的判断出错信息.因此截图是避免不了的.为了避免因为重复运行或者是图片名称相同导致截图被覆盖. 建议在截图时使用时间戳,保证截图图片名称的唯 ...

  5. selenium - 截图下载图片

    from selenium import webdriver from selenium.webdriver import ChromeOptions from time import sleep f ...

  6. selenium截图功能

    selenium自动化测试完后需要查看值观的结果,或者查操作过程中是否正确,此时需要使用自带的截图功能. 示例1: from time import sleep from selenium impor ...

  7. 关于selenium截图

    没时间深入研究源代码,凭调试解决了非浏览器级别的滚动条截图. 首先,定位到带有滚动条的元素,通过xpath. 其次,获取scrollheight和clientheight. 第三,循环截图,循环截图的 ...

  8. c#使用selenium截图,操作dom元素

    using System; //添加selenium的引用 using OpenQA.Selenium.PhantomJS; using OpenQA.Selenium.Chrome; using O ...

  9. selenium 截图

    http://blog.csdn.net/u010953692/article/details/78320025 # coding:utf-8 # coding:cp936 from selenium ...

随机推荐

  1. bugku crypto easy-crypto

    长度不一的01字符串 就要考虑是不是摩斯密码 用这个py脚本: from __future__ import print_function a = input("input the stri ...

  2. scrollbar样式设置

    转载:https://segmentfault.com/a/1190000012800450?utm_source=tag-newest author:specialCoder 一 前言 在CSS 中 ...

  3. Jetson TX1安装pyTorch

    https://www.ncnynl.com/archives/201705/1629.html https://blog.csdn.net/qq_36302589/article/details/8 ...

  4. python类方法以及类调用实例方法的理解

    classmethod类方法 1) 在python中.类方法 @classmethod 是一个函数修饰符,它表示接下来的是一个类方法,而对于平常我们见到的则叫做实例方法. 类方法的第一个参数cls,而 ...

  5. js原生触发事件

    在JQ时代我们使用如下方式,可以手动触发事件 $("button").click(function(){ $("input").trigger("se ...

  6. 重装系统windows10/8/7,绝对纯净版永久激活的详细步骤和固态硬盘找不到分区的原因

    简介:重装系统有两种: 一种是在线重装,可实现电脑双系统或多系统,也可单系统(重装在另外一个盘,再去格式化系统盘),这种方式比较麻烦,前提电脑能开机使用,但是一般能启动使用也没人去重装系统,但是不需要 ...

  7. 分布式协调服务Zookeeper集群搭建

    分布式协调服务Zookeeper集群搭建 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装jdk环境 1>.操作环境 [root@node101.yinzhengjie ...

  8. JS中如何获取JSON有多少个字段,JSON子项的个数或叫length

    如有JSON数据格式如下: {names:'张三','age':16,'sex':‘男’} 或 {names:'张三','age':16,'sex':‘男’} 如何获取JSON子键的数量?挺头疼的.使 ...

  9. 阿里云ACA主要内容

    阿里云 ACA,云计算助理工程师,是阿里云使用的一个入门级别课程.内容比较浅显,但都很很有意思的知识.课程的内容主要有7门,具体见下图: 课程的学习方式是视频+实验 先学习视频 再实际操作.阿里云为每 ...

  10. sql server 查询log日志 sql语句

    xp_readerrorlog 一共有7个参数: 1. 存档编号 2. 日志类型(1为SQL Server日志,2为SQL Agent日志) 3. 查询包含的字符串 4. 查询包含的字符串 5. Lo ...