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()
selenium截图的更多相关文章
- Java&Selenium截图方法封装
Java&Selenium截图方法封装 package util; import org.apache.commons.io.FileUtils; import org.openqa.sele ...
- selenium截图对比校验方法
/**对比图片进行校验是否成功**/package com.allin.pc;import java.awt.image.BufferedImage;import java.awt.image.Dat ...
- selenium 截图加上时间戳
思路: 1 新建screenshot文件夹,不存在则创建该目录 2 在screenshot文件夹下新建当日日期文件夹,比如20190110:不存在则创建该目录 3 截图保存到当日文件夹,且截图文 ...
- selenium 截图 添加时间戳
在自动化程序中运行的代码报错信息或者是相关日志有可能并无法直观的判断出错信息.因此截图是避免不了的.为了避免因为重复运行或者是图片名称相同导致截图被覆盖. 建议在截图时使用时间戳,保证截图图片名称的唯 ...
- selenium - 截图下载图片
from selenium import webdriver from selenium.webdriver import ChromeOptions from time import sleep f ...
- selenium截图功能
selenium自动化测试完后需要查看值观的结果,或者查操作过程中是否正确,此时需要使用自带的截图功能. 示例1: from time import sleep from selenium impor ...
- 关于selenium截图
没时间深入研究源代码,凭调试解决了非浏览器级别的滚动条截图. 首先,定位到带有滚动条的元素,通过xpath. 其次,获取scrollheight和clientheight. 第三,循环截图,循环截图的 ...
- c#使用selenium截图,操作dom元素
using System; //添加selenium的引用 using OpenQA.Selenium.PhantomJS; using OpenQA.Selenium.Chrome; using O ...
- selenium 截图
http://blog.csdn.net/u010953692/article/details/78320025 # coding:utf-8 # coding:cp936 from selenium ...
随机推荐
- C# call webservice方法
https://www.cnblogs.com/Fooo/p/5507153.html
- python调用openstack的api,create_instance的程序解析
python调用openstack的api,create_instance的程序解析 2017年10月17日 15:27:24 CloudXli 阅读数:848 版权声明:本文为博主原创文章,未经 ...
- JDK源码分析(8) StringBuffer & StringBuilder
简介 StringBuffer与StringBuilder是两个常用的操作字符串的类.大家都知道,StringBuilder是线程不安全的,而StringBuffer是线程安全的.前者是JDK1.5加 ...
- App测试全(转自鲁德)
1.App测试流程 1.1流程图 1.2测试周期 测试周期可按项目的开发周期来确定测试时间,一般测试时间为两三周(即15个工作日),根据项目情况以及版本质量可适当缩短或延长测试时间. 1.3测试资源 ...
- laravel 读写分离源码解析
前言:上一篇我们说了<laravel 配置MySQL读写分离>,这次我们说下,laravel的底层代码是怎样实现读写分离的. 一.实现原理 说明: 1.根据 database.php ...
- 第二篇--上传git 代码
准备工作: 首先,注册一个GitHub账号. 接着,新建一个仓库. 最后,下载Git 上传代码步骤: 第一步,新建一个本地文件夹作为本地仓库,进入该文件夹. 右击选择Git Bash Here ,输入 ...
- 第四十四篇--做一个简单的QQ登录界面
功能:输入用户名和密码,正确,显示登录成功,为空的话,提示用户名和密码不能为空,还有记住密码功能. MainActivity.java package com.aimee.android.play.q ...
- JAVA helloworld!
idea创建java项目 https://jingyan.baidu.com/article/48b558e3f8f6637f39c09a44.html 本地文档运行 java helloworld ...
- Git复制已有分支到新分支开发
如果我们需要在现有的分支代码基础上,复制代码到新分支进行开发,并推送至远程仓库,可以进行如下操作: 注:被复制的分支代码(ibis35),创建新的分支(ibis35-dev) 1. 切换到被copy的 ...
- 前端开发IDE VSCode + live preview
live preview 为方便web开发者,编写网页静态代码,不用频繁切换到浏览器去查看的贴心功能. 可以在编辑器中,就实现编辑预览功能, 边编写代码,边查看预览效果. 与vscode相对应的其他两 ...